Page 1 of 1

XNA – User-Controlled Object Rotation (2D) How to allow the user to rotate objects

#1 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Post icon  Posted 31 January 2009 - 11:29 PM

As I showed in one of my tutorials, automatic object rotation is not very complicated. However, sometimes there is a need to allow the user to rotate an object – for example if the object is a space ship or a car. So how to accomplish this?

First of all, make sure that you have XNA Game Studio 3.0 and Microsoft Visual C# 2008 Express Edition or Microsoft Visual Studio 2008 installed.

Step 1:
I assume that you already know how to create XNA Windows Game projects. If not, refer to some of my previous tutorials to find out more on how to accomplish this. If you are new to XNA, also check some of my previous tutorials to understand the basics.

First of all, create a Sprites folder in the Content section in the Solution Explorer window. You will now need to add a sprite to the folder, which will be the actual object that will be rotated. You can download a sample sprite, that is attached to this tutorial (space_ship.jpg).

Step 2:
Now, let’s create a class that will actually build the game objects. In my sample I will use just one game object, but as the class is universal, it can be used to create a wide range of game objects.

Create a new class and call it GameObject.cs.

As you see, the namespaces declared in this class are not quite suitable for game development as they don’t include XNA libraries. Replace the existing set of declared namespaces 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, add the following code to the class:

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

		// The object constructor.
		public GameObject(Texture2D MainTexture)
		{
			ObjectRotation = 0.0f;
			ObjectPosition = Vector2.Zero;
			ObjectTexture = MainTexture;
			ObjectVelocity = Vector2.Zero;
			ObjectCenter = new Vector2(ObjectTexture.Width / 2, ObjectTexture.Height / 2);
		}



This is the constructor, the base structure, for all the game objects in our game. It sets all the properties (like the object sprite, position, center point, velocity and rotation) for the object. I am using the same variables as in the previous tutorials to avoid confusion and to build an understanding on how all the object properties work.

Step 3:
Now, in the Game1.cs (the main game file) source file, we need to actually create the new object based on the object structure declared in the above class. Right after the Game1 class declaration, add the following piece of code:

		GameObject spaceShip;



This will create a new instance of our class that will be the space ship, that will be rotated.

Step 4:
In the LoadContent method in the main file, add the following code right after the spriteBatch initialization:

  spaceShip= new GameObject(Content.Load<Texture2D>("Sprites\\space_ship"));

			spaceShip.ObjectPosition = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);



This will initialize a sprite for the space ship and set the initial position for it on the game screen. Dividing the viewport height and width values by two will actually place the object in the center of the fame screen.

Step 5:
Now, in the Draw method, add the following code right after the code that clears the GraphicDevice:

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



This will draw the space ship, setting its texture, position, rotation, center some additional controlling properties.

Step 6:
Now we have to create the code that will handle the user’s input for object rotation.

In the Update method, add the following code:

KeyboardState keyBoardState = Keyboard.GetState();

			if (keyBoardState.IsKeyDown(Keys.Left))
			{
				spaceShip.ObjectRotation -= 0.1f;
			}
			if (keyBoardState.IsKeyDown(Keys.Right))
			{
				spaceShip.ObjectRotation += 0.1f;
			}



This will actually receive the keyboard input. If the left key is pressed, the object rotates to the left (as you see, the ObjectRotation property is decremented by 0.1). If the right key is pressed, the object will rotate to the right (as the ObjectRotation property is incremented by 0.1).

But this is the code for the PC keyboard. What if you run the game on a Xbox 360 console? Of course this code won’t work as the Xbox 360 console doesn’t support a keyboard device. But XNA offers a set of classes that allow you to handle controller input. If you run your game on a Xbox 360 console, add this code to the Update method instead of the code for the keyboard key handling:

			GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
			cannon.rotation += gamePadState.ThumbSticks.Left.X * 0.1f;



This will handle the input from the controller, specifically if the user uses the left thumb stick:

Posted Image

Completed
Now, try running your application. If you are using a PC, press the right and left keys to check how the object rotates. If you are using an Xbox 360 console, use the left thumb stick to rotate the object.

- Dennis Delimarsky (Core)

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 2
  • +

Replies To: XNA – User-Controlled Object Rotation (2D)

#2 Theaegd  Icon User is offline

  • Hater & Lover

Reputation: -125
  • View blog
  • Posts: 944
  • Joined: 15-August 09

Posted 07 September 2009 - 11:07 AM

nice tutorial
Was This Post Helpful? 0
  • +
  • -

#3 Pipey85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 30-June 09

Posted 22 January 2010 - 03:41 AM

I didn't realise that it was all based on an overloaded spritebatch.draw method, It makes so much more sense now. Thankyou!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1