1
\$\begingroup\$

I've a condition in my game where, I want my camera to follow the Player(its a body from Box2d) and in some not.
This is the start screen of my game:
Game begining Screen

Now, when i press a button(space) the player get an LinearVelocity in x,y direction and starts flying..flying

As soon as it reaches the end. The box is now out of the camera.

To fix this I made my camera centered the box.player centered

This is where I screwed. I dont want my ground to come in the center it should always needs to be at bottom.. like in Picture 1 and the player should also needs to be at the left-bottom at the start of the game. And when player flies away ... the x and y coordinates should be moved as my player moves ... i have certain height where player can fly so camera should be able to move in y-axis also.. and the game is endless running game so x- cordinates also keeps comming in.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

When centering the camera using lookAt you need to clamp the y value to be the max of the body's y and the y of the ground plus half the viewport height (possibly adjusting for the height of the ground).

Something like this might make sense;

float x = body.getWorldCenter().x;
float y = Math.max(body.getWorldCenter().y, ground.getWorldCenter().y + camera.viewportHeight / 2.0f);

camera.lookAt(x, y, 0.0f);

(Note that I might have gottent some the api calls wrong, it's from memory)

\$\endgroup\$
1
\$\begingroup\$

I had this class in previous project

public class ExtendedCamera extends OrthographicCamera {
    public Player player;

    public ExtendedCamera(Player player) {
        super(Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT);
        this.player = player;
    }

    public void followPlayer() {
        if (player.body.getPosition().x - position.x > Constants.CAMERA_FOLLOW_LINE_X) {
            position.x = player.body.getPosition().x
                    - Constants.CAMERA_FOLLOW_LINE_X;
            update();
        } else if (player.body.getPosition().x - position.x < -Constants.CAMERA_FOLLOW_LINE_X) {
            position.x = player.body.getPosition().x
                    + Constants.CAMERA_FOLLOW_LINE_X;
            update();
        }
        if (player.body.getPosition().y - position.y > Constants.CAMERA_FOLLOW_LINE_Y) {
            position.y = player.body.getPosition().y
                    - Constants.CAMERA_FOLLOW_LINE_Y;
            update();
        } else if (player.body.getPosition().y - position.y < -Constants.CAMERA_FOLLOW_LINE_Y) {
            position.y = player.body.getPosition().y
                    + Constants.CAMERA_FOLLOW_LINE_Y;
            update();
        }
    }
}

It's a bit verbose but it works. Just call camera.followPlayer() after updating player's position. Basically player has free movement zone and if he moves out this zone, camera follows him. Play with CAMERA_FOLLOW_LINE to achieve best result.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .