Havok collision callback: how to interpret provided collision info

Playground: https://playground.babylonjs.com/#Z8HTUN#307

In my project, I need to know when to trigger events based on collision results. For example, when to play a crashing sound when two objects collide. But the incoming collision information isn’t very helpful in making the decision to trigger these events – unless I’m misinterpreting something. Keep in mind, the above playground is obviously simple, but in my project I have multiple convex hulls interacting with each other.

When I get collisionEvent info in the collision callback, it tells me the basics about who hit whom, and where, and the normal. It also has a distance field, which seems to be how far into the plane of intersection the contact point is. I have been trying to use Math.abs(distance) as an indicator of the seriousness of the collision, but it’s not always reliable. Sometimes the distance field seems almost random. Sometimes a mathematically deep intersection with a large distance (greater than 1.0, say) only looks like a gentle brushing against each other on the screen. And conversely, sometimes a strong visual collision on the screen only shows a small distance value. This leads to me playing big crashing sounds with apparently nothing is happening on the screen, OR playing no sound at all when a visually interesting collision is happening on the screen.

So my question is: is there another way to interpret the collisionEvent info so that I can reliably make decisions on what is an important collision vs. an unimportant one? And also, is it possible in the future to get more information from the Havok plugin about the nature of the collision? For example, I’d love to know the linear and angular velocities of bodies A and B at the time of impact and also how those velocities will change after the impact. Thanks for any feedback!

edit: I should mention I am only requesting collision callbacks for hk._hknp.EventType.COLLISION_STARTED.value but still I seem to get many collisions for the same body in a single frame. Maybe because the bodies are overlapping a bit? Is it firing one callback for each vertex of the convex hull that is intersecting? Also, when I call hitterBody.getLinearVelocityToRef() at the time of collision, the returned velocity and the distance field are rarely in agreement about how serious the collision is. (Sometimes the distance is small while the velocity is large and vice versa)

The impulse applied to solve the contact constraint should give you an idea of how hard/fast two bodies collide. This is what you’d normally use to determine things such as impact sounds.

There seems to be a bug with fetching the applied impulse from the plugin. It seems to be zero no matter the severity of the collision.
Changing the following line:

eventOut.impulseApplied = floatBuf[offB + 13 + 2];


eventOut.impulseApplied = floatBuf[offB + 13 + 3];

Appears to give, in my opinion, pretty accurate results:

EDIT: @Cedric

1 Like

Oh yeah that’s waaaay better! I assumed the impulse was always zero because I hadn’t applied any impulse from my side. Can’t wait to try this out and see if it helps

PR added


I’ve pinged Havok’s engineer to do a review.

So, I have a case that requires a similar collision trigger (not based on impulse) and the example given in the OPs plaground is almost exactly what I need, except that when you uncomment “continued” and “finished,” the console only logs "started"s and "continued"s, with no "finished"s in between. Am I missing something? I’d really like to have the “finished” action trigger an event.

PG (clone of OPs, but with the continued/finished enabled): Babylon.js Playground

(Also, forgive me if this should be an entirely new question; I thought it was enough of a piggyback and related enough to this concept that it would make more sense to have it answered in this thread.)

Oh, previously there was a bug where collision ended events weren’t being reported, but it was fixed, but to not interfer with existing code, we added the collision ended events on a separate plugin observable: Collision Finished Issue | Babylon.js Playground (babylonjs-playground.com)

Currently there’s no corresponding collision ended observable on the body itself, but we can add it if needed :slight_smile:


Your solution totally works for my purposes.

Thanks so much for the quick and insightful help!

1 Like