Hi all,
I have been having a hell of a time getting one-sided platforms to work effectively in my game and I think I have finally found a workable solution, so I thought I would share my experiences.
First of all, I haven't really hammered this solution from a testing stand-point, but so far so good. Also, the following solutions does NOT apply to angled platforms. That is, it will only work for horizontal platforms and I'm assuming the gravity vector is vertical (ie not some weird angular gravity).
So.. the approach I have settled on is comparison of the contact normal vector with the gravity vector. If the normal vector of the contact is in the same direction as gravity, then I assume the contacting body is hitting the platform on the correct side. So in my contact listener for the platform I have something like this:
The first thing to be aware of is the order of the contacts. In this simple case I am treating ALL static/dynamic bodies as equal. If you have different rules for different bodies, you would probably just inspect your UserData contents in the body to see if it qualifies for contact disabling. Hence the comparison of the normal to 1 vs.-1.
Pretty simple, but there's a problem.
As the body contacts with the "platform", the normals of the contact will change. This was the gotcha for me, and can be seen in the following screenshots from the testbed:

contact1.gif [ 3.67 KiB | Viewed 170 times ]

contact2.gif [ 3.82 KiB | Viewed 170 times ]

contact3.gif [ 3.77 KiB | Viewed 170 times ]
The first two contacts are as I expect, the normal is "horizontal" and doesn't meet my contact criteria and is thus disabled. However as the body continues to contact, the normals change as you can see in the final image. In this case, the normals become "correct" and the above contact filtering approach will kick in and enable the contact. At this point the body will "pop up" onto the top of the platform. Not what we want.
So the method I used to solve this was to retain a record of the bodies that have made contact with the platform and assume the first observation of the contacting body is to be used throughout all subsequent calls to PreSolve.
In my case, I created a simple data structure to keep track of my contacts. The final result looks something like this (pseudo code):
I'm sure this code can be improved, but I just created it from pulling out irrelevant bits from my real code so it may be a bit crufty. The idea is to keep a record of contact with a body based on the initial contact, and re-use that record for all subsequent contacts. Only when contact ends do we remove the record:
The result (for me) is that the dynamic body only contacts when hitting the platform from above, and if it hits from below I don't get that annoying "pop" as the contact normal switches.
Hope this helps someone out there...
😉
I have been having a hell of a time getting one-sided platforms to work effectively in my game and I think I have finally found a workable solution, so I thought I would share my experiences.
First of all, I haven't really hammered this solution from a testing stand-point, but so far so good. Also, the following solutions does NOT apply to angled platforms. That is, it will only work for horizontal platforms and I'm assuming the gravity vector is vertical (ie not some weird angular gravity).
So.. the approach I have settled on is comparison of the contact normal vector with the gravity vector. If the normal vector of the contact is in the same direction as gravity, then I assume the contacting body is hitting the platform on the correct side. So in my contact listener for the platform I have something like this:
Code:
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
b2Fixture* shapeA = contact->GetFixtureA();
b2Fixture* shapeB = contact->GetFixtureB();
b2Body* bodyA = shapeA->GetBody();
b2Body* bodyB = shapeB->GetBody();
if(bodyA->GetType() != b2_staticBody && bodyB->GetType() != b2_staticBody) {
b2WorldManifold worldManifold;
contact->GetWorldManifold(&worldManifold);
b2Vec2 normal = worldManifold.normal;
if(bodyA->GetType() == b2_dynamicBody) {
if(normal.y != -1) {
contact->SetEnabled(false);
}
}
else {
if(normal.y != 1) {
contact->SetEnabled(false);
}
}
}
}
{
b2Fixture* shapeA = contact->GetFixtureA();
b2Fixture* shapeB = contact->GetFixtureB();
b2Body* bodyA = shapeA->GetBody();
b2Body* bodyB = shapeB->GetBody();
if(bodyA->GetType() != b2_staticBody && bodyB->GetType() != b2_staticBody) {
b2WorldManifold worldManifold;
contact->GetWorldManifold(&worldManifold);
b2Vec2 normal = worldManifold.normal;
if(bodyA->GetType() == b2_dynamicBody) {
if(normal.y != -1) {
contact->SetEnabled(false);
}
}
else {
if(normal.y != 1) {
contact->SetEnabled(false);
}
}
}
}
The first thing to be aware of is the order of the contacts. In this simple case I am treating ALL static/dynamic bodies as equal. If you have different rules for different bodies, you would probably just inspect your UserData contents in the body to see if it qualifies for contact disabling. Hence the comparison of the normal to 1 vs.-1.
Pretty simple, but there's a problem.
As the body contacts with the "platform", the normals of the contact will change. This was the gotcha for me, and can be seen in the following screenshots from the testbed:
Attachment:
contact1.gif [ 3.67 KiB | Viewed 170 times ]
Attachment:
contact2.gif [ 3.82 KiB | Viewed 170 times ]
Attachment:
contact3.gif [ 3.77 KiB | Viewed 170 times ]
The first two contacts are as I expect, the normal is "horizontal" and doesn't meet my contact criteria and is thus disabled. However as the body continues to contact, the normals change as you can see in the final image. In this case, the normals become "correct" and the above contact filtering approach will kick in and enable the contact. At this point the body will "pop up" onto the top of the platform. Not what we want.
So the method I used to solve this was to retain a record of the bodies that have made contact with the platform and assume the first observation of the contacting body is to be used throughout all subsequent calls to PreSolve.
In my case, I created a simple data structure to keep track of my contacts. The final result looks something like this (pseudo code):
Code:
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
b2Fixture* shapeA = contact->GetFixtureA();
b2Fixture* shapeB = contact->GetFixtureB();
b2Body* bodyA = shapeA->GetBody();
b2Body* bodyB = shapeB->GetBody();
if(bodyA->GetType() != b2_staticBody && bodyB->GetType() != b2_staticBody) {
// Look in our local data structure for an instance of this body
// ContactRecord is just an imaginary struct used for this example
ContactRecord* record = NULL;
// contactHistory is an imaginary data structure to hold contact records
if(bodyA->GetType() == b2_dynamicBody) {
record = this->contactHistory->find(bodyA);
}
else {
record = this->contactHistory->find(bodyB);
}
if(record == NULL) {
// We haven't seen this body before, create a record.
record = new ContactRecord();
record->bodyId = <some identifier in your body UserData>
record->enabled = true;
// now check the contact
b2WorldManifold worldManifold;
contact->GetWorldManifold(&worldManifold);
b2Vec2 normal = worldManifold.normal;
if(bodyA->GetType() == b2_dynamicBody) {
if(normal.y != -1) {
record->enabled = false;
}
}
else {
if(normal.y != 1) {
record->enabled = false;
}
}
this->contactHistory->add(record);
}
contact->SetEnabled(record->enabled);
}
}
{
b2Fixture* shapeA = contact->GetFixtureA();
b2Fixture* shapeB = contact->GetFixtureB();
b2Body* bodyA = shapeA->GetBody();
b2Body* bodyB = shapeB->GetBody();
if(bodyA->GetType() != b2_staticBody && bodyB->GetType() != b2_staticBody) {
// Look in our local data structure for an instance of this body
// ContactRecord is just an imaginary struct used for this example
ContactRecord* record = NULL;
// contactHistory is an imaginary data structure to hold contact records
if(bodyA->GetType() == b2_dynamicBody) {
record = this->contactHistory->find(bodyA);
}
else {
record = this->contactHistory->find(bodyB);
}
if(record == NULL) {
// We haven't seen this body before, create a record.
record = new ContactRecord();
record->bodyId = <some identifier in your body UserData>
record->enabled = true;
// now check the contact
b2WorldManifold worldManifold;
contact->GetWorldManifold(&worldManifold);
b2Vec2 normal = worldManifold.normal;
if(bodyA->GetType() == b2_dynamicBody) {
if(normal.y != -1) {
record->enabled = false;
}
}
else {
if(normal.y != 1) {
record->enabled = false;
}
}
this->contactHistory->add(record);
}
contact->SetEnabled(record->enabled);
}
}
I'm sure this code can be improved, but I just created it from pulling out irrelevant bits from my real code so it may be a bit crufty. The idea is to keep a record of contact with a body based on the initial contact, and re-use that record for all subsequent contacts. Only when contact ends do we remove the record:
Code:
void EndContact(b2Contact* contact)
{
b2Fixture* shapeA = contact->GetFixtureA();
b2Fixture* shapeB = contact->GetFixtureB();
b2Body* bodyA = shapeA->GetBody();
b2Body* bodyB = shapeB->GetBody();
if(bodyA->GetType() == b2_dynamicBody) {
this->contactHistory->remove(bodyA);
}
else if (bodyB->GetType() == b2_dynamicBody) {
this->contactHistory->remove(bodyB);
}
}
{
b2Fixture* shapeA = contact->GetFixtureA();
b2Fixture* shapeB = contact->GetFixtureB();
b2Body* bodyA = shapeA->GetBody();
b2Body* bodyB = shapeB->GetBody();
if(bodyA->GetType() == b2_dynamicBody) {
this->contactHistory->remove(bodyA);
}
else if (bodyB->GetType() == b2_dynamicBody) {
this->contactHistory->remove(bodyB);
}
}
The result (for me) is that the dynamic body only contacts when hitting the platform from above, and if it hits from below I don't get that annoying "pop" as the contact normal switches.
Hope this helps someone out there...
😉