# Realistic freefall

I’ve been looking at various BJS examples (e.g. https://playground.babylonjs.com/#3I55DK#0) and freefall appears unrealistic as the acceleration is too low with standard physics library calls. I want to create a previsualisaton tool for freefalling objects in BJS - how do I get realistic 9.81 m/s^2 acceleration with the physics library?

@Cedric can probably help with the physics part

When enabling physics, the first parameter is the gravity acceleration.

``````scene.enablePhysics(new BABYLON.Vector3(0,-12,0), new BABYLON.AmmoJSPlugin());
``````

Here, the gravity is 12m/s^2. You can obviously set it to any other value.

Thanks sebavan.

@Cedric I’m interested in producing a simulator for a camera control system which would allow a user to preview the camera’s view of an object freefalling from rest in front of a graphic background, then hitting a surface. The camera would be assigned a motion profile which would be something like a 1 g initial acceleration followed by a >= 1g deceleration. I can’t find a BJS freefall example which looks realistic - the acceleration looks like it’s much less than 1 g. Why is this, and is there anyway to sort this out with the standard physics module, or do I need to implement my own p(t) = 0.5 * 9.81 * t^2 code?

I’ve just changed the default value of 12 m/s^2 to 9.81 m/s^2 in the example https://playground.babylonjs.com/#3I55DK#0, but the objects appear to be ‘freefalling’ which much lower acceleration than real life. Why is this?

If you compare physics with math result, you get the same curve values (with a small difference)

freefalling | Babylon.js Playground (babylonjs.com)

In the demo, the value has been set to have a pleasant rendering. Friction could be tweaked as well. It’s not meant to be physicaly accurate but nice to watch.

Physics engine will trade accuracy for realtime performance. If you need exact freefalling and depending on your scene (do you need collision/response ?), computing freefalling height might be better.

I haven’t looked at the physics source yet, but is there some scale factor being applied to the acceleration calculation to visually reduce it? When I use 98.1 m/s^2 in the example https://playground.babylonjs.com/#3I55DK#0, it starts to ‘look’ more realistic. I was hoping to use the built in physics engine as it’s got all the collision stuff built in. I want to end up with pos/vel/acc/time units and rendering/visual effect which are realistic: e.g. a 0.01 unit dia. sphere freefalling at 9.81 units/s^2, covering the expected 4.905 units in the first 1 second (s = 1/2at^2) and looking like the rate of freefall would for a realworld object.

There is no unit notion in the engine. If unit in your scene is cm, then set physics gravity to -981.
In the case of this PG, everything is expected to be meter.

I understand that there are no realworld units in BJS, but I’m trying to get my head around why physics gravity = -9.81 in the PG we’ve been discussing ‘looks’ way less that it does in reality. The units don’t matter, but the relationship between units (size and position), units/s (velocity) and units/s^2 (acceleration) should be consistent, and this is proved by your console.log in the above PG. What I don’t get is why the acceleration looks a lot less than real freefall when set to 9.81 units/s^2. Sorry if I’m being thick!

@Cedric

Is it possible to retain the collision etc. features of the physics engine whilst overiding an object’s position as a function of time?

That’s not how physics engine work. With their internal state, trying to keep working things while shutting down other will lead to undefined behavior (ie explodind meshes).
That said, it’s possible to use physics engine queries (ray cast, sweep sphere, …)
example, for a cannon bullet, compute parabolic curve as a function of time, estimate X,Z of end position and for that position, cast a ray to the ground to get the corresponding height.

Another approach: perhaps the realtime realism in the browser window isn’t important, but I would need accurate position against time data coming out of the physics engine to drive real motors. Considering an object free falling vertically from rest again, is there a way to query the physics engine to obtain the position as a function of time/frame?

What are you creating? Maybe you don’t need a physics engine.

2 Likes

I’m interested in producing a simulator for a camera control system which would allow a user to preview the camera’s view of an object freefalling from rest in front of a graphic background, then hitting a surface.

I guess I don’t need realtime preview in BJS - the action would complete too quicky. What I need is a slow mo preview in BJS, which is accurate with regard to the relative positions of the camera and falling object. Perhaps I then do a background calculation to generate the actual camera position against time data to output to the realworld camera motion control system?

(I’ve tried implementing a realtime BJS simulation, and the timing seems to be way off - not sure what I’m getting wrong: https://playground.babylonjs.com/#7ZSXFT#6)

Ohh, Wile E. Coyote from ground level! Needs a particle system plum of dust.

if sphere y = g, after 1 second, its height is:

g - 0.5 * g * time * time = g - 0.5 * g * 1* 1 = g - 0.5 *g = 0.5 g

it’s at t = 2^0.5 that the sphere will touch the ground. Which is what I get actually.

My bad! I’ve been confusing speed at time t with distance travelled…doh!

My calc matches yours: 2^0.5 = 1.414 units, which I get too (plus or minus a bit).