Ammo physics .dispose() on impact

Hey Raggar, Givo, Nulier, Delkatosh, and
@Wingnut.

Long-time/first-time…

Thanks to everyone on this forum for your years of open exploration. I’m a welder by trade, a lit major by degree, and a visual learner by nature… I’ve been using BJS to learn coding. I know @Wingnut will want a simpler playground (“just put a cube on a ####ing plane you ass”) but this is what I have so far…my 2 year old calls it “bubbles.”

WASD SHIFT SPACE MOUSE WHEEL:
https://www.babylonjs-playground.com/#6MQB2X#147

Anyway, I’ve been having a problem disposing of items without the playground crashing. If you un-comment line 220, you’ll see what I mean. I made a trashy workaround, but if anyone could explain to me why dispose freezes the playground, that would be helpful. It worked until recently.

cheers,

Mulciber.

Welcome to the new forum!

The issue is that you are disposing of your mesh before calling it into other functions. When I run the scene, the playground doesn’t crash but I receive the error in the console:

VM58:222 Uncaught TypeError: Cannot set property ‘mass’ of null

.Before running the script, I expected to receive an error in setting the position of the collided mesh on line 221; however, the error appears in the next line 222. If you describe the behavior you want to see, then that would help.

Galen

1 Like

Hey @Galen,

Thanks for the warm welcome.
I made a box on a plane,
like I should have done:

https://www.babylonjs-playground.com/#7M8BDR

I’m running this in chrome,
and as soon as the mesh is disposed,
the playground freezes.

As far as what I’m trying to accomplish,
I’m trying to make an item you can pickup.

I want to turn it into a function so that I can make any mesh
disposable/pickable/dosomethingspecificable
if it interacts with another mesh.

Mostly I’m just trying to learn JavaScript,
and I can’t figure out why something like:

for (let i = 0; i < scene.meshes.length; i++){
if (scene.meshes[i]._position.y < -300
&& scene.meshes[i].id !== “hero”
&& scene.meshes[i].id
!== “ball”){
scene.meshes[i].dispose();
}

works,
while:

box.physicsImpostor.registerOnPhysicsCollide
(sphere.physicsImpostor,
function(main, collided) {
collided.dispose();
}
)

fails.

Thanks again.

Also, this is the error I’m getting in the console…

Uncaught abort(1). Build with -s ASSERTIONS=1

The problem arises because you are disposing of your mesh inside a function that is still operating on the mesh. Here’s a PG scene that works.

https://www.babylonjs-playground.com/#7M8BDR#3

If you want to dispose of the mesh, then you should dispose of the mesh after it falls off screen or some other behavior. I also modified this so that the cube falls off the sphere axis on this PG scene.

https://www.babylonjs-playground.com/#7M8BDR#4

Or this.

https://www.babylonjs-playground.com/#7M8BDR#5

Galen

That third playground has tons of character in the movement…awesome.
I imagine the box giving a little thumbs up after it finally kicks that ball off the stage.

Anyway, what you’re saying makes sense to me.
If I tell the box to do something when it hits the sphere
(registerOnPhysicsCollide),
all it knows how to do is hit something and ask
“are you the sphere?”
When I dispose the sphere,
the box looses faith,
spirals into an existential crisis,
stares into the void and realizes there is no sphere…
and the whole thing breaks down.

So, it doesn’t matter how I dispose
(xyz-quanternion-euler-beta-alpha-omega)
the sphere.
If I’ve told the box to look for it,
the program will breakdown when it’s disposed().

Maybe I can make an array of the mesh.id’s and tell the function to run, only if the sphere exists in the array…

I’ll work on that.

I,
now understanding what went wrong,
feel extra thankful @Galen.
It should have been obvious,
but you still took the time.

cheers.

1 Like

I’ve never heard anyone describe a scene so poetically. Based upon what you described, you might want to consider disposing of a mesh based upon the mesh existing within the camera view. To do this, check out code posted by @Pryme8:

var sphere = BABYLON.Mesh.CreateSphere(“sphere1”, 16, 2, scene);
var projectionMatrix = scene.activeCamera.getProjectionMatrix();
var frustumPlanes = BABYLON.Frustum.GetPlanes(projectionMatrix);
console.log(sphere.getBoundingInfo().boundingBox.isInFrustum(frustumPlanes));

Once a mesh leaves the camera view, then perhaps dispose of the mesh.

Galen

3 Likes

Haha…my English degree didn’t help with grammar or a job,
so I’ve got to use it for something.
Also, I just tend to think of things in terms of narrative.
I’ll check out those posts by @Pryme8.
I bet I could definitely get lost in the frustumPlanes.

As far as disposal on Impact goes,
thinking of it is as a narrative helped me figure it out:

When the box can’t find what it was born to look for, it looses faith,
but a disposed() sphere asks no questions…

So now the sphere looks for the box,
and once is it finds it, never talks.

https://www.babylonjs-playground.com/#3XRG8A#5

Thanks again @Galen, this is a pretty awesome community so far.