Gravitational Field falloff


I have been looking at the PhysicsHelper class and there is gravitationalField which returns a PyhsicsGravitationalFieldEvent PhysicsGravitationalFieldEvent | Babylon.js Documentation
however this uses PhysicsRadialExplosionEventOptions as optioins and the falloff only has Constant and Linear. To simulate a true gravitational field it would need a falloff of distance^2… or to be more general and allow different gravities, be able to specify the falloff power.
It seems that the gravitationalField is implemented as a repeating series of radial explosions so I am not sure if this is an easy add or not.

1 Like

My experience with the PhysicsHelper’s gravitationalField was similar to yours.

I ended up calculating the gravitational forces (e.g., the field) separate from the physics engine, then applying the force via applyImpulse. Seems to work really well, actually – here is a PG example of what I’m talking about. Line 107 is where the forces are summed up (there are only two in this sample, but see my GH for an example showing arbitrary numbers of massive bodies), with code like this:

    let f = this.applyGravityImpulseToBox(;
    this.testBox.physicsImpostor.applyImpulse(f, this.testBox.absolutePosition);

Note there may be minor mathematical errors (order of operations IIRC) in the above PG; I can’t recall whether I resolved those in the linked version or not.


Thanks @jelster
That sounds like a better and more manageable approach, I will look at doing similar.
It will also avoid the craziness that happens in the inspector when you a gravitationalFieldEvent - lots of radial explosions being created and destroyed.
Nice example PG too, BTW.

1 Like

Adding @Cedric and @RaananW FYI

That is a very cool PG and a really creative way to do that simulation on existing physics engine. Thanks for sharing :trophy:

I can see how order of operations can affect the net result, but since you take mass into account should be reasonable!! I wrote a physics simulator and wrote it so the order of applying forces like gravity, drag, springs (forces of attraction) could be applied dynamically and in any order, but the order within those did affect results as well especially how an initial state was created if generated from a graph. Fixed time steps were used to make the integration easier to solve. I did an experiment on this recently and ended up relying on Barnes Hut algorithm as a data structure:
Barnes–Hut simulation - Wikipedia

If equilibrium is reached the simulation can stop or be checked less frequently (or new bodies are added, etc.).

I needed a specific QuadTree that stored mass and centerOfMass and that was able to quickly make calculations as well as providing the opportunity to massively reduce math operations in some scenarios - considering the tree needs to be regenerated on each frame. That tree is here:
wammo/quadTree.ts at main · brianzinn/wammo (

I had wrote a physics plugin for BabylonJS as well that uses that force directed graph project and certainly for n-body simulations especially in space it is specialized. I didn’t have time to take it further, but it would be cool to get that production ready one day! :slight_smile:

1 Like

Interesting! Thanks for sharing and thanks for your kind words!

When I mentioned order of operations I was referring to the actual JavaScript expressions being mathematically correct, but when considering multiple sources of external forces the beauty is, as Newton laid out, it’s the sum of all the vector forces that ends up mattering, the properties of addition make the order irrelevant :slightly_smiling_face:

Using a tree structure is something I’d initially looked at and discarded because I didn’t think I needed to take on the complexity for my rather trivial usage, but I can see it’s usefulness in more complex scenarios, e.g., taking the planets “off-rails” and performing a real N-body simulation!

My [GravWell](https://GravWell.liquid project (GH repos) took this to a bit of an extreme, and starting fresh today I might take a different range of approaches, but it works!

1 Like