# ricocheting off a wall

I have a capsule (sphere sweep) that I've computed the collision on a mesh with.

I know the point of impact and normal of the face it collides with.

My moving object - I'm using a capsule to represent it for collision purposes - has a quaternion describing its direction of travel.

How do I compute the new quaternion for the object to travel in on the bounce?

-
I assume this is in a 3D space? – Justin Skiles Oct 7 '12 at 15:11
@JustinSkiles yes, 3D – Will Oct 7 '12 at 15:13
When an object bounces of a wall, it's orientation does not necessarily change. That depends on other physical factors like friction and sometimes elasticity. What does change is the object's velocity Vector. And the velocity is usually independent of pose (oriention) – Steve H Oct 7 '12 at 16:55
@SteveH yes, although my velocity vector is a quaternion. I've tried to clean up my terminology – Will Oct 7 '12 at 20:24
@SteveH is right, but things are usually more complicated since there are also rotational effects/velocities that can result from a collision. hakenberg.de/diffgeo/collision_resolution.htm (the inertia tensor is the accurate way to describe how a body tends to rotate). – teodron Oct 8 '12 at 7:27

If your object has a forward direction fwd=(x,y,z) and an up direction in its rest position (they all have in 3D, unless they're points or lines), then your quat orientation gives you the current fwd and up vectors.

Perform collision resolution and obtain fwd' and up'.

These two vectors then give you an orientation (a frame) that you can convert to a matrix and get the quat out of it describing your object's new direction of travel and up direction.

-
yes, precisely. My question is how to do this though. How do you calculate the new fwd' and up' of a rebound given the normal of the face you hit? – Will Oct 8 '12 at 8:02
@Will, see snake5's answer and, perhaps, an answer I gave a while ago gamedev.stackexchange.com/questions/29355/… and this is what I got using that algorithm youtube.com/watch?v=jS-FhuJkd4c&feature=youtu.be – teodron Oct 8 '12 at 8:37

Once you have the collision normal (not the normal of the face because corner collisions won't work right that way), you can just split the velocity (which should really be a vector, not a quaternion) into two: normal velocity and tangent velocity.

``````normalVelocity = dot( collisionNormal, linearVelocity ) * collisionNormal;
tangentVelocity = linearVelocity - normalVelocity;
``````

Now you can just invert the normal velocity to make the object bounce off the collision and recombine both into the new linear velocity vector.

``````normalVelocity *= -1;
linearVelocity = normalVelocity + tangentVelocity;
``````

I won't cover quaternion-to-vector (and reverse) transformations in this answer because a quaternion isn't the most effective representation of the data. A simple 3D vector does the job as expected and has been already used practically everywhere.

-