Check the distance of the intersection point from the raycast starting point. Keep track of the closest entity hit, apply logic to the closest hit entity
I have the code in the other thread you have posted. There’s a link to a project using layers and in the linked thread, a function to do the filtered raycast.
I have now seen that I cannot use it the way I hoped. This is because I use different raycasts from the same object that should have an effect on different masks.
I don’t think there is a way to check if the raycast hit a specific mask or something like that, so I have to come up with a different setup for my game.
var n = new Ammo.btVector3
, t = new Ammo.btVector3
, e = function(n, t, e) {
this.entity = n,
this.point = t,
this.normal = e
};
pc.RigidBodyComponentSystem.prototype.raycastFiltered = function(o, i, r, a) {
n.setValue(o.x, o.y, o.z),
t.setValue(i.x, i.y, i.z);
var l = new Ammo.ClosestRayResultCallback(n,t);
l.set_m_collisionFilterGroup(r),
l.set_m_collisionFilterMask(a),
this.app.systems.rigidbody.dynamicsWorld.rayTest(n, t, l);
var s = null;
if (l.hasHit()) {
var u = l.get_m_collisionObject()
, c = Ammo.castObject(u, Ammo.btRigidBody)
, m = l.get_m_hitPointWorld()
, d = l.get_m_hitNormalWorld();
c && (s = new e(c.entity,new pc.Vec3(m.x(),m.y(),m.z()),new pc.Vec3(d.x(),d.y(),d.z())))
}
return Ammo.destroy(l),
s
}
I really have no idea how to use the code.
If I have to use this code for every raycast, I will still look for an alternative.
Isn’t that what you want though? A raycast function that ignores certain groups/layers of rigidbodies? The code above adds a new function to the rigidbody system. So instead of calling raycastFirst, you would call raycastFiltered.