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.
Trouble with collision resolution (sliding entities)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Trouble with collision resolution (sliding entities)
- Attachments
-
- collision.love
- (1.09 KiB) Downloaded 298 times
Re: Trouble with collision resolution (sliding entities)
Hi there.
Collision response can be a very complicated problem so keep that in mind.
1. move objects
2. check for intersections
3. separate intersecting objects
4. adjust velocities of colliding objects
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)
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.
Collision response can be a very complicated problem so keep that in mind.
Basically you want to adjust your algorithm so it looks like:I'm writing some collision code
1. move objects
2. check for intersections
3. separate intersecting objects
4. adjust velocities of colliding objects
You can't do that if you want decent-looking collisions.if so move the player to his previous x position
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)
There are 2 variables that will affect collisions: friction and restitution.I'd like to be able to slide along the box while holding two keys
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.
Re: Trouble with collision resolution (sliding entities)
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.
Who is online
Users browsing this forum: Google [Bot] and 4 guests