Raycast not percieving Ammo added object

So I added random distributer of Rock like objects using hardwareInstancing and add collision directly on Ammo.

            const newTrans = new Ammo.btVector3 ();
            newTrans.setX(pos.x);
            newTrans.setY(pos.y);
            newTrans.setZ(pos.z);

            const transformDoIt = new Ammo.btTransform();
            transformDoIt.setOrigin(newTrans);
            
            const rbody = new Ammo.btRigidBody ();
            rbody.setWorldTransform (transformDoIt);
            rbody.setCollisionShape (shape);
  
            dynamicsWorld.addRigidBody(rbody);

            rbody.activate(true);             

I added collision like this way,

(function(){    
    var _distanceVec3 = new pc.Vec3();
    
    pc.RigidBodyComponentSystem.prototype.raycastFirstByTag = function (start, end, tag) {
        // var closestDistanceSqr = Number.MAX_VALUE;
        // var closestResult = null;
        
        // Go through all the entities that intersect with the raycast and 
        // find the closest one that matches the tag query passed
        // var results = this.app.systems.rigidbody.raycastAll(start, end, tag);
        let result_Ammo = null;
        var ammoRayStart = new Ammo.btVector3();
        var ammoRayEnd = new Ammo.btVector3();

        ammoRayStart.setValue(start.x, start.y, start.z);
        ammoRayEnd.setValue(end.x, end.y, end.z);

        var rayCallback = new Ammo.ClosestRayResultCallback(ammoRayStart, ammoRayEnd);

        rayCallback.m_collisionFilterGroup = pc.BODYGROUP_DYNAMIC | pc.BODYGROUP_STATIC| pc.BODYGROUP_DEFAULT;
        rayCallback.m_collisionFilterMask = pc.BODYGROUP_DYNAMIC | pc.BODYGROUP_STATIC| pc.BODYGROUP_DEFAULT;

        this.app.systems.rigidbody.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_Ammo = new pc.RaycastResult(
                    body.entity,
                    new pc.Vec3(point.x(), point.y(), point.z()),
                    new pc.Vec3(normal.x(), normal.y(), normal.z())
                );
            }
        }

        Ammo.destroy(rayCallback);

        
        // for (var i = 0; i < results.length; ++i) {
        //     var result = results[i];
        //     if (result.entity.tags.has(tag)) {
        //         _distanceVec3.sub2(result.point, start);
        //         var distanceSqr = _distanceVec3.lengthSq();
        //         if (distanceSqr < closestDistanceSqr) {
        //             closestDistanceSqr = distanceSqr;
        //             closestResult = result;
        //         }
        //     }
        // }
        
        return  result_Ammo;
    };
})();

So this is raycast code that i used but it doesn’t return collision I added upper way. Any Solution

Hi @Deanies,

Not sure about this, but one thing that can help you debug this is using this extension, it will visualize your rigid bodies on runtime:

1 Like

Most probably the issue is in the filters. If you are using filters in the raycast, you should assign correct group and mask to the body you created in order for the raycast to hit it.

Thank you! I was able to find the problem using debugger. It was the object size Issue. Object was set too small than expected so raycast wasnot able to find it. But as I look through the engine, I was pretty sure btrigidbody.setcollisionshape is the only way to set shape/size for collision. but when I play around it., increasing half extent value or localscaling, Notting happened. Shape only remained as small as dot. Is there another way to increase this size?