4

Using AndEngine, Im trying to move a sprite (ball) with the accelerometer which is supposed to bounce of walls and hit other obstacles.

so far I've found the following two methods.

 public void onAccelerometerChanged(final AccelerometerData myAccelerometerData) {
    mySprite.setPosition(mySprite.getX() + myAccelerometerData.getX(), mySprite.getY() + myAccelerometerData.getY());

}

This works very smoothly but in a non physics world only. (i.e will not bounce of walls and other obstacles smoothly).

And now using this method which is in the examples provided by AndEngine, the ball has good physics but does not move as smoothly as i would like to.

 @Override
    public void onAccelerometerChanged(final AccelerometerData pAccelerometerData) {
            final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), pAccelerometerData.getY());
            this.mPhysicsWorld.setGravity(gravity);
            Vector2Pool.recycle(gravity);
    }

I've also tried multiplying pAccelerometerData with certain values such as 2 and 5, yes it appears more responsive but also makes the ball move a lot faster and difficult to play.

To make my question clear, I would like my game to be as smooth as the balls in Labyrinth or Rebound game are for Android. The balls in them are are more responsive to accelerometer. For instance if you want to stop a moving ball suddenly in Labyrinth, you can but with the following code in andEngine example, it keeps moving for a while as if it is moving in a frictionless world.

Is there anyway I can make it more responsive and smooth like that of the Labyrinth game? https://market.android.com/details?id=se.illusionlabs.labyrinth.lite&hl=en

Waqar Ahmed
  • 258
  • 1
  • 5
  • 16

1 Answers1

3

You can (as you tried) multiply the pAccelerometerData, and add a damping term to the ball. So the ball will be more responsive, will stop faster, and will keep a reasonable speed.

The damping is a property of bodys. So it would be something like

body.setLinearDamping(1.5f);
Zoleas
  • 4,869
  • 1
  • 21
  • 33
  • 1
    I gave it a try and yeah, the sprite is more responsive and provides better control. Thanks Zoleas – Waqar Ahmed Jan 26 '12 at 10:53
  • i have also same issue. where should i use this body.setLinearDamping(1.5f); This should be in Non Physics world or in physics world. – Amit Thaper Apr 02 '12 at 12:19
  • 1
    It is a property of a body, so it is in physic world. Apply the damping to the body of your moving object (a ball, a character…). – Zoleas Apr 04 '12 at 09:02