Native Function in AmmoJSPlugin


#1

Here is a quick demo of using margins on soft bodies.
Default margin https://www.babylonjs-playground.com/#8WC6ZN#8
Margin set to tube radius https://www.babylonjs-playground.com/#8WC6ZN#9

After doing the demos I realised that I knew about setting margins with soft bodies but not how to do it on rigid bodies and generally knew only a little about what is available with Ammo.js. I thought it might be useful to show how to find out about native functions. As I have been working with Ammo recently I will restrict my observations to Ammo.js but the principals apply to the other two as well.

A margin is set using (would you believe it) setMargin so let’s look at setMargin by going to Bullet Collision Detection & Physics Library: Class Members (since Ammo is built from Bullet this is the best place, I have found, for checking out Ammo stuff). Jump to the ‘s’ page and do a ‘Find in This Page’ search for setMargin

This lists all the members to setMargin applies to. How do we find where and when to apply it? Wel we can see that it applies to a box shape so create a box.physicsImpostor and use console.log(box.physicsImpostor.physicsBody)

https://www.babylonjs-playground.com/#YUNAST#14

In the console (for AmmoJSPlugin) you get an object with a ref number that you click on
image

to bring up the prototype list
image

which can expand to give a list of properties and functions that apply to a physicsBody.

image

scroll down this list to see what is available and you come across getCollisionShape which was one object setMargin applied to

image

add this function to your logger

console.log(box.physicsImpostor.physicsBody.getCollisionShape()) and check the console and expand the results to find

![image|289x444](upload://uTGU55rfz4wj7s3oxp1qitzozTw.png
that setMargin is indeed listed (note the majority of functions in Bullet are implemented in Ammo but not all so it is always worth checking functions in the console as deep as possible)

So to set the margin on a box.impostor use

box.physicsImpostor.physicsBody.getCollisionShape()

The next question is does it do anything.

Not if both are rigid bodies

https://www.babylonjs-playground.com/#YUNAST#15

so make the box a soft body https://www.babylonjs-playground.com/#YUNAST#16

add margin to soft box https://www.babylonjs-playground.com/#YUNAST#17

and then adding a margin to the ground as well does affect the distance between them https://www.babylonjs-playground.com/#YUNAST#18

A margin just on the rigid body does not affect distance https://www.babylonjs-playground.com/#YUNAST#19

Further experimentation seems to show that the two margins (soft and rigid) have to be the same number for both to have a reasonable affect.