Sloping Single Sided Platforms

Hmmm... that's some very good thinking toadkick, you may well be on to something there.

I had initially considered this and discounted it because I was explicitly identifying which fixture was my "platform", and which was the "player"... but now that I have read your post I'm realizing regardless of knowing this, I still need to know which is the contact-or, and which is the contact-ee to determine which way to expect the normal vector to go.

Actually... the more I think about it, the more I think you're right. I will review my test case and close the issue if needed.

Thanks!
Thanks snowcrash will check this out today!

Our player is always the first Box2D fixture created so I believe it should be the same contact each time.
I haven't had much time to play with this but it doesn't seem to work very well in my implementation. The main character keeps falling through the sloped platforms. Not entirely sure why yet.
Yeah.. my implementation was dodgy. I was pretty tired when I did it and my brain was running like an old tractor.

Basically the calcs around the angles of the contact vs. the angle of the platform vs. the angle of the direction of the player.. was all wrong.

I may have another go at it, but in the mean time I've done a quick diagram of what I am suggesting.

So it's a bit hard to follow, but here's the idea:

Attachment:
platform.gif
platform.gif [ 8.21 KiB | Viewed 94 times ]


The green circles and dynamic bodies
The blue area is the platform
The yellow semi-circle is the hemisphere that represents the "contactable" side of the platform.
Blue lines represent the velocity vector of the body
Red lines represent the contact vector(s) of the contact.

As I understand it, you want object 1 to collide, but object 2 and 3 should pass through unscathed. Object 2 has just passed through the under side of the platform.

So the idea is this:

When a body contacts with the platform, you check the angle of the contact vector. If the vector angle is within the contactable "hemisphere" of the platform (the yellow area) you say it is a viable contact. If the contact vector is not within this hemisphere, you assume it's not a valid contact.

To overcome the situation where a "valid" contact is actually contact from a body passing through from underneath (object 2) you need to also check the velocity vector. If the velocity vector is inside the hemisphere, then it is NOT a valid contact because it is moving "away" from the platform.

So consider the diagram:

Object 1 has a contact vector within the angle of the contact area (the angle would be normalized from the origin at 0,0.. this may be confusing in my diagram), and the velocity vector is toward the platform.. contact = true.

Object 2 has contact vectors within the angles of the contact area, BUT its velocity vector is also within this area, therefor contact = false

Object 3 does not have a contact vector in the contactable area. Therefore contact = false.

I hope this makes sense. If I have time I'll see if I can fix up my crufty implementation.

Cheers,

Jason
Thanks again snowcrash, if you get some time to revise your code that would be great. I will go through it sometime and see if I can figure it all out. I'm sure there are many people who are interested in a working solution to this.
A bit of thread resurrection, but this thread is what google gave me and I found it lacking a satisfactory solution. I hope you will be pleased with my contribution.

I wrestled with this problem today and I think I won.
I did this with an edge but you could do it with the top vertices of a box too.

It's very simple really.
1) Get the vector from the platforms first vertex to the characters position.
2) Get the angle of the platform.
3) Rotate the vector from step 1 negative angle from step 2.
4) The Y axis result from step 3 is the characters position relative to the platform. Positive and negative distance just as if the platform had been horizontal.

EDIT: I added some handling at the line segment ends. For this I rotate the line end point to get max x and check if the circle is between the line points before disabling the contact. If the circle is outside the line, only disable if its center is below the line. If you don't handle this case, the circle will fall between two connected lines in a convex corner.

NOTE: I did this with positive gravity because of my development preference. This should only affect the one line near the end.
Also, this doesn't take into account rotated bodies, only sloping shapes.
Code:
b2Vec2 position = m_character->GetBody()->GetPosition();

b2PolygonShape* pshape = dynamic_cast<b2PolygonShape*>(m_platform->GetShape());
b2Vec2 lp1 = pshape->GetVertex(0) + m_platform->GetBody()->GetPosition();
b2Vec2 lp2 = pshape->GetVertex(1) + m_platform->GetBody()->GetPosition();

b2Vec2 diff = position - lp1;

b2Vec2 le = lp2 - lp1;
float angle = -atan2(le.y, le.x);

//Rotate point by angle of line and diff.y is the distance.
b2Vec2 diffr;
diffr.x = cos(angle)*diff.x - sin(angle)*diff.y;
diffr.y = sin(angle)*diff.x + cos(angle)*diff.y;
      
float platform_max_x = cos(angle)*le.x - sin(angle)*le.y;
       
//Detects while circle is above the line segment
if(diffr.x>=0 && diffr.x<platform_max_x && diffr.y + radius > 3.0f * b2_linearSlop)  //NOTE: Positive gravity
{
    contact->SetEnabled(false);
}
       
//Detect at corners
if(-diffr.y < radius/2)   //NOTE: Positive gravity
    contact->SetEnabled(false);

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