API

Forces

How To Use Forces

This section gives some terminology needed to discuss the use of forces in the three physics engines:

  1. Cannon.js;
  2. Oimo.js;
  3. Ammo.js

as well as Playground examples to check out the code. In the Playgrounds, the physics engine used can be changed by selecting which ones to comment out.

See How to Use The Physics' Engines for an overall view of setting up and using the three plugins.

Body

Solids in physics are often referred to as bodies. In the simulation, bodies are made up of two parts: the rendered object and the physics object. The rendered object is a mesh, and the physics object, which holds the data about the body, is called a physics impostor.

Note: a box impostor is often preferable when the body is a plane.

Mass

This is the amount of matter in the body and is set when the impostor is created. Static bodies should have a mass of zero.

import { PhysicsImpostor } from "@babylonjs/core/Physics/v1/physicsImpostor";
new PhysicsImpostor(mesh, PhysicsImpostor.BoxImpostor, { mass: 2 }, scene);

Forces

These are gravity, impulses, friction and applied forces.

Gravity

In the simulations, gravity is a universal force applied throughout the simulation, producing gravitational acceleration. Setting a Vector3 for gravity is, in fact, setting the gravitational acceleration. The default value is (0, -9.807, 0). Since it is a universal force, it is set in the physics engine either when it is enabled or later.

import { Vector3 } from "@babylonjs/core/Maths/math.vector";
import { AmmoJSPlugin } from "@babylonjs/core/Physics/v1/Plugins/ammoJSPlugin";
import { CannonJSPlugin } from "@babylonjs/core/Physics/v1/Plugins/cannonJSPlugin";
import { OimoJSPlugin } from "@babylonjs/core/Physics/v1/Plugins/oimoJSPlugin";
/*When physics is enabled use default gravity*/
scene.enablePhysics(null, new CannonJSPlugin());
scene.enablePhysics(null, new OimoJSPlugin());
await Ammo();
scene.enablePhysics(null, new AmmoJSPlugin());
//set gravity
scene.enablePhysics(new Vector3(0, -5, 0), new CannonJSPlugin());
scene.enablePhysics(new Vector3(0, -5, 0), new OimoJSPlugin());
scene.enablePhysics(new Vector3(0, -5, 0), new AmmoJSPlugin());
/*Get and set gravity*/
var physicsEngine = scene.enablePhysics(null, new CannonJSPlugin());
var physicsEngine = scene.enablePhysics(null, new OimoJSPlugin());
await Ammo();
var physicsEngine = scene.enablePhysics(null, new AmmoJSPlugin());
//Get gravity
var gravity = physicsEngine.gravity;
//Set gravity
physicsEngine.setGravity(new Vector3(0, -5, 0));
Gravity Example

Impulses

An impulse is a force applied to a body in an instant, which changes the current linear velocity and/or the angular velocity of the body. Impulses acting at the center of mass of the body will not change the angular velocity. Unless other forces act on it, the body will continue with the new velocities.

An impulse is applied to a body's physics impostor.

Applying an impulse requires a vector giving the magnitude and direction of the impulse and the position vector of the contact point of the impulse. The contact point of the impulse is given in world coordinates.

import { Vector3 } from "@babylonjs/core/Maths/math.vector";
imposter.applyImpulse(impluse_vector, contact_vector);
let localRefPoint = new Vector3(x, y, z);
imposter.applyImpulse(ImpulseVector, mesh.getAbsolutePosition()); //impulse at center of mass
imposter.applyImpulse(ImpulseVector, mesh.getAbsolutePosition().add(localRefPoint)); //impulse at a local point

The following Playground is initially set up to apply an impulse at the center of mass vertically against gravity, which eventually returns the box to the ground. Leaving the friction at 0 and applying horizontal impulses shows the continuity of movement.

Impulses Example 1

Friction

Friction is a property of a body. It is set in the impostor and provides a continuous force between two bodies while they are in contact. You can set friction when creating an impostor and also get and set it later.

import { PhysicsImpostor } from "@babylonjs/core/Physics/v1/physicsImpostor";
new PhysicsImpostor(mesh, PhysicsImpostor.BoxImpostor, { mass: 2, friction: 0.4 }, scene); //on creation
var friction = imposter.friction; // get friction;
imposter.friction = 0.1; //set friction.

Since friction occurs at the boundary of a body, applying an impulse at the center of mass will produce an angular velocity as well as a linear velocity.

Revisiting the following Playground, setting friction on both bodies, and applying horizontal impulses will show the angular velocity effect.

Impulses Example 2

Applied Forces

An applied force only affects the body over the period during which it is applied, which is the duration of the frame interval. With zero friction, a sufficiently large force (to overcome inertia) applied in the first frame interval will set the body in motion. While Cannon.js and Ammo.js have a native apply-force method, Oimo.js does not, so applying a force is replaced internally in Babylon.js with the apply-impulse method, meaning a smaller value has a greater effect.

import { Vector3 } from "@babylonjs/core/Maths/math.vector";
//Force Settings
var forceDirection = new Vector3(1, 0, 0);
var forceMagnitude = 50;
var contactLocalRefPoint = Vector3.Zero();
impostor.applyForce(forceDirection.scale(forceMagnitude), mesh.getAbsolutePosition().add(contactLocalRefPoint));

The following Playground is initially set up with zero friction and applies an impulse at the center of mass horizontally in the X direction.

Applying Forces

Further reading

Using A Physics Engine
Learn how to use one of the available physics engines in Babylon.js.
Using A Physics Engine
Joints
Learn all about using joints in physics engines within Babylon.js.
Joints
Pivots and Axes
Learn all about physics pivots and axes in Babylon.js.
Pivots and Axes
Compound Bodies
Learn all about physics compound bodies in Babylon.js.
Compound Bodies
Soft Bodies
Learn all about physics soft bodies in Babylon.js.
Soft Bodies
Advanced Physics Features
Unlock the full potential of physics with advanced physics features in Babylon.js.
Advanced Physics Features