I have a room with objects inside. Each object has it's own AABB.
I know how to check for collisions between the objects with an AABB-AABB intersection.
How do I calculate an appropriate collision response to make them frictionlessly slide against each other as they collide?
Answer
What you're looking to do is have the solid object (wall, obstacle, etc.) push back with a force equal to that which the player is exerting on it. In a simulated physical world, applying forces will not result in the behavior you want, so the velocity of the player has to be changed directly. This is what's known as an impulse.
The impulse you want to apply to the player's velocity vector is:
impulse = -(player.velocity * collision.normal) * collision.normal
Where collision.normal
is the normal of the face of the AABB that the player is colliding with. This is simply projecting the player's velocity vector onto the face's normal, obtaining the velocity change that the AABB will enact on the player (it is negated because technically we would subtract this vector from the player's velocity, but conceptually it makes more sense to add it). Finally, add the impulse to the player's velocity:
player.velocity += impulse
And the result will be the component of the players velocity which was horizontal to the obstacle, so the player will 'slide' along the object. Note that this will work even if the player is sliding directly along the sphere, provided you calculate the collision normal correctly.
Hope this helps :)
No comments:
Post a Comment