Layer-based raycast?


#1

Hey, is there any way to do raycasting ignoring some entities in the middle?

I saw this thread existed in 2016 but there was not any way to achieve this: How to Create a Ray with Ignore Collision Entity? Or Multi Hit Result

Has this changed nowadays? Or we still can’t ignore entities based on layer or something?

This seems like a must-have feature to me :frowning:


#2

The ammo/bullet API allows you to assign physics objects to groups and have them collide according to a mask. This can also be used with raycasting although none of this is exposed in the PlayCanvas API.

A project that shows how to use the groups can be found here: https://playcanvas.com/project/519294/overview/physics-mask

The minified code from Virtual Voodoo for the raycast is here:

    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
    }

#3

Thanks I will test this out on monday


#4

Can this be implemented without Rigidbodies? Raycast works against plain Colliders, and I don’t need Rigidbodies for anything.

Edit: okay forget it I didn’t see you already implemented it without rb.

Btw is there any reason why this isn’t documented or implemented by the PlayCanvas team if they see it’s working great?


#5

Just to update the thread.

The difficulty here was to assign the custom group to the collider as it is not exposed in the same way as the rigidbody.

In the end, I had to remove the body from the world and re-add it with the desired group. eg (where flags represent the custom groups)

    // Ugh
    this.app.systems.rigidbody.removeBody(body);

    // set the body's activation state to disable simulation so
    // that it properly deactivates after we remove it from the physics world
    body.forceActivationState(pc.BODYSTATE_DISABLE_SIMULATION);

    this.app.systems.rigidbody.addBody(body, pc.BODYGROUP_TRIGGER | flags, pc.BODYMASK_NOT_STATIC ^ pc.BODYGROUP_TRIGGER);

    // set the body's activation state to active so that it is
    // simulated properly again
    body.forceActivationState(pc.BODYSTATE_ACTIVE_TAG);

    body.activate();

    this.entity.trigger.syncEntityToBody();