• Create Account

## Collision in walls - SFML

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.

14 replies to this topic

### #1mypel16000  Members

14
Like
0Likes
Like

Posted 28 January 2013 - 01:18 PM

Hi, I am developing a top-down shooter and I have implemented walls on my map. I want my player to be able to move, but not go through the walls. I tried this method:

Move the player
If there is collision: undo the movement.

Problem is that sometimes my player got stuck, and other times, on touching the wall the player stopped completely (If the player is moving diagonally (up and right for example) and hots a wall on its right, I would like it to still move up like on all other games)

Any ideas on this?

Thank you,

Edit: As my walls are circles, "L" shapes and polygons, I have created a sprite containing the outlines and using pixel perfect collision, but the problem persists. Please, I need a consistent method and some code if possible please.

Edited by mypel16000, 28 January 2013 - 01:20 PM.

### #2Milcho  Members

1177
Like
3Likes
Like

Posted 28 January 2013 - 02:12 PM

You can't just cancel out the last movement of the player when he collides - that is why your player is freezing/getting stuck.

What you need to do in the event of a collision is move the colliding object out of the shape by the shortest distance possible.

For a circle, this means move the colliding object out along the direction of the circle's radius projected towards the collision point.

For a rectangle, this means move the object out by either x or y amount, up/down, whichever distance is shortest.

There's several ways you can do collision, but the simplest I can think of is a mathematical representation of point vs circle and point vs rectangle shape.

In rough pseudo-code (I'm assuming point is a vector (porbably sf::Vector2f) and circle has properties like center (Vector2f) and radius (float))

CollidePointVsCircle( point, circle )
{
float dist = distance(point, circle.center);
if ( dist < circle.radius )
{ // point is inside circle, i.e. colliding
vector2f direction = normalize(point - circle.center); // figure out the shortest direction 'out' of the circle
float amountToMove = radius - dist;  // the minimum distance to get out of the circle
point = point + directoin * amountToMove;  // move the point along the direction by the smallest necessary amount
}
}


For a rectangle, you need to figure out if the point is inside the rectangle, and then which side its closest to, then move it out along that amount.

Rough pseudo code:

CollidePointVsRectangle(point, rect)
{
float distToLeft = point.x - rect.left;
float distToRight = (rect.left + rect.width) - point.x;
float distToTop = point.y - rect.top;
float distToBottom = (rect.top + rect.height) - point.y
if ( distToLeft > 0 && distToRight > 0 && distToTop > 0 && distToBottom > 0 )
{ // point is inside rectangle
// determine which distance of the above is smallest - done by a lengthy if/else combination
// then move the point along the direction depending on which side the point was closest -
// if distToLeft is smallest, move point.x by amount (-distToLeft)
// if distToRight is smallest, move point.x by amount (distToRight)
// if distToTop is smallest, move point.y by amount (-distToTop)
// if distToBottom is smallest, move point.y by amount (distToBottom)
}
}


When I say move the point, you can actually compose your player of multiple points, and collide each point with whatever is nearby (you'll have to figure out a way to make sure you don't do collision with all objects)

This is very simple point-circle and point-rectangle collision. If your player is small enough relative to the objects he's colliding with (i.e. there's no wall with a size smaller than the player's size), this will work relatively well.

Alternatively, there's some more complicated, but also more versatile collision methods described here: http://www.metanetsoftware.com/technique/tutorialA.html - as I mentioned on another similar post, they provide source, which should be fairly easy to adapt to your purpose - i did so for the 2d platformer I'm making right now.

Or you could use a library like Box2D - though since I've never used it, i don't know if it has everything you need.

### #3mypel16000  Members

14
Like
0Likes
Like

Posted 28 January 2013 - 02:28 PM

Hi, thanks for the info, but I find it is poorly explained. As I have many walls in my room, how would I check for collision on all of them. INCLUDING AN L SHAPE

___

|     |

|     |____

|_______|

????

### #4fastcall22  Moderators

9735
Like
0Likes
Like

Posted 28 January 2013 - 02:42 PM

... INCLUDING AN L SHAPE

Hmm? I don't see an L-shaped piece; I see four individual square pieces in an L-arrangement.
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

### #5Milcho  Members

1177
Like
2Likes
Like

Posted 28 January 2013 - 02:50 PM

As fastcall said - you can decompose an L shape piece into four square pieces.

Alternatively, you can decompose it into two rectangles, like so:

 _
| | __
|_||__|


In general, any polygonal shape consisting of only right angles can be decomposed into a number of rectangles.

With circle, you can also approximate more complex shapes.

Hope this clarifies the L shaped collision. If there's any other specific part of my previous post you don't find clear, let me know.

### #6mypel16000  Members

14
Like
0Likes
Like

Posted 28 January 2013 - 04:09 PM

How would I go about checking all my walls at the same time??

### #7BeerNutts  Members

4287
Like
2Likes
Like

Posted 28 January 2013 - 04:15 PM

I would suggest moving your player in the x-direction 1st, then checking for collision.  If it collided, move it back to where it was (by just adding the -velocity).  Then move the player in the y-direction and check for collisions.  If it collided, move it back to where it was. This would keep the player always just off the wall.

This is a simple method of doing it, but its effective.  it will handle those cases where the player is moving up and right, and he hits a wall to his right.  You will keep moving him right, then placing him back on the x-axis, then move him up, and he won't hit anything bove him, and the y-position will change.

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)

