Page 1 of 1

Trouble with collision resolution (sliding entities)

Posted: Fri Apr 01, 2016 7:41 am
by monkeyman
Hi, I'm writing some collision code that I can't seem to get working the way I want. Here are the steps I'm taking to try to resolve collisions between a player moved by the arrow keys and a static box:

Step 1: move player in response to key presses

Step 2: check if the player's bounding box intersects with the box's bounding box, if so continue to step 3, otherwise back to step 1

Step 3: check if the player collided with the box along the x axis, if so move the player to his previous x position (before the collision happened) and continue to step 4, otherwise go to step 5

Step 4: check if the player collided with the box along the y axis, if so move the player to his previous y position, then go back to step 1

Step 5: the player must have only collided along the y axis, so move the player to his previous y position, and leave his x position as-is

So far the code mostly works, it resolves the collision nicely if the player is only moving in one direction (either at the box or along it), but if you try to move into the box diagonally the player stops in a jarring way. I'd like to be able to slide along the box while holding two keys. I've used Kikito's Bump library before and it handled the sliding behavior very well, but his code is slightly above my understanding so if I can code my own collision functions from scratch in a more simple way (even with outside help) that would be preferable.

Any help you guys are able to provide would be great.

Re: Trouble with collision resolution (sliding entities)

Posted: Fri Apr 01, 2016 8:00 am
by ivan
Hi there.
Collision response can be a very complicated problem so keep that in mind.
I'm writing some collision code
Basically you want to adjust your algorithm so it looks like:
1. move objects
2. check for intersections
3. separate intersecting objects
4. adjust velocities of colliding objects
if so move the player to his previous x position
You can't do that if you want decent-looking collisions.
You have to find the "shortest separation vector" between the two colliding rectangles.
Then you separate the objects and adjust their velocities.
This is called "non-continuous" collision (bump uses "continuous" collision which is more complicated)
I'd like to be able to slide along the box while holding two keys
There are 2 variables that will affect collisions: friction and restitution.
These two variables affect how the velocities are adjusted AFTER the collision.
Here is the tutorial I wrote a while ago:
http://2dengine.com/doc/gs_collision.html
HTML5 demo:
http://2dengine.com/doc/tutorials/html5/index.html
Note that if the restitution (bounce) is always 0 and friction is always 0
then the equations can be simplified even further.

Things get quite complicated if you want to have torque/rotation,
realistic transfer of momentum (depending on mass)
or a lot of moving objects (broadphase) so it's important to keep it as simple as possible
when writing your own collision system.

Re: Trouble with collision resolution (sliding entities)

Posted: Fri Apr 01, 2016 8:19 am
by monkeyman
Thanks Ivan, I appreciate your help. That's definitely a lot of information to take in, I'll keeping working at it and I'll bump the thread if I need anymore guidance.