How to do one-way collision?

I would like to know how can I do one way collision. It's the collision common in mario's games and many platforms. I try to do an code but I cannot post because it's part of a paid game. Basically I have two rectangles like this:

struct Rect {
int x, y; // positions in the world
int w, h; // width and height
};

The rect A moves and the rect B is fixed with the world. Someone has a idea of how to do this?

• Just to check if i have the question right, you are asking how to check for a collision between an actor and a static object once and not check collisions between the static object and the actor? Feb 25, 2016 at 13:40
• @user2645227 I think samleo is asking about the effect in platformers, where the character can jump through a platform from the bottom, but will collide and land on it when falling downward. Samleo, adding more descriptions or images to your question will help avoid ambiguity for future readers. As game developers we tend to lean pretty heavily on shared jargon, which can lead to a lot of confusion when we don't all have the same definition in mind... ;) Feb 25, 2016 at 13:55
• Requisite reading: higherorderfun.com/blog/2012/05/20/… Feb 25, 2016 at 23:31
• So the problem is (simplified) if(Actor.bottom > obstacle.top){check collision} else{do nothing}? Feb 26, 2016 at 6:53
• Kinda, instead of checking the position; check the direction of movement: If the actor is moving up, do not check collision. If the actor is moving down: check for collision. Aug 6, 2016 at 14:22

Assuming that you have functional 2-way collision, all you need to do is check that the bottom of Rect A is above the surface of Rect B, and that the y velocity of Rect A is negative/down. Roughly:

void OnCollide(a, b)
{
if(a.y - a.h * .5 > b.y + .5 * b.h && a.yvel < 0)
{
// do collide
}
else
{
// pass through
}
}
• Almost there, I tried to do this before and a I had a bad effect when the player's rect go from horizontal and so the collision is processed subtle. Any other idea? Feb 25, 2016 at 13:34
• What do you mean the collision is processed subtle? Can you rephrase?
– Nox
Feb 25, 2016 at 14:11
• For example, when the player goes to up side suddenly (was this word hehehe), like a teleport to up side. Feb 25, 2016 at 17:04

You can use a Platform Effector on the game object that has the collider:

This provides a One-Way collision detection for platforms.

http://docs.unity3d.com/Manual/class-PlatformEffector2D.html

• I think OP never mentioned that he is working with Unity :| Feb 25, 2016 at 13:25
• I do this all the time. I'll leave it here in case Unity users are looking, lol. Feb 25, 2016 at 13:26
• Well, I'm not using unity, sorry for this. I would like to know how to do this "in nature", just code from scratch, can you help me? Feb 25, 2016 at 13:26

A solution may consist in deactivating all fixed colliders above your character and activating the ones under your character

• It's this way that I use. But, I hold a vector of platforms without collision while player jumping or even when vel.y < 0. The problem is when the player go trough platforms it came from the sides, like in your image. I don't know when is the right moment of de/active the collision. Thank you for the help. I will try another approach. Feb 25, 2016 at 17:02
• This method doesn't scale well to multiplayer games, where one player could be above a platform and another player below it, or situations where AI characters need to be able to jump through platforms from below the way the player does. Feb 25, 2016 at 19:59
• This sort of deactivation only works properly in all cases if you're tracking each pair of contacts. e.g., once you collide, record the pair of collided objects. When they are no longer colliding, remove the record. This record can then store flags like "ignored" which disables the resolution code for that pair of objects, which you set from the collision callback if the player hits a platform from below, or if the player presses the "drop down" button, or whatever. Feb 25, 2016 at 23:34

I solved the problem. I just deactivate the collision detection in axis X and hold the detection in axis Y. Thank you people.