raycastFirst error when passing options

I’m trying to raycast only to a collisionGroup, but the function throws an error when I pass the filterCollisionGroup as a parameter.
This is the part of my code where I make the raycast:

var result = this.app.systems.rigidbody.raycastFirst(from, to, {
        filterCollisionGroup: pc.BODYGROUP_USER_2
    });

And this is the error that I received:
image

If you pass the ‘options’ parameter to the raycastFirst of a Rigidbody it throws an error because at the end of that function is trying to use that parameter as a function:

raycastFirst(start, end, options = {}) {
        // Tags and custom callback can only be performed by looking at all results.
        if (options.filterTags || options.filterCallback) {
            options.sort = true;
            return this.raycastAll(start, end, options)[0] || null;
        }

        let result = null;

        ammoRayStart.setValue(start.x, start.y, start.z);
        ammoRayEnd.setValue(end.x, end.y, end.z);
        const rayCallback = new Ammo.ClosestRayResultCallback(ammoRayStart, ammoRayEnd);

        if (typeof options.filterCollisionGroup === 'number') {
            rayCallback.set_m_collisionFilterGroup(options.filterCollisionGroup);
        }

        if (typeof options.filterCollisionMask === 'number') {
            rayCallback.set_m_collisionFilterMask(options.filterCollisionMask);
        }

        this.dynamicsWorld.rayTest(ammoRayStart, ammoRayEnd, rayCallback);
        if (rayCallback.hasHit()) {
            const collisionObj = rayCallback.get_m_collisionObject();
            const body = Ammo.castObject(collisionObj, Ammo.btRigidBody);

            if (body) {
                const point = rayCallback.get_m_hitPointWorld();
                const normal = rayCallback.get_m_hitNormalWorld();

                result = new RaycastResult(
                    body.entity,
                    new Vec3(point.x(), point.y(), point.z()),
                    new Vec3(normal.x(), normal.y(), normal.z()),
                    rayCallback.get_m_closestHitFraction()
                );

                // keeping for backwards compatibility
                if (arguments.length > 2) {
                    Debug.deprecated('pc.RigidBodyComponentSystem#rayCastFirst no longer requires a callback. The result of the raycast is returned by the function instead.');

                    const callback = arguments[2];
                    callback(result);
                }
            }
        }

        Ammo.destroy(rayCallback);

        return result;
    }

This code is from the system.js of the rigidbodies
am I doing something wrong or is there a workaround?

I think you’re right, there’s a bug, which was introduced about 2 months ago here I believe, where the options were added: Implements results filtering for `raycastAll` and `raycastFirst` by MushAsterion · Pull Request #5180 · playcanvas/engine · GitHub

2 Likes

I created an issue: raycastFirst error when passing options · Issue #5367 · playcanvas/engine · GitHub

I think this code should just be removed now:

// keeping for backwards compatibility
if (arguments.length > 2) {
    Debug.deprecated('pc.RigidBodyComponentSystem#rayCastFirst no longer requires a callback. The result of the raycast is returned by the function instead.');

    const callback = arguments[2];
    callback(result);
}

The fix: Remove no longer compatible handling of deprecated parameter by RigidBody.raycastFirst by mvaligursky · Pull Request #5368 · playcanvas/engine · GitHub