View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# 2D collision resolution code not behaving properly.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1farmdve  Members

Posted 12 December 2012 - 04:25 PM

Long story short, I followed the SDL tutorial on LazyFoo here http://lazyfoo.net/S...son29/index.php 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

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);
}
}


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 http://pastebin.com/mrFkLTLH 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; http://i.imgur.com/Rn7MY.png
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, 12 December 2012 - 05:17 PM.

### #2Khatharr  Members

Posted 12 December 2012 - 05:58 PM

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 pseudocodedef 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 endend[/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, 12 December 2012 - 06:07 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #3BeerNutts  Members

Posted 12 December 2012 - 11:03 PM

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.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #4Trienco  Members

Posted 12 December 2012 - 11:15 PM

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)
f@dzhttp://festini.device-zero.de

### #5farmdve  Members

Posted 13 December 2012 - 06:30 AM

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, 13 December 2012 - 06:30 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.