0
\$\begingroup\$

Sorry if this sounds ambiguous. But I am writing a traditional top down game. And normally the player would be centered in the middle while moving the orthographic camera. However, I would like this to be dynamic. Therefore, use player.setPosition(x,y) near screen boundaries but center on player if its far from screen boundary.

This is some sample code:

public void centerCamOnPlayer() {
         camera.position.set(player.getX() + (player.sprite.getOriginX()),
                             player.getY() + (player.sprite.getOriginY()),
                             0);
}

public void update() {
     boolean shouldCenterOnChar = ???;
     if (shouldCenterOnChar) {
         centerCamOnPlayer();
     } else {
         player.setPosition(x,y);
     }
}

Therefore render will call update(). However how do I determine the shouldCenterOnChar? I tried using frustum.pointInFrustum but its only when the sprite moves completely out of screen. I'm sure there is a better way to switch dynamically. Any one has an idea? Tks!

\$\endgroup\$
0
\$\begingroup\$

It's probably simpler to set boundaries on the camera set:

camera.position.set(clamp(player.getX() + (player.sprite.getOriginX()), 
                          minScreenX, maxScreenX),
                    clamp(player.getY() + (player.sprite.getOriginY()), 
                          minScreenY, maxScreenY),
                    0);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.