0
votes

I am checking with .collidesWith if two sprites with different body types are colliding.

With the code below collisions with dynamic and static body are not detected. I would also like to delete the bodies after collision.

Current Code:

ContactListener cl = new ContactListener() {

            @Override
            public void preSolve(Contact contact, Manifold oldManifold) {
                // TODO Auto-generated method stub

            }

            @Override
            public void postSolve(Contact contact, ContactImpulse impulse) {
                // TODO Auto-generated method stub

            }

            @Override
            public void endContact(Contact contact) {
                final Fixture x1 = contact.getFixtureA();
                final Fixture x2 = contact.getFixtureB();
            }

            @Override
            public void beginContact(Contact contact) {
                final Fixture x1 = contact.getFixtureA();
                final Fixture x2 = contact.getFixtureB();
                if (x1 != null && x1.getBody() != null
                        && x1.getBody().getUserData() != null) {
                    if (x2 != null && x2.getBody() != null
                            && x2.getBody().getUserData() != null) {
                        Log.d("TEST", "x1: "
                                + x1.getBody().getUserData().toString());
                        Log.d("TEST", "x2: "
                                + x2.getBody().getUserData().toString());

                        if (x2.getBody().getUserData().equals("Ball")
                                && x1.getBody().getUserData().equals("Dynamic")) {
                            destroy();
                        } else if (x2.getBody().getUserData().equals("Ball")
                                && x1.getBody().getUserData().equals("Static")) {
//                          ballSP.destroy();
                            // Toast.makeText(
                            // ResourcesManager.getInstance().activity,
                            // "touched static", Toast.LENGTH_LONG).show();
                        } else if (x2.getBody().getUserData().equals("Ball")
                                && x1.getBody().getUserData().equals("Figur")) {
//                          figSP.destroy();
                        }
                    }
                }
            }
        };

This is the platform:

public void createPhysics() {
        if (bodyType != null) {
            FixtureDef FIXTURE = PhysicsFactory.createFixtureDef(0.0f, 0.0f,
                    1.0f);
            platformBody = PhysicsFactory.createBoxBody(physicsWorld, this,
                    bodyType, FIXTURE);
            physcConnector = new PhysicsConnector(this, platformBody, true,
                    true);
            physicsWorld.registerPhysicsConnector(physcConnector);
            physicsWorld.registerPhysicsConnector(new PhysicsConnector(this,
                    platformBody, true, true));
            this.setUserData(platformBody);
            platformBody.setLinearVelocity(physicsWorld.getGravity().x, 0);

            if (bodyType == BodyType.StaticBody) {
                platformBody.setUserData("Static");
                platformBody.setAwake(true);
            } else if (bodyType == BodyType.DynamicBody) {
                platformBody.setUserData("Dynamic");
//              platformBody.setAwake(true);
            }
        }

And this is the moving ball:

FixtureDef FIXTURE = PhysicsFactory.createFixtureDef(0.0f, 0, 1.0f);
        ballBody = PhysicsFactory.createBoxBody(physicsWorld, this,
                BodyType.DynamicBody, FIXTURE);
        physicsConnector = new PhysicsConnector(this, ballBody, true, true);
        physicsWorld.registerPhysicsConnector(physicsConnector);
        Vector2 shoot = new Vector2((6.78125f - 0.625f), (15.4375f - 0.625f));
        // shoot.nor().mul(4);

        scene.attachChild(this);

        // ResourcesManager.getInstance().camera.setChaseEntity(ballSP);

        ballBody.setLinearVelocity(shoot);
        ballBody.setAngularVelocity(0.5f);
        ballBody.setUserData("Ball");
        ballBody.setAwake(true);

How is it possible to detect collision between static and dynamic body and after the collision the bodies and sprite should be removed?

1
Use a ContactListener and check there for collisions of your bodies. - mrkernelpanic
I have already tried with ContactListener but it does not detect collisions between static and dynamic bodys. - ninjaxelite
Can you Show your Code? - mrkernelpanic
I have edited my question - ninjaxelite
And now I need the Code how you create your static/ dynamic bodies. I assume you dont see any Log messages right? - mrkernelpanic

1 Answers

1
votes

OK a few things:

1.Enable the moving of the ball:

PhysicsHandler physicsHandler = new PhysicsHandler(ballBody);
ballBody.registerUpdateHandler(physicsHandler);

2. A Sample ContactHandler from me

 @Override
public void beginContact(Contact contact) {
    final Fixture fixtureA = contact.getFixtureA();
    final Body bodyA = fixtureA.getBody();
    final Object userDataA = bodyA.getUserData();

    final Fixture fixtureB = contact.getFixtureB();
    final Body bodyB = fixtureB.getBody();
    final Object userDataB = bodyB.getUserData();

    GameBody givenGameBodyA = (GameBody) userDataA;
    GameBody givenGameBodyB = (GameBody) userDataB;

    LevelGameBodyType bodyTypeA = givenGameBodyA.getBodyType();
    LevelGameBodyType bodyTypeB = givenGameBodyB.getBodyType();

    if(areBodiesCollided(bodyTypeA, bodyTypeB, LevelGameBodyType.PLAYER, LevelGameBodyType.OBSTACLE)){
        Debug.v(TAG, "Player hit an obstacle!");

  }

private boolean areBodiesCollided(LevelGameBodyType givenGameBodyA, LevelGameBodyType givenGameBodyB, LevelGameBodyType wantedGameBodyA, LevelGameBodyType wantedGameBodyB) {
    return (givenGameBodyA.equals(wantedGameBodyA) || givenGameBodyA.equals(wantedGameBodyB))
            && (givenGameBodyB.equals(wantedGameBodyA) || givenGameBodyB.equals(wantedGameBodyB));

}

}

GameBody is an interface and LevelGameBodyType an Enum which identifies the bodies. So in your body definition set the UserData with the GameBody and Identifier Enum.

If this does not help, try to use isSensor = true in your FixtureDef of each body.

FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0.0f, 0.0f, true);

3. Deleting Bodies:

When you delete your body on the current thread you will run into timing issues, so dont do that!

You really should create a separate Java Class for your Bodies, e.g. MovingBall.java etc..

Inside this class you should have a method like this:

    @Override
public void removeFromPhysicsWorld() {
    isWaitingForDestruction = true;
    GameManager.get().getEngine().runOnUpdateThread(new Runnable() {
        @Override
        public void run() {
            body.setActive(false);
            physicsWorld.destroyBody(body);
        }
    });
}

In your ContactHandlerImpl do something like the following when the bodies were collided:

if(body.isWaitingForDestruction()){
            Debug.v(TAG, "Body waiting for destruction is " + body.getObjectId());
            return;
        }
        Debug.v(TAG, "Player collides with collectable! " + body.getObjectId());
        body.removeFromPhysicsWorld();