School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,143 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,743 people online right now. Registration is fast and FREE... Join Now!




XNA – User-Controlled Object Rotation (2D)

 
Reply to this topicStart new topic

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

Core
Group Icon



post 31 Jan, 2009 - 10:29 PM
Post #1


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:

CODE

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:

CODE

// 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:

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:

CODE

  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:

CODE

            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:

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:

CODE

            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:

IPB 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
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Theaegd
***



post 7 Sep, 2009 - 10:07 AM
Post #2
nice tutorial
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 03:34PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month