Page 1 of 1

XNA – Linear Motion How to move objects on a vector (line)

#1 Core  Icon User is offline

  • using System.Linq;
  • member icon

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

Post icon  Posted 31 January 2009 - 11:25 PM

Linear motion is the key element of many games. In this tutorial I will show how to make an object move on a straight line (vector) by using code.

Before starting this tutorial, make sure you have the following products installed:

  • Microsoft Windows XP SP2 (or later)
  • Microsoft Visual C# 2008 Express Edition or Microsoft Visual Studio 2008
  • Microsoft XNA Game Studio 3.0


C# programming experience is not required, but I would recommend learning the basics of C# programming first if you don’t have the foundational C# knowledge.

Now, let’s start.

Step 1:
Create a new XNA Windows Game project. If you don’t know how to accomplish some specific tasks (like creating a new project or add an item), refer to my previous XNA tutorials. Name the new project as you want.

Step 2:
Create a new class and name it GameObject.cs. This will be the class that will allow you to build game objects.

Step 3:
The newly created class does not contain the needed namespaces declared. Let’s change the existing namespace declarations. Replace them with the following set:

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;



As you see, there are required XNA namespaces in this list.

Step 4:
Now, add the following code for the class:

		// This will set the texture for the object.
		public Texture2D ObjectTexture;
		
		// This will set the postition for the object.
		public Vector2 ObjectPosition;

		// This will set the center point for the object.
		public Vector2 ObjectCenter;

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



The code above just sets the properties that will be used to create game objects. The constructor has some default values for the object properties. This is made for the following purpose – sometimes the developer creates an object, but does not necessarily change the object properties. To avoid errors, when a new instance of an object is created, the property that was never changed will have its default value. Of course, you can set other values to be the default ones for the object properties right in this class.

Step 5:
Now, switch to theGame1.cs class. Let’s a create an instance of a game object (based on the GameObject class). Right after the class declaration declare a new GameObject using the following code:

GameObject objectInMotion;



Step 6:
Now, in the Solution Explorer, in the Content section, create a new folder and call it Sprites. This folder will be the storage for the sprites that will be applied to game objects.

Step 7:
Add a sprite (image file) that will represent your object to the Sprites folder. If you don’t have one, you can download the sprite attached to this tutorial (truck.jpg).

Step 8:
Add the following code to the LoadContent method in the Game1.cs class:

			objectInMotion  = new GameObject(Content.Load<Texture2D>("Sprites\\truck"));
			objectInMotion.ObjectPosition = new Vector2(GraphicsDevice.Viewport.Width/2, GraphicsDevice.Viewport.Height/2);



This will initialize the object texture (sprite). If you have set a different name for the sprite, go ahead and change the file name from truck to the name of your file.
The second line sets the position of the object to the center of the viewport (game screen).

Step 9:
In the Draw method of the main file, add the following code:

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



This will start the drawing of the spriteBatch, draw the objectInMotion object with the specified location and color settings and will end the drawing process.

Step 10:
Now, add the following code to the Update method.

objectInMotion.ObjectPosition.X += 1;



As update method updates the screen constantly, this code will also be executed constantly and the object will move on the X axis (horizontal). You can easily modify this code for the object to move faster (the bigger is the increment value, the faster the object moves) or to move on the Y axis (vertically). Incrementing the X value will move the object to the right. Decrementing it will move the object to the left.

Completed
Now, run your project and see the object move on a straight line!

- Dennis Delimarsky (Core)

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: XNA – Linear Motion

#2 vishner  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 19-September 09

Posted 21 September 2009 - 05:14 AM

HELLO!!! This is very vital! I need your help! please read my topic at http://www.dreaminco...ex.ph...3&st=0&

My question regarding your post will be. How will I rescale the sprites size... In this tutorial there is no definite way on how i can change the size or rescale the texture so I can make it more flexible.
I was be able to implement your code to have the image movement controllable via key press. I used this code

[csharp]
if (newState.IsKeyDown(Keys.Space))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.Space))
{
objectInMotion.ObjectPosition.X += 10;
}

}
if (newState.IsKeyDown(Keys.A))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.A))
{
objectInMotion.ObjectPosition.X -= 10;
}

}[/csharp]

HELLO!!! This is very vital! I need your help! please read my topic at http://www.dreaminco...ex.ph...3&st=0&

My question regarding your post will be. How will I rescale the sprites size... In this tutorial there is no definite way on how i can change the size or rescale the texture so I can make it more flexible.
I was be able to implement your code to have the image movement controllable via key press. I used this code

if (newState.IsKeyDown(Keys.Space))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.Space))
{
objectInMotion.ObjectPosition.X += 10;
}

}
if (newState.IsKeyDown(Keys.A))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.A))
{
objectInMotion.ObjectPosition.X -= 10;
}

}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1