Finding where collision happend

Started by
1 comment, last by Rasmadrak 16 years, 6 months ago
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
Advertisement
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
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
"Game Maker For Life, probably never professional thou." =)

This topic is closed to new replies.

Advertisement