### #8Servant of the Lord  Members

33471
Like
0Likes
Like

Posted 28 January 2013 - 04:15 PM

Though if the L shape is the shape of the inside of the room (and not the wall), you'll want a rectangle for each wall.

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #9mypel16000  Members

14
Like
0Likes
Like

Posted 28 January 2013 - 04:22 PM

Thank you beernuts! Most helpfull of all.

And Servant of the lord, I appreciate your effort, but the room isn't actually L shaped. There is a counter with an L shape inside the room

### #10Milcho  Members

1177
Like
2Likes
Like

Posted 28 January 2013 - 04:26 PM

I would suggest moving your player in the x-direction 1st, then checking for collision.  If it collided, move it back to where it was (by just adding the -velocity).  Then move the player in the y-direction and check for collisions.  If it collided, move it back to where it was. This would keep the player always just off the wall.

This is a simple method of doing it, but its effective.  it will handle those cases where the player is moving up and right, and he hits a wall to his right.  You will keep moving him right, then placing him back on the x-axis, then move him up, and he won't hit anything bove him, and the y-position will change.

interesting, separation of the axis. This would work for rectangle collision, but it won't give accurate results for collision vs a circle. Here's one case, where the y-axis will fail, but the x-axis won't, and you'll end up in the wrong spot (the red arrows are the axis separation, the green is the minimum distance displacement)

Worst case I can think of actually ends up where both your x and y axis tests will fail (both will collide with circle) and you'll end up being stuck on the circle.

### #11BeerNutts  Members

4287
Like
1Likes
Like

Posted 28 January 2013 - 04:29 PM

This would work for rectangle collision, but it won't give accurate results for collision vs a circle.

Oops, he was asking about circles and polygons.  Yes, my solution could cause problems.  I thought it was only rectangles.

Be careful using my solution mypel.

Edited by BeerNutts, 28 January 2013 - 04:31 PM.

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)

### #12mypel16000  Members

14
Like
0Likes
Like

Posted 28 January 2013 - 04:31 PM

Yes, beernuts, my work is only on rectangles

### #13Milcho  Members

1177
Like
1Likes
Like

Posted 28 January 2013 - 05:34 PM

.

Oops, he was asking about circles and polygons.  Yes, my solution could cause problems.  I thought it was only rectangles.

Be careful using my solution mypel.

I'm just nitpicking here but I also have to point out that with your solution, under certain not so difficult to achieve conditions, the player sprite will 'collide' with the wall before he is physically touching it  - in the case where the player is, say, 0.2 meter from the wall, and your 'move' takes you 0.3 m into the wall, your collision method would stop the player from moving any closer to the wall and he will remain 0.2m from the wall.

The faster the movement speeds, as well as the slower the position update, the less accurate the results will be. Of course at 60fps and normal human speeds, this isn't an issue, which is why it's just nitpicking

### #14Megahertz  Members

286
Like
0Likes
Like

Posted 29 January 2013 - 12:21 AM

Collision detetion is fairly easy and straight forward. It's what to do after you've detected a collision thats the hard part.

The following link is not meant to solve your issues however it should give you a good deal of insight as to how to go about things. It was written with 3D in mind and quite honestly never quite worked right due to floating point errors (maybe later fixed?) however I think it's still worth reading.

As the article is 3D oriented, it can certainly be "dumbed down" for 2D as the principles still apply.

Hope this helps.

-=[Megahertz]=-

### #15mypel16000  Members

14
Like
0Likes
Like

Posted 29 January 2013 - 01:09 AM

Yes, the article is quite useful! Thumbs up tp you.

@BeerNuts and Milcho: I'll try combining both your methods and instead of moving -velocity.y or -velocity.x, I will calculate the distance to the edge and correct it, while still separating the axis

That would be a good idea wouldn't it?

If it is, I'll start writing it and then post it here so that everyone can see.

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.