Entity Hit with ammo api

Hello to all developers!

I’ve been working with PlayCanvas for a while now, and I’m working on a game with a lot of physics. I’m getting to grips with the ammo.js API, which I’m not very familiar with.

Currently, I’m using the convexSweepTest() function, and I can retrieve the position and normal of the hit, but not the hit object itself.

Is it possible to retrieve the entity hit by a sphere or a capsule cast? Is there another function available?

The engine will attach the entity to a body it creates, so if the body was created by the engine, you can cast it to Ammo body and you will have access to it. For example:

if (resultCallback.hasHit()) {
    const body = Ammo.castObject(resultCallback.get_m_hitCollisionObject(), Ammo.btRigidBody);
    // body.entity 
}

Hi LeXXik,

Thank you for taking the time to reply!

With a callback like ClosestRayResultCallback or AllHitsRayResultCallback, yes, it’s possible to retrieve the hit object. But with a callback like ClosestConvexResultCallback, there’s a pointer to the hit object const btCollisionObject* m_hitCollisionObject; but I can’t retrieve the entity from this variable.

After checking with console.log(), the variable m_hitCollisionObject is not defined. It seems that it’s not possible to retrieve the hit object with convexSweepTest(). I’m going to perform a raycast with the retrieved information, such as the hitPoint and hitNormal.

If Ammo.ClosestConvexResultCallback.get_m_hitCollisionObject is undefined, then your Ammo version is too old. Simply get the latest from Ammo:

1 Like

Thanks! That was indeed the problem!

However, I imported the Ammo library via the editor, and apparently, it’s not quite the same since it doesn’t contain Ammo.ClosestConvexResultCallback.get_m_hitCollisionObject().

Is it normal for the editor to import an “older” version?

The version in Editor is rarely updated, so it is normal that it is some years old. You can safely use the recent one from Ammo repo.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.