Forces
How to use it
Neither forces nor impulses have any effect on bodies whose mass is 0.
import { Vector3 } from "@babylonjs/core/Maths/math.vector";body.applyForce(new Vector3(0, 10, 0), // direction and magnitude of the applied forcenew Vector3(0, 0, 0) // point in WORLD space where the force will be applied );// .... OR .....body.applyImpulse(new Vector3(0, 10, 0), // direction and magnitude of the applied impulsenew Vector3(0, 0, 0) // point in WORLD space where the impulse will be applied );As a reminder, if a body's transform node contains Thin Instances, you can choose which instance to apply the force/impulse to by passing instanceIndex as a parameter:
import { Vector3 } from "@babylonjs/core/Maths/math.vector";// Apply a force to the first instance ONLYbody.applyForce(new Vector3(100, 0, 0), new Vector3(0, 0, 0), 0);If no index is provided, the force/impulse is applied to all instances.
Difference between a force and an impulse
A force is a continuous effect that is applied to an object over time, which can change the object's velocity or direction of motion. For example, a force could be used to simulate gravity, wind resistance, or a player pushing an object.
An impulse, on the other hand, is a sudden, instantaneous effect that changes the velocity of an object. It is a specific amount of force applied over a very short duration of time, often modeled as a single frame in a game. For example, a collision between two objects might generate an impulse that changes the direction and speed of both objects.
In summary, a force is a continuous effect over time, while an impulse is a sudden, instantaneous effect that changes the velocity of an object. Click on the sphere in the following examples to experiment with forces and impulses.
applyForce applyImpulsePhysics Helper
The helper assists you with creating some phenomena like radial explosions or vortices.
import { Vector3 } from "@babylonjs/core/Maths/math.vector";import { PhysicsHelper, PhysicsRadialImpulseFalloff, PhysicsUpdraftMode } from "@babylonjs/core/Physics/physicsHelper";var physicsHelper = new PhysicsHelper(scene);var origin = Vector3(0, 0, 0);var radius = 10;var strength = 20;var falloff = PhysicsRadialImpulseFalloff.Linear; // or BABYLON.PhysicsRadialImpulseFalloff.Constantvar explosionEvent = physicsHelper.applyRadialExplosionImpulse(// or .applyRadialExplosionForceorigin, radius, strength, falloff);// the second `radius` argument can also act as options: `.applyRadialExplosionImpulse(origin, { radius: radius, strength: strength, falloff: falloff })`// orvar gravitationalFieldEvent = physicsHelper.gravitationalField(origin, radius, strength, falloff);// the second `radius` argument can also act as options: `.gravitationalField(origin, { radius: radius, strength: strength, falloff: falloff })`gravitationalFieldEvent.enable(); // call this if you want to activate the gravitational fieldsetTimeout(() => gravitationalFieldEvent.disable(), 3000);// orvar updraftEvent = physicsHelper.updraft(origin, radius, strength, height, PhysicsUpdraftMode.Center // or BABYLON.PhysicsUpdraftMode.Perpendicular);// the second `radius` argument can also act as options: `.updraft(origin, { radius: radius, strength: strength, height: height, updraftMode: PhysicsUpdraftMode.Center })`updraftEvent.enable();setTimeout(() => updraftEvent.disable(), 5000);// orvar vortexEvent = physicsHelper.vortex(origin, radius, strength, height);// the second `radius` argument can also act as options: `.vortex(origin, { radius: radius, strength: strength, height: height, centripetalForceThreshold: 0.7, centripetalForceMultiplier: 5, centrifugalForceMultiplier: 0.5, updraftForceMultiplier: 0.02 })`vortexEvent.enable();setTimeout(() => vortexEvent.disable(), 5000);For a more detailed explanation, please take a look at the playground example below.
Physics helpers