Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have been trying to get my camera to follow a falling ball but with no success.

here is the code

        float cameraY = (FrustumHeight / 2)+((ball.getPosition().y) /2) - (FrustumHeight /2);
        if (cameraY <  FrustumHeight/2 )
            cameraY =  FrustumHeight/2;
        camera.position.set(0f,cameraY, 0f);"test",camera.position.toString());

        batch.draw(backgroundRegion, camera.position.x - FrustumWidth / 2,
 -cameraY - (FrustumHeight/2) , 320, 480);
        batch.draw(ballTexture, (camera.position.x - FrustumWidth / 2) + ball.getPosition().x,-cameraY + ball.getPosition().y - (FrustumHeight/2)  ,
                32, 32);

I'm sure I am doing this completely wrong - what is the correct way to do this?

share|improve this question
up vote 5 down vote accepted

The following part of code is why your camera did not follow along with the falling ball:

 cameraY + ball.getPosition().y - (FrustumHeight/2)

(In the last line) Since your cameraY is already at the Y-ball position, you are basically setting this to a constant factor, making your Ball stationary, yet your Camera moving. Most likely the source of your error.

Hint: you shouldn't be adding and then subtracting the same amount in the first line.

Perhaps, a short description of what is actually happening (how the Camera behaves, etc) would be interesting.

share|improve this answer
Yea already removed the first line duplicate addition and subtraction and made some small changes. Now it seems to be working :) Thanks – Jason Sep 17 '11 at 10:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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