Trouble with collision resolution (sliding entities)
Posted: Fri Apr 01, 2016 7:41 am
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.
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.