0
votes

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

1

1 Answers

0
votes

SOLVED.

Found the solution at [Removing Body completely1]. You must delete the hole and reset the ball in a separate thread.
I created a new method SwapHoleAndBall() and called it from within beginContact.

Here's the modified code.

    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"){
            SwapHoleAndBall();
            ......

And the swap method.

    public void SwapHoleAndBall() {
    final PhysicsConnector physicsConnector =
            mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(hole);

        mEngine.runOnUpdateThread(new Runnable() 
        {
            @Override
            public void run() 
            {
                if (physicsConnector != null)
                {
                    bodyBall.setTransform(bodyHole.getPosition(), 0);
                    mPhysicsWorld.unregisterPhysicsConnector(physicsConnector);
                    bodyHole.setActive(false);
                    mPhysicsWorld.destroyBody(bodyHole);
                    mScene.detachChild(hole);
                    bodyBall.setType(BodyType.StaticBody);
                }
            }
        });
    }

Hope this will help someone else.