Trouble with collision resolution (sliding entities)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
monkeyman
Prole
Posts: 6
Joined: Fri Feb 19, 2016 6:29 am

Trouble with collision resolution (sliding entities)

Post 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.
Attachments
collision.love
(1.09 KiB) Downloaded 298 times
User avatar
ivan
Party member
Posts: 1918
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Trouble with collision resolution (sliding entities)

Post 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.
Last edited by ivan on Mon Apr 04, 2016 11:19 am, edited 1 time in total.
monkeyman
Prole
Posts: 6
Joined: Fri Feb 19, 2016 6:29 am

Re: Trouble with collision resolution (sliding entities)

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests