4
\$\begingroup\$

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);
        Gdx.app.log("test",camera.position.toString());
        camera.update();
        camera.apply(Gdx.gl10);
        batch.setProjectionMatrix(camera.combined);

        batch.begin();
        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?

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Yea already removed the first line duplicate addition and subtraction and made some small changes. Now it seems to be working :) Thanks \$\endgroup\$
    – Jason
    Commented Sep 17, 2011 at 10:37

You must log in to answer this question.

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