# Finding where collision happend

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

## Recommended Posts

Hello everyone! So, I have a problem thats been bugging me for 2 days now. I just cant seem to solve it. And I believe I will have to waste another 2 days if you dont help me. So, lets say I have a player and a block. Now, I know how to find out if the player collided with the block. I just dont know on which side. I have to know if the player collided on the upper side, left side, right side or down side. But I just cant figure out how. Recenlty, I have had a function called InRect(x, y, rect) that checked if point x,y is in the specified rectangle. Lets say the player has 4 points. Like this:
1____2
|    |
|    |
3____4


Point 1 is upper-left, point 2 is upper-right, etc. I checked if the player fell on the block like this: pseudo-code:
// 1 specifys x,y of the players upper-left etc.
if ( (InRect(3, rect) || InRect(4, rect)) && !(InRect(1, rect) || InRect(2, rect)))
{
return COLLIDED_DOWN_SIDE;
}


I need to check that 1 and 2 arent colliding because 1 would be true if the player collided from the left and 2 if he collided from the right... Now this all seems well and fine but it doesent work. I beg you to help me! Is there an error in my code? Or is my method just stupid? Please, tell me, suggest something! End my pain! I would be very grateful! You can write your own techniques how you would do it... Thank you! Vanneto

##### Share on other sites
Well... it depends how you check for collision, but a simply generic approach would be to see which quadrant the players centre was in at the point of collision. By quadrant I mean is he above or below the first diagonal... is he above or below the second diagonal of the block he is colliding with? These two questions will tell you which quadrant he is in. Above both, he hit the top... below both he hit the bottom... the other two cases are left and right (which is which depends which diagonal corresponds to which test, but as long as you consistently choose diagonals, this should be consistent).

The information could be implicit in your collision test too. How do you look for collisions? another option is to look a the velocity of the player on impact... if he was moving mostly up, it's an impact from below... mostly down, impact from above etc. you could also specifically intersect the players velocity vector with the edges to do this exactly (the nearest intersection is the true point of collision.

There are other methods too... you just need to think logically about exactly what you know and what you need to determine.
Hope this helps,

Dan

##### Share on other sites
Hi,

It's all a simple check, really:

(pseudo)

if (player.left.x < block.right.x) collision=fromRight;
if (player.right.x < block.left.x) collision=fromLeft;

etc...

/Robert

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

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011524
• ### Who's Online (See full list)

There are no registered users currently online

×