Not very sexy, but very useful: there are 2 new methods in the bullet wrapper that enables retrieval of an ordered list of the collisions along a ray.

The job was super easy, thanks to the bullet lib consistency and documentation. The object to use is AllHitsRayResultCallback. Once processed, it returns a list of bodies touched by the given ray.

The last part of the job was to order this list. The fastest way I thought of was to use an std::map, and use distances between the start of the ray and the hit point as keys. The nice thing with maps is that they are automatically sorted, if you use a native numeric key, such as floats.

This map is then turned into an array of PBulletRay.

In the code that use these methods, you can loop over the array, all objects are sorted from the closest (relatively to camera) to the farthest.

Available in repo (not yet documented nor doxygened)