# 2d rectangle colision, figuring out what comes from where.

This topic is 3615 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am currently designing a platformer. The arch enemy of my existence (next to onions), collision detection has once again come to attack me. My method can detect and act on collisions from the left, right, and bottom, correctly. My problem is differentiating collisions from the top from the rest. Note: My current method evaluates the following if an overlap is detected. Projected values are where the player will be should we move him. Note: It is also worth mention that I am using SDL, so my coords are the top left of the given object, greater means further down and less than means further up than. Anyways, my method: If we will hit the left side //If our projected right X is greater the the objects X AND //If our projected bottom Y is greater than the objects top AND //If our projected Y is less than the objects bottom AND //If the player is heading right If we will hit the right side //If our projected X is greater the the objects left X AND //If our projected Y bottom is greater than the objects top Y //If our projected Y is less than the objects bototm //If our player is heading left. If we will hit the bottom //If projected Y is less than objects Y bottom. //If we are jumping These work, I can run into any of them and it does what it should. My problem now is finding out if it is falling on the top. Whenever I check for it, other conditions match and it executes that one. What conditions are unique to it hitting the the top? I have been wrestling with this for about one week and three rewrites.

##### Share on other sites
I think I'd use the positions from the previous frame somewhere in the calculations. If in the previous frame the player was above the box, and after updating the positions the player is overlapping the box, then the player must have collided with the top of the box.

##### Share on other sites
I will try that, thanks.

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 10
• 27
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633404
• Total Posts
3011676
• ### Who's Online (See full list)

There are no registered users currently online

×