Let us set filter masks for HavokPlugin.prototype.raycast()

I saw in the Babylon.js source code that HavokPlugin.prototype.raycast() uses some default settings for filter masks (queryMembership and queryCollideWith) for what the raycast should “collide” with. As a test, I altered those filters to see if they actually worked when set to useful values, and they do! Exposing these parameters to the users would be extremely useful to fine tune the fast physics raycasting, especially since no predicate function is available to filter out unwanted hits. Thank you.

3 Likes

@Cedric

Makes sense. I’m opening an issue to keep track.

Are you OK to do a PR for that? It looks like you know exactly what to do. I’d be happy to review it.

I don’t have a build environment set up, but if it’s helpful, I could show how I would do it:



Current code/comments in PhysicsEngine:

    /**
     * Does a raycast in the physics world
     * @param from when should the ray start?
     * @param to when should the ray end?
     * @param result resulting PhysicsRaycastResult
     */
    PhysicsEngine.prototype.raycastToRef = function (from, to, result) {
        this._physicsPlugin.raycast(from, to, result);
    };
    /**
     * Does a raycast in the physics world
     * @param from when should the ray start?
     * @param to when should the ray end?
     * @returns PhysicsRaycastResult
     */
    PhysicsEngine.prototype.raycast = function (from, to) {
        var result = new _physicsRaycastResult__WEBPACK_IMPORTED_MODULE_1__.PhysicsRaycastResult();
        this._physicsPlugin.raycast(from, to, result);
        return result;
    };


Proposed new code/comments in PhysicsEngine, trying to keep backward compat.:

    /**
     * Does a raycast in the physics world
     * @param from when should the ray start?
     * @param to when should the ray end?
     * @param result resulting PhysicsRaycastResult
     * @param optional bitmask of categories this ray originator belongs to
     * @param optional bitmask of categories this ray should intersect with
     */
    PhysicsEngine.prototype.raycastToRef = function (from, to, result, queryMembership, queryCollideWith) {
        this._physicsPlugin.raycast(from, to, result, queryMembership, queryCollideWith);
    };
    /**
     * Does a raycast in the physics world
     * @param from when should the ray start?
     * @param to when should the ray end?
     * @returns PhysicsRaycastResult
     * @param optional bitmask of categories this ray originator belongs to
     * @param optional bitmask of categories this ray should intersect with
     */
    PhysicsEngine.prototype.raycast = function (from, to, queryMembership, queryCollideWith) {
        var result = new _physicsRaycastResult__WEBPACK_IMPORTED_MODULE_1__.PhysicsRaycastResult();
        this._physicsPlugin.raycast(from, to, result, queryMembership, queryCollideWith);
        return result;
    };



Current code/comments in HavokPlugin:

    /**
     * Performs a raycast from a given start point to a given end point and stores the result in a given PhysicsRaycastResult object.
     *
     * @param from - The start point of the raycast.
     * @param to - The end point of the raycast.
     * @param result - The PhysicsRaycastResult object to store the result of the raycast.
     *
     * Performs a raycast. It takes in two points, from and to, and a PhysicsRaycastResult object to store the result of the raycast.
     * It then performs the raycast and stores the hit data in the PhysicsRaycastResult object.
     */
    HavokPlugin.prototype.raycast = function (from, to, result) {
        var queryMembership = ~0;
        var queryCollideWith = ~0;
        var query = [this._bVecToV3(from), this._bVecToV3(to), [queryMembership, queryCollideWith]];
        this._hknp.HP_World_CastRayWithCollector(this.world, this._queryCollector, query);
        ... etc.





Proposed new code/comments in HavokPlugin, trying to keep backward compat.:

    /**
     * Performs a raycast from a given start point to a given end point and stores the result in a given PhysicsRaycastResult object.
     *
     * @param from - The start point of the raycast.
     * @param to - The end point of the raycast.
     * @param result - The PhysicsRaycastResult object to store the result of the raycast.
     * @param optional bitmask of categories this ray originator belongs to
     * @param optional bitmask of categories this ray should intersect with
     *
     * Performs a raycast. It takes in two points, from and to, and a PhysicsRaycastResult object to store the result of the raycast.
     * It then performs the raycast and stores the hit data in the PhysicsRaycastResult object.
     */
    HavokPlugin.prototype.raycast = function (from, to, result, queryMembership, queryCollideWith) {
        if (queryMembership === void 0) { queryMembership = ~0; }
        if (queryCollideWith === void 0) { queryCollideWith = ~0; }
        var query = [this._bVecToV3(from), this._bVecToV3(to), [queryMembership, queryCollideWith]];
        this._hknp.HP_World_CastRayWithCollector(this.world, this._queryCollector, query);
        ... etc.



And then the docs at Raycast | Babylon.js Documentation should probably mention this filtering ability. The havok raycast is so blazing fast, I use it for everything. Especially with these filters, it’s like a swiss army knife. Thank you in advance.

1 Like