Search code examples
c#matrixxnavelocityperspectivecamera

Camera Jitters Matrix Camera


I am using a translation matrix to move the screen but when the player collides with an object the player will jitter as if it wants to be in 2 places at once. It looks like velocity wants to keep going down while the block pushes it up, how would I go by fixing this?

Video: Here

Camera class:

class Camera
{
    public Vector2 Position;
    Viewport viewPort;
    public Vector2 cameraBounds;

    public float wasGround;

    public Matrix Transform()
    {
        var translationMatrix = Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0));

        return translationMatrix;
    }

    public Player thing(Player player)
    {
        cameraBounds.X = player.Position.X - Game1.offset.X;
        if (cameraBounds.X > 0)
            Position.X = player.Position.X - Game1.offset.X;
        else
            Position.X = 0;

        //Problem      
        cameraBounds.Y = player.Position.Y - Game1.offset.Y;
        if (cameraBounds.Y > 0)
        {
            Position.Y = player.Position.Y - Game1.offset.Y;

            if (player.goingUp == false && (wasGround != player.ground))
                Position.Y = player.ground - Game1.offset.Y;

            wasGround = player.ground;
        }
        else
            Position.Y = 0;


        return player;
    }

    public Camera(Viewport viewport)
    {
        viewPort = viewport;
    }
}

I tried to fix the problem by adding in player goingUp and ground if statements but that did not help.


Solution

  • I solved it. It is about sequence of operations. Just move method camera.thing() as shown below:

                // TODO: Add your update logic here
            HandleInput(Keyboard.GetState());
            player.Update(gameTime);
            // delete from here
            Time += (float)gameTime.ElapsedGameTime.TotalSeconds;
    
            foreach (Block b in Blocks)
            {
                player = b.BlockCollision(player);
            }
            // place here
            camera.thing(player);
    

    Explanation: You have to set camera position after all collisions have done.