Physics Collision Types

I’m using the “registerOnPhysicsCollide” to detect collisions between two impostors. When testing this I notice it continually calls the callback even when the impostors are touching but not moving (like a sphere touching the ground). I understand this is how it should work since technically it’s still colliding, but is there a way to check if a collision happens once (first time it collides)?

I know in Unity you can use an “OnCollisionEnter” and “OnCollisionExit” and wanted to know if there was something similar within Babylon? If not I suppose I could add in some sort of boolean toggle of sorts to check this myself (possibly more complex for exiting).

What immediately comes to mind as a work around is to store a lastCollided state in some variable, increment every frame, and pull it to zero on collision, then if lastCollided > 1, and the callback is triggered, you know it’s on enter collision, also when incrementing, if lastCollided == 2, it’s a new collision.

Apologies for the vague logic dump, hope that helps

1 Like

@jgonzosan it unfortunately does not exist by default but would be an awesome contribution :slight_smile:

1 Like

Yeah, that could work for detecting how many times something collides. I think for the collision “exit” I could detect if a collision has happened at least once and check the distance between the objects. Would be nice if there was something easier for the collision exit.

Good to know, thanks.