# 2D Camera movement with SpriteBatch rendering

I cannot seem to wrap my head around how to correctly move my “Ship” which I have my camera fixed in the center of the screen on forwards and backwards based on the direction it is currently heading.

I managed to figure out handling LEFT and RIGHT movement but don’t see how to apply the correct velocity to move my ship’s position in the correct forward direction. The left and right movement works perfectly based on the “rotation” of the ship but I don’t see how I apply forward velocity to my ship.

I would greatly appreciate anyone’s input on this and I thank you in advance for any help!

Here is my players movement code just apply velocity and bleeding some off over time

``````// Works (A)
if (GameScreen.InputHandler.IsActionPressed("Left"))
{
// Calculate the direction the player is facing
var direction = new Vector2((float)Math.Cos(Rotation), (float)Math.Sin(Rotation));
direction.Normalize();

Velocity -= direction*MoveSpeed;
}

// Works (D)
if (GameScreen.InputHandler.IsActionPressed("Right"))
{
// Calculate the direction the player is facing
var direction = new Vector2((float)Math.Cos(Rotation), (float)Math.Sin(Rotation));
direction.Normalize();

Velocity += direction*MoveSpeed;
}

// Forward .. (W)
if (GameScreen.InputHandler.IsActionPressed("Up"))
{
//Velocity -= new Vector2(0, MoveSpeed);
}

// Down (S)
if (GameScreen.InputHandler.IsActionPressed("Down"))
{
//Velocity += new Vector2(0, MoveSpeed);
}

// Ship Movement
Position += Velocity;
Velocity *= 0.95f;
``````

Here is some basic camera update code

public override void Update(GameTime gameTime)
{
var delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

``````  // Create our SRT transforms
Transform = Matrix.Identity*
Matrix.CreateTranslation(-Position.X, -Position.Y, 0)*
Matrix.CreateRotationZ(Rotation)*
Matrix.CreateTranslation(Origin.X, Origin.Y, 0)*
Matrix.CreateScale(new Vector3(Scale, Scale, Scale));

if (Focus != null)
{
// update our camera position based on the object we are focusing
Position += new Vector3((Focus.Position.X - Position.X)*MoveSpeed*delta,
(Focus.Position.Y - Position.Y)*MoveSpeed*delta, 0);
}

base.Update(gameTime);
}``````

Does not the ship (or background) move up and down when your rotation is diferent from 0 degree?

I figured it out … I just had to read up on some trig to remember how to correctly do this with a simple Rotation Matrix and then Transform the UP vector against that to get the correct direction the ship should be moving towards when you hit up or down (W/S).

if (GameScreen.InputHandler.IsActionPressed(“Up”))
{
// Calculate the direction the player is facing
var up = new Vector2(0, -1);
var rotMatrix = Matrix.CreateRotationZ(Rotation);
var direction = Vector2.Transform(up, rotMatrix);

``````      Velocity += direction*MoveSpeed;
}

if (GameScreen.InputHandler.IsActionPressed("Down"))
{
// Calculate the direction the player is facing
var up = new Vector2(0, 1);
var rotMatrix = Matrix.CreateRotationZ(Rotation);
var direction = Vector2.Transform(up, rotMatrix);

Velocity += direction * MoveSpeed;
}``````