AMMO: raycastAll hits back-faces, behaves differently than raycasFirst

Hi dearest community,

I’ve encountered some strange behaviour: Sometimes raycastAll hits the wrong faces.

weird_raycasts

  • raycastFirst on mesh with triangles exported: WORKS
  • raycastFirst on mesh with quads exported: WORKS
  • raycastAll on mesh with triangles exported: WORKS SOME TIMES?
  • raycastAll on mesh with quads exported: WORKS

Internally, the physics should convert the quads into triangles, which makes the behaviour even weirder.

Please find the sample project here: https://playcanvas.com/project/722719/overview/forum-raycasting-quads

Maybe I’ve found a strange bug here, or maybe I just need some more sleep.
Anyways, any hints appreciated.

Have a great day,
Rene

EDIT:

raycastAll also returns one additional raycast result at the backface. The returned results are not sorted by distance, so one could do it manually like this (sample position is the start position of the raycast):

Raycaster.prototype._getNearestResult = function(samplePosition, results) 
{
    var nearestDistance = Number.MAX_VALUE;    
    var nearestResult = null;
    
    for(var i=0; i<results.length; i++)
    {
        var distance = samplePosition.clone().sub(results[i].point).lengthSq();
        
        if(distance < nearestDistance)
        {
            nearestResult = results[i];
            nearestDistance = distance;
        }
    }
    
    return nearestResult;
};

This works for me, but I’m still curious why it returns a result on an outward facing normal and sorts the results differently on quads…

I don’t think we have the filter added to ignore backfaces looking at our code :thinking: Not sure if it was by design or not. Looks like it is possible to add extra conditions on the raycast object (https://pybullet.org/Bullet/phpBB3/viewtopic.php?f=9&t=10924)

Yes, we should really add that to the documentation.

I would just assume they are not sorted rather than sorted differently.

1 Like

Hi @yaustar,

thanks for your reply.

Coming from Unity (where the back facing triangles get skipped by default) this was nothing I was aware of.

It may be faster to skip the backfaces by default (HERE) but I’m not sure whether Ammo supports this, and also the strings attached to such a big change…

Best,
Rene

1 Like