2
votes

I'm developing 2D Side scroll Android Game, using AndEngine and its BOX2D extension.

I have player body, with 2 sensors for 'feet' and 'head' so I might know exactly which side of the player touched different object etc. Here's image showing how does it work:

enter image description here

It works well for checking if player is currently touching ground with feet, so he can jump for example, etc. Now I'm trying to implement actions executed after contact with monster body.

In my contact sensor, I'm checking

                if (x1.getBody().getUserData().equals("monster") && x2.getUserData().equals("foot"))
                {
                    jump();
                }

                if (x1.getBody().getUserData().equals("monster") && x2.getUserData().equals("player"))
                {
                    GameManager.playSound(lostSound);
                    handleDie();
                }

But every time I jump on the 'head' of the monster (So basically I'm touching it with feet sensor) died action is executed. Because both contacts are noticed by contact listener, it works if I would make feet sensor higher, to protrude more from player body (player body is exact shape of player's sprite texture) so it would look like there wasn't contact between player and monster at all.

Thanks in advance for any tips how to handle it properly.

2

2 Answers

1
votes

The easiest way i think is to make your 'Feet' Sensor a solid fixture. Also make players'body smaller. You can actually make the player of 3 solid fixtures: head, body, feet. The collision will still be handled the right way, but if you touch the monster with feet there is no way to touch him with the body because of the solid fixture

0
votes

Check contact like this on your update method:

for (b2ContactEdge* ce = feet->GetContactList(); ce; ce = ce->next)
{

     const b2Body* bodyA = c->GetFixtureA()->GetBody();
     const b2Body* bodyB = c->GetFixtureB()->GetBody();
     //if one of them is a monster, mark him for being ignored this frame.
}

for (b2ContactEdge* ce = player->GetContactList(); ce; ce = ce->next)
{
     const b2Body* bodyA = c->GetFixtureA()->GetBody();
     const b2Body* bodyB = c->GetFixtureB()->GetBody();
     //if the monster is ignored, jump(), else die() 
}

You probably should also check if the monster is below the player.

This is C++, but you should be able to do something similar in Java.

Hope it helps.