Rotation Based Movement using XNA Game Development Studio
The easiest thing to do is give you the code, so here. Take it.
Alright, that’s the source code for a top-down navigation system.
I’m not going to hold your hand and give you any math lessons. Instead, I will simply inform you that the system uses a rotation, position, velocity, and some fancy math to find the next point to go to over a period of time. Specifically, this means that a new point is calculated anytime the Update method is called in the default Game1.cs file created by the XNA Game Studio 2.0 Game Windows/XBOX 360 Project.
All you really need to do is copy the MovementProperties.cs file to your existing project and add this line of code to your Game.Update method:
MovementProperties.Update(gameTime);
You will need to add a “using” reference to IMACM any files that need it (i.e. Game1.cs). With that, you’re ready to use the MovementProperties class. Simply instantiate a MovementProperties object, like below:
MovementProperties spriteMovement = new MovementProperties();
If you want to set the properties in the instantiation then pass in a Vector2 variable for positioning the sprite, a float variable for rotation, and a float variable for velocity in that order:
MovementProperties spriteMovement = new MovementProperties(new Vector(0f, 0f), 0f, 0f);
You can also change it’s position, rotation, and velocity separately, like below:
spriteMovement.CurrentVector = new Vector(100, 100);
spriteMovement.CurrentRotation = MathHelper.Pi;
spriteMovement.CurrentVector = 5.0f;
When you set the position (CurrentVector) you must use a Vector2 variable, but if you need to set only the horizontal or the vertical position of a MovementProperties object, then you can easily access it with the CurrentX or CurrentY property, respectively:
spriteMovement.CurrentX = 350f;
spriteMovement.CurrentY = 500f;
If you’re wondering about all the fs after the numbers, that’s how you cast an integer number (C# reads numbers as integers by default) to a float number. When working with movement based on a rotation you need floats to keep the numbers pretty. Otherwise, things tend to move jumpily/jerkily. This is also useful if you develop your game for multiple resolutions because it’s more accurate to convert a float placement to a screen than it is to convert an integer placement.
Also, make note that rotation is based on radians. That means that, pi is the same as 180 degrees. You can use the MathHelper class for other basic rotations. MathHelper.PiOver2 is 45 degrees. The rotation is clockwise, so if you have a sprite of an arrow with a default position facing up:
oSpriteMP.CurrentRotation = 0; //Arrow faces up
oSpriteMP.CurrentRotation = MathHelper.PiOver2; //Arrow faces right
oSpriteMP.CurrentRotation = MathHelper.Pi; //Arrow faces down
oSpriteMP.CurrentRotation = MathHelper.Pi + MathHelper.PiOver2; //Arrow faces left
Within the Update handler you can do stuff like change rotation or velocity based on input. Check out the files for a simple example of movement.
Leave a Reply