1
votes

Ok so here is the problem, I found a nice working 2D Camera from:

http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/

So now, I have implemented it into my 2D Top-Down Shooter game and it works nicely. When I have the camera position equal that of the player position, it works perfectly; to an extent. Now when I have the camera position = the player position, it jerks around a little bit like this:

http://www.youtube.com/watch?v=mh4Tx9xg324

As you can see the sprite goes and then goes back a little bit. I will give a rough example: If the player position is (100, 100) I move to the right and it goes to (120, 100). Now all the numbers are fine, its the visualization. The visualization seems like this:

(100, 100) -> (130, 100) -> (120, 100)

I have no idea why it is doing this and it bugs me to the point that it is all that I am working on to fix. Now when I have the camera centered on a point (1000, 1000), the player doesn't jerk around as such. So this makes everything point directly at the Camera2D Class.

Anyway, if anyone could help it would be greatly appreciated!

  • Bobby

** EDIT ** Movement Code:

    //Update Movement for user controlled sprites
    //A bit rough around the edges at the moment...
    public void UpdateMovement(Input input) {
        //Get ready to point sprite at mouse location in relation to the center of the screen
        MouseState mouse = Mouse.GetState();
        mouseLoc = new Vector2(mouse.X, mouse.Y);

        direction = new Vector2(512, 300) - mouseLoc;
        angle = (float)((Math.Atan2(-direction.Y, -direction.X)));

        m_Rotation = angle;
        //End angle information

        //reset the changed vector 2 back to zero
        changed = Vector2.Zero;

        //checkCollision(vector2)
        //it gets the estimated new point and if it doesnt hit a wall
        //it sets to the new point.
        if (input.CurrentKeyboardState.IsKeyDown(Keys.A)) {
            changed.X = -m_Speed;
            if (!checkCollision(changed)) {
                m_Position += changed;
            }
        }

        if (input.CurrentKeyboardState.IsKeyDown(Keys.D)) {
            changed.X = m_Speed;
            if (!checkCollision(changed)) {
                m_Position += changed;
            }
        }

        if (input.CurrentKeyboardState.IsKeyDown(Keys.W)) {
            changed.Y = -m_Speed;
            if (!checkCollision(changed)) {
                m_Position += changed;
            }
        }

        if (input.CurrentKeyboardState.IsKeyDown(Keys.S)) {
            changed.Y = m_Speed;
            if (!checkCollision(changed)) {
                m_Position += changed;
            }
        }
    }
1
Can you post your movement code?Neil Knight
@NeilKnight added the movement code like you askedMister

1 Answers

1
votes

Thanks to the help provided at http://xnachat.com/ , i was able to quickly fix the problem.

How:

I passed the camera to the player rather than setting the camera position over and over I just added the changed vector to the camera position.