Sloping Single Sided Platforms

There is an example in the testbed demonstrating single sided platforms using a very basic case of a square platform. I am trying to take this one step further and apply it to sloping platforms.

Since the character in the game is the shape of a circle and our platforms can be many different shapes I came with the idea of adding an "Angle" parameter to the level editor to calculate which side the character is coming from.

It looks a bit like this inside PreSolve

Code:
b2WorldManifold worldManifold;
contact->GetWorldManifold(&worldManifold);
   
b2Vec2 point1 = worldManifold.points[0];
b2Vec2 point2 = worldManifold.points[1];
   
float radiansLo = DEGREES_TO_RADIANS(platform->Angle - 180); // left side of platform
float radiansHi = DEGREES_TO_RADIANS(platform->Angle); // right side of platform
float radians = radiansBetweenPoints(actorPos, point1); // radians between player position and point1
   
if (radians >= radiansLo && -radians <= radiansHi) // are we coming from below the platform?
   contact->SetEnabled(false); // yes so turn off collision


This actually works very well for the most part apart from the fact the character will "jitter" as it's pushed out from the centre of the platform when it reaches it's "top side". I was curious if people had some ideas on how to solve this particular problem.

(I'll attach a picture of the level editor with the Angle parameter to help visualize the problem)
The alternate solution is to inspect the contact normal vectors in the manifold.

Eg (from current manual)
Code:
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
  b2WorldManifold worldManifold;
  contact->GetWorldManifold(&worldManifold);
  if (worldManifold.normal.y < -0.5f)
  {
    contact->SetEnabled(false);
  }
}


You can determine which side of the platform your actor is on by inspecting the normal vector projecting from the contact point.

Be aware however, I have recently submitted in issue with the current trunk version which I suspect is a bug (although not yet confirmed):

http://code.google.com/p/box2d/issues/detail?id=124
That's an interesting solution snowcrash that seems to work just as well as my method. Infact I think your method should be in the testbed example.

Unfortunately it still presents the same issues I had originally and that is the "jittering" of the player when passing through a platform. Any ideas on how to solve/reduce this problem?
I'm not 100% cure I understand what you mean. Are you saying that your object is pushed away from the platform BEFORE it has exited? Either way, the alternate/additional solution would be to inspect the velocity vector of the object in motion as it relates to the velocity vector of the platform (if the platform is moving). (getLinearVelocity)

If, relative to one another, they are moving away from each other then you can assume a collision is impossible. Hence if your object passes through the non-colliding side of the platform, when it reaches the colliding side it will still not collide because it's motion is "away" from the platform. Hence it should just pass cleanly through.

This should work for most cases, however there may be situations where your object IS moving towards the platform at the time it passes through the colliding side. If this is the case you can also check the velocity vector of your object against the expected collision vector. So.. let's say you only want to allow perfectly perpendicular collisions (in reality, it would likely be a 180 degree range). If the velocity vector of the object is not in the same range as the normal vector of the collision, then you assume the collision is ignored. In simpler terms, if the collision should only fire if the object moves "downward" onto the platform, and the object is moving "upward", then ignore the collision.

Make sense?
Yes you get what I meant and thanks for the explaination of a solution. If you could give a code example that would be terrific, if not thanks for your time anyway!
I'll see if I can create a demo for the testbed. It won't be for several hours though (I'm at work at the mmt!). Stay tuned...
snowcrash wrote:
I'll see if I can create a demo for the testbed. It won't be for several hours though (I'm at work at the mmt!). Stay tuned...


Great! Thanks!
OK.. so I've whipped up a sample app for the test bed.

I had a bit of a brain freeze when it came to working out the angles, but I think I got it.

Basically the idea is that I check both the angle of the contact, and the angle of the velocity against the platform. If the contact angle is within the "positive hemisphere" (ie above) of the angle of the platform then we are considered above. Similarly if the velocity angle is "above" , we are considered moving "away" from the platform.

Anyway.. sample is attached.

P.S.

I noticed the map editor tool in your previous post. Is this something you created? And if so, is it something you'd be willing to share? It looks pretty sweet.

Cheers,

Jason.
Hmmm.. the demo I attached has a few issues actually.

I think the problem is in the calculation of the angles as I described. Anyway.. hopefully it's enough to get you started 🙂
Hey Snowcrash,

Are you sure the issue you filed is actually a bug? I've been thinking about this, and I think I realize how creation order can affect the normals received in the contact callbacks, but I don't think it's a bug. According to the manual (section 9.1), the convention for the direction of the normal is that it points from fixtureA to fixtureB. As a result, creation order *could* affect which fixture is "fixtureA" and which fixture is "fixtureB", thus affecting the direction of the normal. It seems like it is up to the user to check to see which body fixtureA/fixtureB belongs to, and reverse the normal if necessary. This definitely falls in line with what I've experienced using Box2D, so I think a simple check in your listener might fix the issue.

Or that might not be your issue at all, but I thought I'd post just in case 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=4871