I am writing a GameActivity of a swinging pendulum (in AndEngine).
While applying CollisionHandler, this will produce a series of collision events in the instead of one?? Actually, I’d like it is executed once only when sprites collide.
My case: when the movingBall collides with the mLeftWall, it called 5 times of onCollision(); It was abnormal, I expected only one time.
Should I use ContactListener instead and register with PhysicsWorld? Here’s my Code:
MyGameActivity.java:
/** List of objects to check collision of any animating objects. E.g: walls */
private ArrayList<IShape> mCollidingTargetList = new ArrayList<IShape>();
/** user-defined collision handler */
PendulumCollisionCallback collideActionCallback = new PendulumCollisionCallback();
@Override
public Scene onCreateScene()
{
final Scene scene = super.onCreateScene();
final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
//Create walls
mWallLeft = new Rectangle(0, 0, 2, mCameraHeight, vertexBufferObjectManager);
mWallRight = new Rectangle(mCameraWidth - 2, 0, 2, mCameraHeight, vertexBufferObjectManager);
PhysicsFactory.createBoxBody(mPhyscisWorld, mWallLeft, BodyType.StaticBody, mWallFixtureDef);
PhysicsFactory.createBoxBody(mPhyscisWorld, mWallRight, BodyType.StaticBody, mWallFixtureDef);
scene.attachChild(mWallLeft);
scene.attachChild(mWallRight);
//Create movingBall - the pendulum ball
final AnimatedSprite movingBall = new AnimatedSprite(mCenterX, mCenterY, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager());
final Body ballBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, movingBall, BodyType.DynamicBody, mObjectFixtureDef);
movingBall.setUserData(ballBody); // for getting the body attached with UI
//..... Stuff to create anchorBody and RevoluteJoint with the movingBall
//list of bodies where sprites collide
this.mCollidingTargetList.add(mWallLeft);
this.mCollidingTargetList.add(mWallRight);
//Manage user-defined collision handler: movingBall collides with walls
CollisionHandler pendulumCollideHandler = new CollisionHandler(collideActionCallback, movingBall, mCollidingTargetList);
scene.registerUpdateHandler(pendulumCollideHandler);
return scene;
}
PendulumCollisionCallback.java:
public class PendulumCollisionCallback implements ICollisionCallback
{
public PendulumCollisionCallback()
{ }
/**
* @param animatedShape
* Entity to check collision with other targets
* @param pTargetShape
* Target to check the collision
* @return <code>true</code> to proceed, <code>false</code> to stop further collosion-checks.
*/
@Override
public boolean onCollision(IShape animatedShape, IShape pTargetShape)
{
String pendulPos = String.format("Pendul:x=%f, y=%f", animatedShape.getX(), animatedShape.getY());
Log.d("COLLIDE", pendulPos);
return false;
}
}
I found the issue. This is because of separated
Physics Handlerand Scene’sUpdateHandlerwork sequence.While
PhysicsWorldis still controlling body collision, the body is still moving forward for a few frames => so that the position of UI sprite (attached with this body) is in junction with colliding object. During these frames, theupdateHandlerproduces collision events byCallbackHandler.The solution is a tricky method of adding Boolean flag which is set at first colliding and unset when finish colliding (though this is not nice).
P/S: my frame rate observed form
FPSLoggeris~88,9 FPS.