How to use only the needed result of raycastAll?

For example my racaystAll get 3 results:

  1. Player > The problem why need to use raycastAll.

  2. Ground > The needed ground entity.

  3. Ground > Another ground entity under the first ground entity.

In this case I only want to use the second result.
How can I prevent that my script does the same things for the third result?

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

Okay, that’s a possibility indeed, but I don’t know exactly how to do this.
Or is there a possibility to exclude an entity from raycastFirst?

Not without using masks and layers.

I would like to know how I can do this with mask and layers. Do you also have an example project for this?

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.

Another option, could probably be to use a plane, instead of a cube for a ground, which will give you only 1 intersection.

Hello @yaustar!

I think you mean the function in this post?


Can you tell me where (or how) I have to use that function?

Or do I only need the scripts in the project?

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.

They raycast function I made allows you to do a raycast that ignores rigidbodies on certain layers/group. Is that what you want?

Yes, but for example I use 2 raycasts from 1 entity and raycast 1 has to ignore group 1 and raycast 2 has to ignore group 2.

Yes, that should be doable with that raycast.

Do you mean with the script(s) from this project?

With the code from that project and also the raycast code from the forum post linked above.

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.

Oke, then yes that is what I need. :yum:

So I have to past that code somewhere in my script?

How do I use it?

Trying to work on a quick example :sweat_smile:

:heart:

Ok, I’ve started doing this but I don’t like the way I’ve done this in the past as it doesn’t scale into a larger project like yours.

If I do find some time, I will try to put together a more cohesive example :thinking:

In the meantime, I can put together an example of what I was referring to in this post. It’s simpler and much less likely to have bugs: How to use only the needed result of raycastAll?