Capsules, Collisions and Quaternions

Capsules, Collisions and Quaternions

A short code postmortem on my work on Game-Physics for my MSC Course Work

ยท

3 min read

๐Ÿ’ก
Key Features: Capsule collisions with AABBs, OBBs, Capsules, and Rays.

When I gleaned that we would be writing our own collision code, I knew that I wanted to attempt capsule collisions. Capsules are integral to games, and what I didn't know back then, but am wise to now, is that they're all essentially just spheres.

Below are some code samples and added directors commentary explaining what everything does.

Capsule vs Sphere

Once we realize the fact that capsules can be treated as spheres if we can figure out the right position for the collision, the rest is then just doing some math as to where we treat it as a sphere. Understanding the rotation of the capsule is important to figure out where the top and the bottom of the capsule is, since the capsule class did not have the ends included in it.

The SqDistPointSegment and ClosestPointLineSegment helper methods were written by me, and return the squared distance of a point from a line segment, and the closest point on a line segment from a point, respectively.

Since Spheres essentially don't have rotation, their rotation can be ignored and the function can be simplified to two spheres colliding.

Capsule vs AABBs

Another case of interesting math to figure out where the Sphere would be with respect to an AABB before doing what is a relative sphere AABB test collision. We find the closest point on the face on the AABB where it is colliding, take a projection on the direction of the capsule, which then allows us to calculate the closest point on that line segment, and do whats done in a sphere AABB collision.

Capsule Vs OBB

Ah, one of my favorites. Nothing really feels as good as working with quaternions in game physics. Since OBBs are rotated boxes, we will have to account for their rotation in our collision. We want to reduce the amount of math done and still have a relatively accurate collision.

So, we bring the capsule to the OBBs local rotation by rotating it by the inverse of the OBBs rotation. This rotation is achieved by taking the conjugate of the quaternion's Matrix4. And then we move it to the local space by subtracting the local position. This is now a capsule vs AABB collision test, which we call and then move the collision info back into the world space by multiplying it with the original transform.

Capsule vs Rays

A fairly short method, we calculate the projection of the Ray along the direction vector of the ray's origin pointing to the capsule's center. Then we get the closest point on that direction, along the capsule line segment, and call a RaySphere test there.

Helper functions

These are some quick vector math functions I wrote up to return values that I knew I would be using a lot.

Potential Improvements

  • The orientation, Capsule top, capsule bottom could have been cached into the capsule volume class so these calculations wouldn't have to be done in every method itself.

  • Since all the collision calculation is assuming the capsule is a sphere, sometimes during slow continuous collisions the behavior of the capsule mirrors that of a sphere too many times. The fix for this would be conditionals that could rule out some cases.

  • Some edge cases, especially when rays are fired from a sharp angles tend to miss the capsule because of how dot product works. The potential fix for this would be to set the closest point to the edges of the capsule line segment for cases where the ray is fired from extremely sharp angles.

And that's capsules for you! Stay tuned for SAT and cool Shader stuff!

ย