Test stationary capsule against colliders in scene.

I have a capsule defined by two end points and a radius. I would like to query the scene for any collisions with this capsule and retrieve the collision point and normal.

Note: Physics.CapsuleCastAll (p1,q1, Radius, Vector3.zero); Does not produce any hits.

Note: Physics.CapsuleCastAll (p1,q1, Radius, Vector3.up, 0); Doesn’t produce any hits.

This appears to be erroneous as a capsule has a defined volume and whether or not I’ve dragged it through space shouldn’t be relevant to if it intersects things.

I’d rather not write capsule tests for all the shapes in Unity and manually test the scene. So how do I query Unity for this information?

Motivations for my approach:

I’m implementing thread simulation via position based dynamics. I have a thread composed of nodes which each have a mass and velocity, position, and torsion. I solve a set of constraint equations to make the thread behave as one would expect. The space between two connected nodes forms a capsule shape. Since the thread has radius, and two nodes have two points I get a capsule. I handle self-collisions internally but in order to integrate it into unity’s physics I need to get collision data for these capsules.

Why I can’t use a GameObject:

If I wanted to use a GameObject I would have to create an object for each space inbetween nodes. I would have to use a non-kinematic rigidbody for this intermediary spaces between nodes (Or else I wouldn’t get collisions against static shapes). Which in lies the first problem, the capsules aren’t rigid they change size every frame. I’d have to update a rigid body each frame with approximated values for velocity, mass, inertial tensor, etc… The capsules don’t behave that way. I’d be forming a bridge which would introduce further error into the simulation. I can’t have that messing with my thread. I need a way to query the scene for the data to handle the collisions myself. How do I accomplish this?

I may have misunderstanding your question, but if you want to get the collision from a capsule gameObject, attach a capsuleCollider and a rigidbody to it. Then attach a script with the OnCollisionEnter(Collision collision) function.

I ran into a similar situation a while back where I really needed to get all colliders intersecting a capsule. It’s not really obvious from the documentation, but CapsuleCastAll needs a distance greater than zero to return any results, and apparently the results it will return are what colliders would intersect the capsule if it was moved by distance along direction, which isn’t what I needed and it seems it’s not what you need either.

In the end I got around it by approximating the shape of the capsule using spheres and calling Physics.OverlapSphere on each sphere. It was more than a good enough approximation for my use case, and I’d venture to guess that it should be enough for yours as well.