rphello101
rphello101

Reputation: 1771

When using a camera to follow a sprite, should I be using a bound camera (Andengine)?

I know I can use mCamera.setChaseEntity(mSprite) to set the camera to follow my sprite. But in doing my research, I saw suggestions to use a bound camera. What's the difference/benefit?

For the record, I have some development experience with Andengine and PhysicsBox2D, but I've only ever worked within the bounds of the screen. Essentially, what I am trying to do is get an object to start on the left hand side of the screen. Once it passes through the horizontal center (it is not vertically aligned), I need the camera to start to follow it as it moves towards the right through the rest of the world.

In my first attempt at this, I used a handler to check to see if the sprite was at the horizontal center (give or take a 5% error). I then set mCamera.setChaseEntity(mSprite). Since the object was not centered vertically, the camera obviously jumped to center the object. It followed it in an exceptionally jittery fashion (research leads me to believe the camera can't handle the speed of the physics engine and to use a timer to slow it down).

That's when I came upon the Bound Camera example that Nicholas put out. It seemed to be exactly what I wanted, but upon implementing it, I just got a very stretched image of my world and the camera did not follow the sprite beyond the bounds of the screen. This is what I used (ignoring the center-camera when passing horizontal center):

public EngineOptions onCreateEngineOptions() {
        // Initialize camera
        mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2);
        mBoundChaseCamera.setCenter(CAMERA_WIDTH/2, CAMERA_HEIGHT/2);

        // Create engine options
        EngineOptions options = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH*2, CAMERA_HEIGHT), mCamera);

        return options;
    } 

.....

@Override
public void onPopulateScene(Scene pScene,
        OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {

    // Create player sprite
    sPlayer = new Sprite(0, 0, playerTR,getVertexBufferObjectManager());
    sPlayer.setPosition(20, CAMERA_HEIGHT/2);
    mBoundChaseCamera.setBoundsEnabled(true);
    mBoundChaseCamera.setChaseEntity(sPlayer);
    final FixtureDef PLAYER_FIX = PhysicsFactory.createFixtureDef(1.0f,.5f, .2f); // *(density, elasticity, friction)
    body = PhysicsFactory.createBoxBody(physicsWorld, sPlayer,BodyType.DynamicBody, PLAYER_FIX);
    physicsWorld.registerPhysicsConnector(new PhysicsConnector(sPlayer,body, true, false));

    Sprite sLaunch = new Sprite((CAMERA_WIDTH-80),10, launchTR, getVertexBufferObjectManager()){
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            body.applyLinearImpulse(20, 30, body.getWorldCenter().x, body.getWorldCenter().y);
            return true;
        }
    };

    mScene.attachChild(sPlayer);
    mScene.registerTouchArea(sLaunch);
    mScene.attachChild(sLaunch);
    mScene.setTouchAreaBindingOnActionDownEnabled(true);

    // Callback
    pOnPopulateSceneCallback.onPopulateSceneFinished();
}

I'm not looking for someone to write my code or even debug it necessarily. If someone could just point me in the right direction or perhaps recommend a best practice for implementing what I'm trying to do.

Upvotes: 1

Views: 1141

Answers (1)

erahal
erahal

Reputation: 433

The error with your code is that it is missing the setBound(...) method which is used when setting camera bounds enabled in order to limit the X,Y movement of the camera. Without it, the camera will keep the chased entity in the middle of the screen.

Example: Suppose you have a game where the player climbs from bottom to the top, in portrait mode. So to look pro, the player must start from the bottom of your device screen and move upwards. So in this case the camera chases the player but at the beginning we need to set the lower bounds of the camera to be just below the player, so when it is actually following the player, at the beginning the player is not centered vertically, instead it appears on the bottom.

What I suggest, is to use the SmoothCamera ( It extends the bounds camera, but allows you to set the speed of the camera) and you then set the Bounds as needed. Remember the "bounds" effect appears only when the player reaches in the scene the bounds set for the camera

Code Example ( not for the previous climb thing):

    @Override
    public EngineOptions onCreateEngineOptions() {

    this.mCamera = new SmoothCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, 1750, 1750, 0.1f);

    EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
            CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);

    mCamera.setBoundsEnabled(true);
    mCamera.setBounds(0 , 0, MAX_CAMERA_X , MAX_CAMERA_Y);
    return engineOptions;

}

Upvotes: 3

Related Questions