Sign in to follow this  

Collision detection direction

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 simpler
My 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by simpler
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?
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 this post


Link to post
Share on other sites
Quote:
Original post by simpler
Gosh 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 simpler
Could 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 this post


Link to post
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.)
[code]
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.
}
}
[/code]

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. :)

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this