Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.