Using density and an Overview of physics properties

Hierarchy: mesh.physicsBody.shape.material

BODY

  • .setMassProperties() - sets user properties that override calculated values:
    – mass, inertia, centerOfMass, inertiaOrientation
  • .getMassProperties() - gets resultant properties, calculated then overridden
  • .setLinearDamping(linDamp)
  • .setAngularDamping(angDamp)
  • .setGravityFactor(gravFact)

SHAPE

  • .density
  • other shape properties: .filterCollideMask, .filterMembershipMask, .isTrigger

MATERIAL

  • .friction
  • .staticFriction
  • .restitution
  • .frictionCombine
  • .restitutionCombine
    – PhysicsMaterialCombineMode:
    • ARITHMETIC_MEAN, (a+b)/2
    • GEOMETRIC_MEAN, sqrt(aa + bb)
    • MAXIMUM (default restitution),
    • MINIMUM (default friction),
    • MULTIPLY

Using a PhysicsAggregate appears to disable calculating mass from density, even if mass is not a part of the passed parameters. Instead, use PhysicsBody directly. Shape.density must be defined/assigned before assigning the shape to physicsBody. Alternatively, assign the shape again after modifying/setting density. The default density is 1000 (i.e. 1000 kg/m^3).

mesh.physicsBody.shape.density = 1.18 / 1000 // g/cm^3
mesh.physicsBody.shape = mesh.physicsBody.shape // force recalc of mass from shape and density

To create a PhysicsBody directly, first get the shape, then create a PhysicsBody instead of a PhysicsAggregate:

const defaultPhysicsShapeType = BABYLON.PhysicsShapeType.CONVEX_HULL // or MESH

const shape = new BABYLON.PhysicsShape( {type:defaultPhysicsShapeType,parameters:{mesh:mesh}}, scene );
new BABYLON.PhysicsBody(mesh,BABYLON.PhysicsMotionType.DYNAMIC,false,scene).shape = shape

shape.density = 2 // example of changing the density
mesh.physicsBody.shape = mesh.physicsBody.shape // to recalculate mass from mesh and density

If you scale the mesh, you’ll have to generate a new shape (and dispose the old one). Just generate the new shape and assign it to your existing body:

mesh.physicsBody.shape = newShape

Hopefully this will save you time by not having to figure these details out. If I got anything wrong, please let me know!

5 Likes

Thank you! Bookmarked!