I'm using this to create a player body
private void addPlayer(){
// viral -- add player
final float startX = (CAMERA_WIDTH - this.mPlayerTextureRegion
.getWidth()) / 2;
final float startY = CAMERA_HEIGHT + 100;
this.playerFace = new Sprite(startX, startY, this.mPlayerTextureRegion,
this.getVertexBufferObjectManager());
this.playerBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld,
playerFace, BodyType.DynamicBody, FIXTURE_DEF);
final PhysicsHandler physicsHandler = new PhysicsHandler(playerFace);
playerFace.registerUpdateHandler(physicsHandler);
playerBody.setBullet(true);
playerFaceHalfWidth = playerFace.getWidth() / 2;
playerFaceHalfHeight = playerFace.getHeight() / 2;
this.mScene.attachChild(playerFace);
playerFace.setY(CAMERA_HEIGHT - playerFaceHalfHeight * 2);
}
This is how I'm detecting collision with other sprites:
protected void onManagedUpdate(float pSecondsElapsed) {
if (playerFace.collidesWith(this)) {
// safe of touching border - else game over
if (!isPlayerInSafeZone()) {
Log.w("cd", "Game Over");
isGameInProgress = false;
mScene.setIgnoreUpdate(true);
processLevelEnd();
}
}
};
The issue now is that even though i've added a circle body, the collision is detected with the player when the other sprites touch the corner of the png for player. it is a transparent area and the image has only a circle in it for which the body was created. Why is this happening? What is wrong in the above code?
::Update based on Lucaz's inputs:
This is my contactlistener
private ContactListener createContactListener()
{
ContactListener contactListener = new ContactListener()
{
@Override
public void beginContact(Contact contact)
{
final Fixture x1 = contact.getFixtureA();
final Fixture x2 = contact.getFixtureB();
final String x1id = (String) x1.getBody().getUserData();
final String x2id = (String) x2.getBody().getUserData();
Log.w("cd", "x1 = " + x1id + " --- x2 = " + x2id);
}
@Override
public void endContact(Contact contact)
{
}
@Override
public void preSolve(Contact contact, Manifold oldManifold)
{
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse)
{
}
};
return contactListener;
}
This is how I'm setting the user data for the player and other sprites:
Player:
this.playerFace = new Sprite(startX, startY, this.mPlayerTextureRegion,
this.getVertexBufferObjectManager());
this.playerBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld,
playerFace, BodyType.DynamicBody, FIXTURE_DEF);
playerBody.setBullet(true);
playerBody.setUserData("player");
other sprites (balls):
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face,
BodyType.DynamicBody, FIXTURE_DEF);
body.setLinearVelocity(vector2);
body.setBullet(true);
body.setUserData("ball");
This is how i added the listener to physics world
this.mPhysicsWorld.setContactListener(createContactListener());
The listener is written to print the user data for colliding objects:
but when a sprite collides with player i get null for the player but user data for the other sprite as shown below... how to fix this?
x1 = null --- x2 = ball