How to use only the needed result of raycastAll?

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_collisionFilterMask(a),, 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),

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:


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?

Okay, I understand. Thanks for your effort though!

The problem is mainly that the raycast starts from the player and then first hit the player. That prevents me from using raycastFirst.

Maybe someone knows how to work around this without using raycastAll because using raycastAll again causes problems like the problem of this post.

And yes, that can come in handy anyway. :upside_down_face:

Added a raycastFirstByTag extension function get returns the closest hit entity with a particular tag.

1 Like