# Collision detection direction

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

## Recommended Posts

I'll make this quick. Made 2D game is made up by rectangles representing platforms, the player, enemies etc.. Untill now I've passed the moving direction of the player to the collision detection function. But now I've redesgined it all so that the player can move in more than one direction.

My question is, how do I find out if the player is on top, under or hitted the sides of a platform? Before this was easy as I past the moving direction to the function so if the player moved right and hitted a wall he was left to the platform. Buw how to find out now?

Hope you understand, I'm in a hurry writing this [smile]

##### Share on other sites
Quote:
 Original post by simpler... Untill now I've passed the moving direction of the player to the collision detection function. But now I've redesgined it all so that the player can move in more than one direction.

How does this mean that you cannot pass the current direction anymore? Perhaps you mean that the player was able to move up, down, left, or right formerly, and now can also move diagonally; but also then there are ways to encode the direction an pass it to the collision routine.

Quote:
 Original post by simplerMy question is, how do I find out if the player is on top, under or hitted the sides of a platform?

a) Still pass the movement direction (see above).

b) Pass the former position, so that the difference of the former to the current position can be used to compute the movement direction.

c) If the movement step (i.e. speed by frame time) is low enough compared to the dimensions on the objects, you may look at the penetration depths (in each dimension) to estimate the direction. Although this method doesn't require passing of additional parameters, it it usually less handy due to the problems (accuracy and such) that may occur.

##### Share on other sites
Okey great suggestions!
Wondering one thing more:

My goal now is to only call the Level::collisionDetection() function once in my Player::update() function. To do this I first set a temporary position to the new position. Which is calculated by the player pressing "A" or "D", player falling and player jumping. I then send this temporary position to Level::collisionDetection() and if there was no collision I set the players position to the temporary.

1.) First set a temporary position to the new position
2.) Check collisions at the new pos
3.) No collisions -> move
Collisions -> dont move

Is that how it should be done?

##### Share on other sites
Quote:
 Original post by simplerMy goal now is to only call the Level::collisionDetection() function once in my Player::update() function. To do this I first set a temporary position to the new position. Which is calculated by the player pressing "A" or "D", player falling and player jumping. I then send this temporary position to Level::collisionDetection() and if there was no collision I set the players position to the temporary.1.) First set a temporary position to the new position2.) Check collisions at the new pos3.) No collisions -> move Collisions -> dont moveIs that how it should be done?
It is a possibility, yes.

But step 3.) is perhaps a bit too simple, because it implements a "all or nothing" strategy. This is correct only for discretized movement, e.g. where the avatar can move from one tile to the next in exactly one not interruptible movement. Otherwise the situation may occur where the avatar is requested e.g. to move 2 steps towards an obstacle that is 1 step away, but the collision system cancels the movement as an entirety rather than allowing the 1st step.

That said, a collision system usually doesn't cancel movement but actually does a correction opposite to the original movement direction. This may reach a correction of the entire latest movement, of course.

##### Share on other sites
Gosh I'm totally confused and stuck at the collision detection. I dunno how I should do. Could someone please explain with pseudo code how it should look?

##### Share on other sites
Quote:
 Original post by simplerGosh I'm totally confused and stuck at the collision detection. I dunno how I should do.
Hopefully not due to my comments ...

Quote:
 Original post by simplerCould someone please explain with pseudo code how it should look?
Please tell us detailed what you want to achieve. Discussing in pseudo code w/o knowledge of the concrete situation, how movement is given, how collision volumes are given, etc, is problematic.

##### Share on other sites
For simple collision detection, just do something like...

(RECT and IntersectRect do exist under the standard windows library, but every other variable/function are just made up for this example.)
 RECT plr = {x, y, x+img_size, y+img_size}; // Make an invisible rectangle that surrounds the player. RECT obj = {object.x, object.y, object.x+img_size, object.y+img_size}; // Make one for "object" RECT rect = {0}; // This is just for the IntersectRect function, don't worry about this one. if ( UserPressed(right_key) ) // If pressed right, { x += speed; // Move right. if ( IntersectRect(&rect, &plr, &obj) ) // If the player's rect intersected with the object's rect, { // Collision! x -= speed; // Move left at the same speed, giving the effect that you haven't moved in the first place. } } 

This is of course very basic rect-rect collision detection using the standard windows library's IntersectRect. It is not always accurate but should work.

Or if basic collision isn't what you want, be more specific.

1. 1
2. 2
Rutin
25
3. 3
4. 4
5. 5

• 9
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632940
• Total Posts
3009328
• ### Who's Online (See full list)

There are no registered users currently online

×