How can I track a falling ball with a camera?

Posted by Jason on Game Development See other posts from Game Development or by Jason
Published on 2011-09-17T09:46:13Z Indexed on 2012/12/10 5:20 UTC
Read the original article Hit count: 157

Filed under:
|

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?

© Game Development or respective owner

Related posts about camera

Related posts about libgdx