XNA Rotate and Move Sprite

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<pre class="prettyprint 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;


namespace AsteroidsGaming
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class dgc_Spaceship : DrawableGameComponent
{

Texture2D shipTexture;
Rectangle spriteRectangle;
SpriteBatch spriteBatch;
//
bool horizisSelected = false;
bool rotateSelected = false;
bool fireisSelected = false;

SpriteFont Count;

//Controls for ship
dgc_TriggerHorizontal triggerHorizantal;



Vector2 spritePosition;
Vector2 spriteOrigin;
Vector2 direction;


float rotation = 0.1f;
Random x, y;


List<Missle> missleCollection = new List<Missle>();
List<Asteriod> asteriodCollection = new List<Asteriod>();


Missle missle;

Asteriod asteriod;

public dgc_Spaceship(Game game)
: base(game)
{
// TODO: Construct any child components here


triggerHorizantal = new dgc_TriggerHorizontal(game);
triggerHorizantal.triggerHorizantal += new EventHandler(triggerHorizantal_Handler);
triggerHorizantal.triggerRotate +=new EventHandler(triggerHorizantal_triggerRotate);
triggerHorizantal.triggerFire +=new EventHandler(triggerHorizantal_triggerFire);
Game.Components.Add(triggerHorizantal);
missle = new Missle(game);
asteriod = new Asteriod(game);

for (int i = 0; i < 2; i++)
{
missleCollection.Add(new Missle(game));
}


for (int i = 0; i < 2; i++)
{
asteriodCollection.Add(new Asteriod(game));
}


}




/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here

base.Initialize();
}

protected override void LoadContent()
{

spriteBatch = new SpriteBatch(GraphicsDevice);
shipTexture = Game.Content.Load<Texture2D>(@"Spritesspaceship");
spritePosition = new Vector2(450,320);
missle.LoadContent();

Count = Game.Content.Load<SpriteFont>(@"SpritesCount");
//
x = new Random();
y = new Random();

foreach (Asteriod display in asteriodCollection)
{
display.LoadContent();

int xVec = x.Next(0, 200), yVec = y.Next(0, 200);
display.position = new Vector2(xVec, yVec);
}





base.LoadContent();
}

/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here

spriteRectangle = new Rectangle((int)spritePosition.X, (int)spritePosition.Y, shipTexture.Width, shipTexture.Height);

spriteOrigin = new Vector2(spriteRectangle.Width / 2, spriteRectangle.Height / 2);

direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));


if (horizisSelected)
{
spritePosition.X-= 0.5f;
spritePosition.Y-= 0.5f;

}


if (rotateSelected)
{
rotation += 0.1f;
// missle.rotateSprite();
spritePosition += direction * 20 * gameTime.ElapsedGameTime.Milliseconds;
}

if (fireisSelected)
{
missle.update();
}



base.Update(gameTime);
}

public override void Draw(GameTime gameTime)
{

spriteBatch.Begin();
// missle.draw(spriteBatch,rotation);
spriteBatch.Draw(shipTexture,spritePosition,null,Color.White,rotation,spriteOrigin,1f,SpriteEffects.None,0);
// spriteBatch.DrawString(Count,asteriodCollection.Count.ToString(),new Vector2(100,100),Color.Red);
foreach (Asteriod display in asteriodCollection)
{
display.Draw(spriteBatch);
}
spriteBatch.End();

base.Draw(gameTime);
}


public void triggerHorizantal_Handler(object sender, EventArgs e)
{
//Do Stuff
horizisSelected = !horizisSelected;

}

public void triggerHorizantal_triggerRotate(object sender, EventArgs e)
{
//Do Stuff
rotateSelected = !rotateSelected;
}

public void triggerHorizantal_triggerFire(object sender, EventArgs e)
{
//Do Stuff
fireisSelected = !fireisSelected;
}




}
}
[/code]
My Problem:
I cant seem to figure this out. I have 2 functional buttons on screen. When clicked, the ship rotates clockwise and the other moves it forward. A little searching around lead me to the formula with the Math.Sin/Cos, but I dont think I implemented it properly.

What I need it to do is move the ship in the direction of the rotation.
The way it works: The user will hit the rotate button and it will rotate ship clockwise. When its in a position the user wants, they hit the rotate button to stop it in that particular direction.
The hit the horizontal button and it will move the ship forward in that direction.

<br/>

View the full article
 

Similar threads

Back
Top