XNA Short: using sine & cosine to move an object forward

In this short XNA tutorial I’m going to show you how to move an object forward. Unlike Unity, XNA doesn’t have a lot of fancy libraries and you’ll sometimes have to use your math skills in order to get the job done. I suck at Math, really, just cause I never really knew why it would be useful. When the (horrifying) thought of me needing math to do game development hit me, I started to get more interested in everything math. One of these was Trigonometry, and I’ll be doing a short tutorial on that here. If SOHCAHTOA and hypotenuse drive you nuts you might want to skim the trigonometric functions page on Wikipedia first.

define: XNA Short – Tutorial were I won’t go over all the code, only the important parts, and I assume you already know something about XNA;

In this XNA tutorial, we want to move an object forward by a  certain distance (let’s say 10).  We’re also able to rotate the object, so it can move 10 in ANY direction. In that case, we don’t know the X and Y coordinates of the movement vector, so we need to use some of the elementary trigonometry functions. These functions allow us to get size or angle information from triangles. Huh? A triangle? We don’t have any triangles don’t we? Well, if you graph down the data we have, it turns out that we do.

Since we need to move in both the X and Y direction to reach this unknown point, when you draw it, you’ll see a perfect right-angled triangle. The hypotenuse (longest side) is  10, the adjacent (x) and opposite (y) side are unknown. The unknown sides are used in the following Trig formulas

Sine(angle) = opposite / hypotenuse or Y / 10

Cosine(angle) = adjacent/ hypotenuse or X / 10

Now, with a little bit of derivation we can get the values of X and Y:

Y = 10.sin(angle) and X = 10.cos(angle)

The X and Y values are the only things needed in order to move an object, so we can start coding now. Create a new XNA project, load in a texture, make it rotate with the arrow keys (overload of Draw accepts a rotation). To handle moving, I created a method Move which accepts a distance, you can either make this positive or negative to move your object forward or backwards. Below is the method’s implementation, and you’ll see it uses the same above formulas to get the X and Y value. Math.Sin & Math.Cos return a double so we need to convert it to a float in order to get the right result.

 private void Move(float moveDistance)
 Vector2 movement = new Vector2();
 movement.X = moveDistance * (float)Math.Cos(rotation);
 movement.Y = moveDistance * (float)Math.Sin(rotation);

 destinationRect.X += (int)movement.X;
 destinationRect.Y += (int)movement.Y;

The above code should be pretty easy now. You could’ve just copied the short snipped of code, but I hope this post has learned you what everything exactly means. This concludes the first XNA Short tutorial. Have fun coding!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s