Sign in to follow this  
farmdve

2D collision resolution code not behaving properly.

Recommended Posts

farmdve    194
Long story short, I followed the SDL tutorial on LazyFoo here [url="http://lazyfoo.net/SDL_tutorials/lesson29/index.php"]http://lazyfoo.net/S...son29/index.php[/url] and wrote the game from scratch in C(since the examples were C++), but borrowed some of the functions.

What is happening is that the collision code is working, but not entirely. If I were to hit a wall, and continue to walk towards it(and being unable to pass through it obviously) and at the same time try to move in a different direction(but not the opposite to the wall) I can't move.

This is my if/else statement

[CODE]
if(touches_wall(player.box))
{
if(player.box.x < player.prev_x)
{
player.box.x += GetVelocity(player, 0);
}
else if(player.box.x > player.prev_x)
{
player.box.x -= GetVelocity(player, 0);
}

if(player.box.y > player.prev_y)
{
player.box.y -= GetVelocity(player, 1);
}
else if(player.box.y < player.prev_y)
{
player.box.y += GetVelocity(player, 1);
}
}
[/CODE]

player.box.x and player.box.y are the current positions of the player on the X and Y axis respectively. player.prev_x and player.prev_y are the previous positions.
GetVelocity pretty much returns some static hardcoded values of the velocity for each axis(depending on the second argument being 0 or 1).

My previous collision handling code was this huge one [url="http://pastebin.com/mrFkLTLH"]http://pastebin.com/mrFkLTLH[/url] but while it worked the way I wanted it allowed under some conditions the object to pass through the wall near the corners.

Here's a picture of a more detailed explanation; [url="http://i.imgur.com/Rn7MY.png"]http://i.imgur.com/Rn7MY.png[/url]
Where the arrows are, I basically make the object go left, let it collide and then either go up or down, while still going left, but it will stay still and won't move either way.
With my previous large code that I just pasted on pastebin, where the small blue lines are on the screenshot, that's where the object passed through the wall on some conditions.

So I basically need a solution on how to fix this. I've been working on that since last night, to the point where I wrote the same code over and over again, expecting a different result.

EDIT:It is fairly obvious to me, that since my collision code is inside a single if-statement, and I am indeed colliding with a wall, while trying to move up or down, either of the sub-statements always execute thus preventing me to move. No idea how to fix it though, Edited by farmdve

Share this post


Link to post
Share on other sites
Khatharr    8812
If you collide with a wall then rather than undoing that frame's motion you should probably place yourself at a position 'against' the wall.

Rather than stepping back by your velocity, detect the wall edge that you're colliding with and set yourself at the most extreme valid position:

[source lang="ruby"]#ruby flavored pseudocode
def playerMovingLeft(player, amount)
player.box.x -= amount
collidedObject = stage.testCollide(player.box)
if collidedObject != nil #hit the left wall (since we're moving left)
player.box.x = collidedObject.rightEdge #so set position to a valid one
end
end
[/source]

As for your code, another way of writing exactly what you have there would be:

[source lang="cpp"]if(touches_wall(player.box)) { //don't know which direction(s) caused collision.
player.box.x = player.prev_x;
player.box.y = player.prev_y;
}[/source]

Which makes it easier to see the problem. You say in your post that you already know the problem though. I don't understand what you're confused about then. If a single 'if' won't work then don't use a single 'if'.

Get some rest. I think it will come to you when you're freshened up a bit. If you code yourself into a corner taking a break can often simplify difficult problems. Edited by Khatharr

Share this post


Link to post
Share on other sites
BeerNutts    4400
You could possibly fix this by moving the player in the x direction 1st, and checking for collisions, then move him in the y direction and check collisions then. The issue is you are doing 2 collision checks per update, but, depending on the game, this probably won't be an problem.

Share this post


Link to post
Share on other sites
Trienco    2555
It will end up being either a loop or recursive calls, since you'll have to do the same steps until all your movement is "used up".

-check for _first_ collision (your path might have you collide with more than one object)
-move up to point of collision (or slightly less, because floats can be a pain)
-resolve collision (determine new direction and remaining movement)
-back to step one (unless your collision response is to stop dead, in which case the logic still applies with 0 as remaining movement)

Share this post


Link to post
Share on other sites
farmdve    194
Thank you guys, after moving the code of each axis after the code where I increase each axis upon movement, I was able to overcome ALL the limitations I had previously. I could not have done it without you guys. Thank you all. Edited by farmdve

Share this post


Link to post
Share on other sites

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