I'm using an outter collision body around our Actors to detect when they are touching ground/ceil/left/right. It is just a shape slightly larger than the Actor. It is set to be a sensor (so I can't use PreSolve) so I am using BeginContact to detect a collision. But when the player (circle) hits an NPC (square) the point returned from b2WorldManifold is the centre of the player's circle. How can I get the actual point of collision?
Using an outter collision body
General DiscussionSensors contacts don't compute contact points. Look at b2Contact, I think there is a function to compute the contact manifold manually.
Erin Catto wrote:
Sensors contacts don't compute contact points. Look at b2Contact, I think there is a function to compute the contact manifold manually.
All I can see is b2Contact::Create() but it's protected.
In 2.1.x we have:
Code:
virtual void b2Contact::Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) = 0;
Thanks Erin I seem to have it working now 🙂 I will post the code here for others. Does this look okay to you?
Code:
void ContactListener::BeginContact(b2Contact* contact)
{
b2Fixture* fixtureA = contact->GetFixtureA();
b2Fixture* fixtureB = contact->GetFixtureB();
b2Body* bodyA = fixtureA->GetBody();
b2Body* bodyB = fixtureB->GetBody();
b2Shape* shapeA = fixtureA->GetShape();
b2Shape* shapeB = fixtureB->GetShape();
Actor* actorA = (Actor*) bodyA->GetUserData();
Actor* actorB = (Actor*) bodyB->GetUserData();
Actor* sensorActor = NULL;
Actor* otherActor = NULL;
if(actorA == NULL || actorB == NULL)
return;
if(actorA->SensorFixture == fixtureA)
{
sensorActor = actorA;
otherActor = actorB;
}
if(actorB->SensorFixture == fixtureB)
{
sensorActor = actorB;
otherActor = actorA;
}
if(sensorActor != NULL)
{
b2Manifold manifold;
const b2Transform xfA = bodyA->GetTransform();
const b2Transform xfB = bodyB->GetTransform();
contact->Evaluate(&manifold, xfA, xfB);
b2WorldManifold worldManifold;
worldManifold.Initialize(&manifold, xfA, shapeA->m_radius, xfB, shapeB->m_radius);
b2Vec2 point1 = worldManifold.points[0]; // Point of contact
}
}
{
b2Fixture* fixtureA = contact->GetFixtureA();
b2Fixture* fixtureB = contact->GetFixtureB();
b2Body* bodyA = fixtureA->GetBody();
b2Body* bodyB = fixtureB->GetBody();
b2Shape* shapeA = fixtureA->GetShape();
b2Shape* shapeB = fixtureB->GetShape();
Actor* actorA = (Actor*) bodyA->GetUserData();
Actor* actorB = (Actor*) bodyB->GetUserData();
Actor* sensorActor = NULL;
Actor* otherActor = NULL;
if(actorA == NULL || actorB == NULL)
return;
if(actorA->SensorFixture == fixtureA)
{
sensorActor = actorA;
otherActor = actorB;
}
if(actorB->SensorFixture == fixtureB)
{
sensorActor = actorB;
otherActor = actorA;
}
if(sensorActor != NULL)
{
b2Manifold manifold;
const b2Transform xfA = bodyA->GetTransform();
const b2Transform xfB = bodyB->GetTransform();
contact->Evaluate(&manifold, xfA, xfB);
b2WorldManifold worldManifold;
worldManifold.Initialize(&manifold, xfA, shapeA->m_radius, xfB, shapeB->m_radius);
b2Vec2 point1 = worldManifold.points[0]; // Point of contact
}
}
Close. You should check the point count on the b2Manifold. There is a small chance it could be zero.
Should the outter body be able to detect collision with a static body?
Original thread: https://box2d.org/forum/viewtopic.php?t=5020