Page 1 of 1

XNA – Coded Object Rotation (2D) How to make rotate objects using code

#1 Core   User is offline

  • using System.Linq;
  • member icon

Reputation: 783
  • View blog
  • Posts: 5,101
  • Joined: 08-December 08

Post icon  Posted 31 January 2009 - 11:46 PM

Rotating objects is a pretty obvious task when it comes to games. There are situations, when you need to create a rotating object that will rotate automatically (without the user controlling the rotation). This tutorial will show how to create rotating objects.

Prerequisites? First of all I would recommend you downloading the source code attached to this tutorial to start with a project that already has some basic game elements. XNA Game Studio 3.0 is required, so is Microsoft Visual C# 2008 Express Edition or Microsoft Visual Studio 2008. Your operating systems must be Microsoft Windows XP (or later).
Let’s start.

Step 1:
First of all, either create a new XNA 3.0 Windows Game project or download the solution that is attached to this tutorial.

Posted Image

Step 2:
Now, let’s add an actual game object sprite, that will be rotated. I attached a sample arrow image (object.jpg) to this tutorial, so you can use that one, or you can draw your own. I draw an arrow just because you will clearly see how it rotates. If you are creating a new project, in the Solution Explorer, right click the Content node, select Add -> New Folder and name the folder Textures. Then, right click on the newly created folder and select Add -> Existing Item. Make sure that the file format filter is set to Content Pipeline Filed and navigate to the object image. If you are using the source project attached to this tutorial, just add the object sprite to the existing Textures folder following the same steps, but without creating a new folder.

Step 3:
Now, let’s get to code. First of all, we will need to define the new game object. To make our object operations easier, let’s create a class, that will actually create new game objects. To do this, right click on your project name in the Solution Explorer windows, and select Add -> Class:

Posted Image

Name the class GameObject.cs.

But the newly created class does not contain the needed namespaces declared. To fix this, change the declared namespaces in the class with the following:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

Now, let’s start working on the future game objects. The new class will actually construct the objects, so we will need to declare all object properties in this class. Include the following code in your class:

		// The properties that will define the object.
		public Texture2D ObjectTexture;
		public Vector2 ObjectPosition;
		public float ObjectRotation;
		public Vector2 ObjectVelocity;

		// The object constructor.
		public GameObject(Texture2D MainTexture)
			ObjectRotation = 0.0f;
			ObjectPosition = Vector2.Zero;
			ObjectTexture = MainTexture;
			ObjectVelocity = Vector2.Zero;

This code basically defines all the properties and a constructor that are needed to build and rotate the game object. One interesting moment about this class is that you can use it to create event user-controlled objects. It is an universal class to create game elements.

Step 4:
Now, let’s create the game object. In the Game1.cs file, right after the main class declaration, add the following code:

		// The rotating game object.
		GameObject arrow;

This will create a new game object based on the class we created in the previous step.

Step 5:
In the LoadContent method, add the following code:

			// Set the texture for the game object.
			arrow = new GameObject(Content.Load<Texture2D>("Textures\\object"));
			// Set the position for the game object (center of the window).
			arrow.ObjectPosition = new Vector2(GraphicsDevice.Viewport.Width/2,GraphicsDevice.Viewport.Height /2);

This will set the object sprite and position. As you see, the Viewport is considered the main game screen. By creating an instance of Vector2 class and diving the Viewport Height and Width by 2 we obtain the center point of the game window, therefore the object will be placed in the center.

Step 6:
In the Update method you now have to add the following code:

			// Rotate the object.
			arrow.ObjectRotation += 0.01f;

Changing the float rotation value will obviously rotate the object. The Update method updates the game screen constantly, so you can put here any piece of code that needs to be updated all the time while the game runs.
As you see, the rotation value is incremented by 0.1 (which is a float, so we add the f suffix). The bigger this value is, the faster the object will rotate.

Step 7:
Now we only need to draw the final object on the screen. So, in the Draw method, add the following code:

// Start building the sprite.
			// Draw the rotating object.
			spriteBatch.Draw(arrow.ObjectTexture, arrow.ObjectPosition, null, Color.White, arrow.ObjectRotation, arrow.ObjectCenter, 1.0f, SpriteEffects.None, 0);
			// End building the sprite.

If you already have the spriteBatch.Begin() and spriteBatch.End(), just add the following code between them:

			spriteBatch.Draw(arrow.ObjectTexture, arrow.ObjectPosition, null, Color.White, arrow.ObjectRotation, arrow.ObjectCenter, 1.0f, SpriteEffects.None, 0);

Now, as we are done with all declarations and initializations, we can run the project. Go ahead and build the solution. The result should resemble the image below (but the image in the center is moving):

Posted Image

Now you understand how to make objects rotate in your XNA game.

- Dennis Delimarsky (Core)

Attached image(s)

  • Attached Image

Attached File(s)

Is This A Good Question/Topic? 0
  • +

Replies To: XNA – Coded Object Rotation (2D)

#2 Guest_TJLDaily311*


Posted 27 January 2011 - 09:37 AM

Great Topic/Post! Kept simple and interesting. Thanks. :bananaman:
Was This Post Helpful? 0

#3 Guest_ep*


Posted 24 February 2011 - 06:19 AM

Hi, I used your code, it was very helpful for getting an object to rotate.
But I am getting an error in the draw method, with "arrow.ObjectCenter" It will not take that, I dont think it is defined in your tutorial. Could you help please,
Thank you.
Was This Post Helpful? 0

Page 1 of 1