Im using libGdx to develop a 2d game, which has the following:

  • The world moves in an upward direction by having a camera follow the player's y position.

  • The user only controls player's x movement, the y is a constant speed.

What i want to do now is let the user control the players x and y movement and let the camera move constantly up without following the player.

What is the best way to do this?

This is what i tried:

in Render()

player.body.setTransform(fingerPosition.x, fingerPosition.y, player.body.getAngle());

box2dCamera.position.y = box2dCamera.position.y + cameraSpeed;

This worked but all my collisions stopped working with other box2d bodies. Do i need to update each body in my game with the recalculated camera some-how? How would I do this?

  • \$\begingroup\$ Why use physics bodies to control something that does not interact with anything physically? \$\endgroup\$ Dec 12 '14 at 18:21
  • \$\begingroup\$ The player does interact with other bodies, as stated in the question, the collisions stopped working when I tried to do this. @ClassicThunder \$\endgroup\$
    – ParampalP
    Dec 13 '14 at 19:17
  • \$\begingroup\$ I'm referring to the camera. \$\endgroup\$ Dec 14 '14 at 2:39

There is no need to update each body when modifying the camera. Bodies' positions stays the same when you move the camera around. batch.setProjectionMatrix(cam.combined); already offsets your batch to the right position. The problem must result of this line :

 player.body.setTransform(fingerPosition.x, fingerPosition.y, player.body.getAngle());

Assuming you are rendering after this line of code and updating your world before, your player will always be positioned to fingerPosition, since the world has not been updated between this and rendering.

To make an object follow your mouse/finger, you should use a joint. Get an eye on the MouseJoint class used in the libgdx box2D testbed. You can find how to implement that here.


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.