Ammo.btMultiSphereShape - How can this work?

Looking at the ammo.js physics plugin source code on github:

If you palce a debugger right at this point in the code and step into the ammo.js function call you’ll arrive at a function that looks like this:

        function sD(a, c, d) {
            zC();
            a && "object" === typeof a && (a = a.Fz);
            "object" == typeof c && (c = DC(c));
            d && "object" === typeof d && (d = d.Fz);
            this.Fz = Cl(a, c, d);
            h(sD)[this.Fz] = this
        }

As soon as the second line gets evaluated, a has been assigned the value undefined. So how can passing an array of Ammo.btVector3’s ever have any effect?

cc @Cedric and @RaananW our Physix experts

I’ve been doing some archeology here :slight_smile:

That change was related to this discussion : Use Rollup to bundle Babylon. but

And this PG that was failing before https://playground.babylonjs.com/#6QF3KP#3

And the fix is here :

2 Likes

I’m not quite sure I follow what you’re showing me there in github - I can see why some extra work was needed for ellipsoids versus spheres, but I’m wondering what Ammo.js does with the positions argument. Sorry if I’m missing the answer to that in these posts.

In the case of an ellipsoid I wouldn’t be surprised if Ammo.js is able to able to create a single shape which can than be positioned & scaled as needed; in the end you wouldn’t notice it ignoring the argument parameter positions. However are you able to see multiple spheres using bjsAMMO.btMultiSphereShape when positions contains more than one btVector3 objects?

What I’m trying to verify is if the spheres are positioned relative to each other correctly according to the positions array argument that gets passed in. I was having trouble playing around with this, and that is when I stepped through the code and realized that the argument for positions gets set to undefined in Ammo.js.

The positions are stored in m_localPositionArray

Did you try to change this line

const positions = [new this.bjsAMMO.btVector3(0, 0, 0)];

and see what it does with this pg ? https://playground.babylonjs.com/#6QF3KP#3

Is there a way I can make local edits to the babylon.js source code in a playground?

Edit: I ended up just stepping through the function calls and modifying the arguments inside the ammo.js multi sphere function call site:

What this is doing? I don’t really know. What I can say for sure is that the results of the physics sim are definitely different when I do this, versus when I don’t and let the playground run unmodified. You can try this if you want, by stepping through the code up to that function call returnValue = new this.bjsAMMO.btMultiSphereShape(positions, radii, 2); (line 142 in ammoJSPlugin.ts)

v3 = a[0].constructor; a.push(new v3(-1, 0, -1)); a.push(new v3(-1, 0, -1)); a.push(new v3(3,2,3)); c.push(1,2,3); d = 4;

If I run that modification on the Ammo arguments to each call that creates an asteroid, it seems to actually be creating larger objects, I just can’t tell if they exactly correspond to the vectors and radii I specified in those arguments. I suppose I could run my ammo debug drawer code as well in this playground.

However, back to the Ammo multi sphere constructor:

        function sD(a, c, d) {
            zC();
            a && "object" === typeof a && (a = a.Fz);
            "object" == typeof c && (c = DC(c));
            d && "object" === typeof d && (d = d.Fz);
            this.Fz = Cl(a, c, d);
            h(sD)[this.Fz] = this
        }

So as long as I make those modifications to the arguments (a, c, d) before the zC(); invocation happens, those extra arguments do seem to have an effect. I guess trying to make sense of wasm code from JS land wasn’t such a great idea.

So I went ahead and added in my debug drawer code to the pg you linked above:

Unless my debug drawer code has a bug, it doesn’t look like the ammo js shapes match the visual shapes being rendered.

Next, when I add in my argument modifications, it almost looks like ammojs is placing spheres within spheres:
image

To be honest, I’m not sure if my interpretation of what a multi sphere convex hull is supposed to look like is correct, but it wasn’t this.

Edit: Here’s what I was expecting -


image

I think the crux of the matter is that we need to be able to pass in an Ammo.js btVector3Array object for the positions argument, otherwise each sphere is centered on the local origin of the collision shape.

Yes, it makes sense!

1 Like