Thursday, 5 May 2011

XNA, C# The beginning of a journey.

I have started learning how to program games in XNA, main reason is because I want to expand my programming skill base and think it would just be pretty awesome to be able to create my own! I have decided to share what I learn and hope that others benefit from it. Before you read any further please bare in mind that I have never done this sort of coding before and some of the math is new on me. So if you have any suggestions or general comments please make yourself heard!
for more info on XNA check out http://create.msdn.com/en-US/
So I had this idea for this massive game, set in space that would feature epic MMORTS battles and extremely high graphics and.... Then I realised I didn't have the knowledge, the time or the money to create something on that scale. I settled on the basics. What can get more basic than drawing a 2D sprite to a screen and then adding some interaction. Right?

After a good few hours of work, here's what I had!


So loading the Sprite was simple, it was just a case of adding an Image file to Assetlist assigning it to an object and calling the object from the main class. That was the easy part.

The hard part came next, I wanted to be able to rotate the sprite using the keyboard and then when the player presses forward, I wanted to the sprite to go in the direction it was pointing. There's some maths involved I haven't heard of for a long time.

Vector2 Pos = new Vector2(400, 300); //Position Sprite will spawn in
Vector2 oRigin = new Vector2(34, 114); //Sprites Origin
Vector2 dIrection; //Initialise Direction vector
Vector2 up = new Vector2(0, -1); //defining which way is forward
float angle = 0; //the value that changes direction
float rotation_angle; //angle after it is converted to angle from radians
string debug_info;

So I had declared these variables at the top of the class, these will control everything about the player character. I then created an Update method to call everytime the game refreshes and there is where my mind started to throb.

Matrix rotmatrix = Matrix.CreateRotationZ(MathHelper.ToRadians(angle)); //create a matrix to calculate which direction object should be moving

I had never come across this Matrix type before, but after reading some tutorials (an awesome site I have found very helpful is http://www.riemers.net) I came to the conclusion that it converts, in my case a Float value (an angle) to a Vector2 x,y values. Please correct me if I'm wrong.
I found a very good function, MathHelper.ToRadians() from what I can gather converts an angular value to radians, very good for me as, at the moment I'm rusty on radians!
dIrection = Vector2.Transform(up, rotmatrix);
This assigns the direction found in the rotmatrix and orientates relative to the value in up, and finally to visually rotate the sprite I simply assigned "angle" to the rotation argument in the call to draw the sprite.
On running the game, to my surprise I got what I wanted!

Does this explanation sound right, or am I misunderstanding something along the way? Please let me know what you think and if you have any suggestions. I'm all ears!