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?