Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

2D Camera movement with SpriteBatch rendering

$
0
0

@kooksta wrote:

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);
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles