I've been using AndEngine for a year now and feel it's time to move on to some physics. I've followed the AndEngine Physics Example to start a Roll-a-Ball game where a ball is rolled around the screen using the accelerometer to guide it into a hole. I got my ball rolling OK but can't make it drop into the hole. I've Googled and tried everything but the best I can get is the ball collides with the hole and either bounces off it or rolls over it.
Here's some code.
This is my ball.
ball = new AnimatedSprite(pX, pY,this.mBallFaceTextureRegion,this.getVertexBufferObjectManager());
bodyBall = PhysicsFactory.createCircleBody(this.mPhysicsWorld,ball,BodyType.DynamicBody, FIXTURE_DEF);
bodyBall.setUserData("Ball");
ball.animate(200);
this.mScene.attachChild(ball);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ball, bodyBall, true, true));
Thia is my hole.
hole = new Sprite(pX, pY, this.hHoleFaceTextureRegion, this.getVertexBufferObjectManager());
bodyHole = PhysicsFactory.createCircleBody(this.mPhysicsWorld, hole, BodyType.StaticBody, FIXTURE_DEF);
bodyHole.setUserData("Hole"); // ID for hole
this.mScene.attachChild(hole);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(hole, bodyHole, false, false));
This is my ContactListener inside Scene onCreateScene() with some of code I've tried.
this.mPhysicsWorld.setContactListener(new ContactListener() {
@Override
public void beginContact(final Contact pContact) {
final Body BodyA = pContact.getFixtureA().getBody();
final Body BodyB = pContact.getFixtureB().getBody();
if(BodyA.getUserData() == "Ball" && BodyB.getUserData() == "Hole"){
ball.setPosition(hole);
// BodyA.setType(BodyType.StaticBody); // bodyBall
//mPhysicsWorld.destroyBody(BodyB); // bodyHole
}else if(BodyA.getUserData() == "Hole" && BodyB.getUserData() == "Ball"){
ball.setPosition(hole);
// BodyB.setType(BodyType.StaticBody); // bodyBall
// hole.dispose(); // bodyHole
}
}
Could someone please explain how to swap the hole for the ball or some other way to simulate the ball dropping into the hole. Thanks