XNA Game – Rotation – Multi platform anyone? – Part 15

After listening to a recent episode of Hanselminutes where he interviewed the draw a stick man guys, I was made aware of Mono Game. Mono game is an open source implementation of the XNA API on every platform including iOS and Android. This is awesome news as it means that with minimal code changes it should be possible to get my game running on every platform. Just goes to show how the open source community is really thriving.

I have reworked the solution quite a bit to turn it into a mono game project. You will need to visit the mono game site to download the mono game platform to be able to run the latest version of the code. The game still works on Windows 7 as is. Before I implement the code to get the game running on iOS and Android, I need to make a few changes to the project to make it cope with running on different platforms.

The first thing I needed to do was take into account the fact that the game will run at different speeds on different platforms. Luckily the XNA framework provides an easy way to do this as it passes an instance of the GameTime class into the update method. The GameTime class gives you that time that has elapsed between the last call to your update method.

To do animations currently I am moving the object a set amount for each call. Now obviously when I port to other platforms (or even run on a different Windows 7 machine) this code won’t work as if the method is called faster it will speed up the animation and vice versa. For example the code to animate A left rotation is something like:

square.Angle += GameConstants.Animation.ANGLE_INCREASE_AMOUNT;

Now to fix this we need to pass the GameTime into the animate method. We can now use a speed to determine how much the animation (and in this case the angle) should move. Speed = distance/time so distance = speed*time. So to calculate the distance to move all we need to do is multiply the time elapsed between update calls by our constant speed. So the example code becomes:

square.Angle = (float)(square.Angle - (GameConstants.Animation.ANGLE_INCREASE_SPEED * gameTime.ElapsedGameTime.TotalMilliseconds));

As you can see we are now using a speed to update the angle. I have updated all of the animations to take speed into account. As per usual a TDD style was used and all of the tests have been updated.

If you want to see the latest version of the code please get the latest from the github and switch to branch part15. You can find more details of how to do that on the rotation git page on this blog.


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