Archive 17/01/2023.

Can’t get collisionMask to work in physics ray/sphere cast

Dave82

I have a character and a ground body set up various collision mask to perform sphere cast. I’m trying to get the distance between player head and the ground so i have set it up like this :

enum eCollisionMask
{
      CLM_NONE,
      CLM_LEVELMESH = 1,
      CLM_PLAYER = 2,
      CLM_RAYCASTABLE = 4,
}

characterBody->SetCollisionMask(CLM_PLAYER | CLM_LEVELMESH);
groundPlaneBody->SetCollisionMask(CLM_LEVELMESH | CLM_RAYCASTABLE);

And perform a sphere cast (Note : i want to perform a spherecast from the top of the player’s head so i want to ignore bodies that have CLM_PLAYER flag set.

SphereCast(result , ray , radius , maxGroundDistance , CLM_RAYCASTABLE);

The problem is there’s no body in result and distance is set to infinity when it clearly should be a hit.

if i ignore the viewMask flag in the SphereCast function (using MAX_UNSIGNED) and set the characterBody collision flag to 0 before perform the spherecast then restore it back after spherecast it works perfectly , however this method is most likely slow and it just doesn’t seem right.

unsigned oldMask= characterBody->GetCollisionMask();
characterBody->SetCollisionMask(0);
SphereCast(result , ray , radius , maxGroundDistance , MAX_UNSIGNED);
characterBody->SetCollisionMask(oldMask);

// It Works !

Am i missing something ? BTW im using 1.5

Modanung

The collision mask defines what layers an object collides with. The layer of a body can be set using RigidBody::SetCollisionLayer(unsigned).

Dave82

It works now ! Thanks !