Simple thruster like behaviour when rotating sprite
Posted
by
ensamgud
on Game Development
See other posts from Game Development
or by ensamgud
Published on 2012-04-01T20:45:19Z
Indexed on
2012/04/01
23:40 UTC
Read the original article
Hit count: 250
I'm prototyping some 2D game concepts with XNA and have added some basic keyboard inputs to control a triangle sprite.
When I press key up the sprite accelerates in it's current facing direction, when I release the key it brakes down.
For rotation, when I press left/right keys I rotate the sprite.
Currently the sprite immedately changes direction when I rotate it. What I want is for it to keep moving in the same direction when I rotate, until I hit key up, adding thrust in whatever direction the sprite is pointing at.
This would simulate thrusters on a classic space shooter like Asteroids.
I'm adding an image to describe the behaviour I'm after and some code samples of how I'm doing things at the moment.
This is my player struct, holding information of the sprite.
public struct PlayerData
{
public Vector2 Position; // where to draw the sprite
public Vector2 Direction; // travel direction of sprite
public float Angle; // rotation of sprite
public float Velocity;
public float Acceleration;
public float Decelleration;
public float RotationAcceleration;
public float RotationDecceleration;
public float TopSpeed;
public float Scale;
}
This is how I'm currently handling thrusting / braking (when pressing/releasing key up) (simplified, removed some bounds checking etc):
player.Velocity += player.Acceleration * 0.1f;
player.Velocity -= player.Acceleration * 0.1f;
And when I rotate the sprite left and right:
player.Angle -= player.RotationAcceleration * 0.1f;
player.Angle += player.RotationAcceleration * 0.1f;
This runs in the update loop, keeps the direction updated and updates the position:
Vector2 up = new Vector2(0f, -1f);
Matrix rotMatrix = Matrix.CreateRotationZ(player.Angle);
player.Direction = Vector2.Transform(up, rotMatrix);
player.Direction *= player.Velocity;
player.Position += player.Direction;
I am following along various beginner tutorials and haven't found any describing this, but I have tried some on my own without success. Do I need to change my velocity and acceleration fields to Vectors instead of floats to accomplish this type of movement? I realise my Angle
and the Direction
vector is currently tied together and I need to disconnect these somehow to be able to rotate freely without changing the direction of the movement, but I can't quite figure out how to do this while keeping the acceleration/decceleration functional.
Would appreciate an explanation rather than pure code samples.
Thanks,
© Game Development or respective owner