Ich bin Überprüfung mit .collidesWith
wenn zwei Sprites mit verschiedenen Körpertypen zu kollidieren.AndEngine/Box2D/Java - Kollisionserkennung zwischen statischem und dynamischen Körper
mit dem Code unter Kollisionen mit dynamischen und statischen Körper nicht erkannt werden. Ich möchte auch die Körper nach der Kollision löschen.
Aktuelle 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();
}
}
}
}
};
Dies ist die Plattform:
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);
}
}
Und das ist die bewegliche Kugel:
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);
Wie ist es möglich, eine Kollision zwischen statisch zu erkennen und dynamischer Körper und nach der Kollision sollten Körper und Sprite entfernt werden?
Verwenden Sie ein ContactListener und überprüfen Sie dort auf Kollisionen Ihrer Körper. – mrkernelpanic
Ich habe bereits mit ContactListener versucht, aber es nicht erkennt Kollisionen zwischen statischem und dynamischen Bodys. – ninjaxelite
können Sie Ihren Code anzeigen? – mrkernelpanic