Native Function in AmmoJSPlugin

Here is a quick demo of using margins on soft bodies.
Default margin
Margin set to tube radius

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)

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

to bring up the prototype list

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


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


add this function to your logger

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

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


The next question is does it do anything.

Not if both are rigid bodies

so make the box a soft body

add margin to soft box

and then adding a margin to the ground as well does affect the distance between them

A margin just on the rigid body does not affect distance

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.