Gravity not affecting camera until a key is pressed

Hi,
I made a super simple example of a camera with controller and physics.
Why the gravity is not affecting the camera until there is at least an input from the keyboard?
How can I have the camera automatically fall to the ground on scene load?

Thanks.

cc @Cedric

camera._needMoveForGravity = true;

I believe there is another way but I can’t put my fingers on it.

1 Like

Thanks a lot, it did the trick.

Now I have another issue. when I try to load my models into the scene the gravity is not working anymore

let scene = new BABYLON.Scene(this.engine)
await BABYLON.SceneLoader.AppendAsync("./models/", "world.babylon", scene)

The loading happens without any issues and I see the models in the scene but gravity on the camera is not working anymore even if everything (camera/ground/etc) is created after the load is complete. And also trying to load the models after the scene is completely initiated makes no difference.

Any idea why?

absolutely no idea.
Can you repro in a playground?

Ok, I just made the createScene function async and added:

await BABYLON.SceneLoader.AppendAsync("https://www.claudiodallabernardina.com/works/models/", "world.babylon", scene)

Scene load fine but if I try to create the ground as before now I get this error:

this._scene.getUniqueId is not a function

So i commented out the ground creation. Anyway the issue is there, when I load the scene the gravity on the camera is not working anymore.

This is because you have this.scene instead of scene in your createGround function

also, add:

scene.gravity = new BABYLON.Vector3(0, -0.9, 0);

You are my man! Thanks a lot.
Is there a logical explanation why without loading any asset the scene gets gravity automatically where after the assets are loaded you need to specify it?

In my code I have: scene.enablePhysics(gravityVector, physicsPlugin)
I thought it was assigning that gravity Vector automatically.

I guess scene.gravity is overriden somewhere.
scene.enablePhysics …well, enable physics with a physics engine.
camera collision is another beast that do not share gravity with physics engine.
It’s a totally different feature of the engine.

I removed the physics engine.
So now I see that if I add the scene.gravity = new BABYLON.Vector3(0, -0.9, 0) before using AppendAsync it does not work while after it does. So as you said it get overridden somewhere.

So my question now is: what if I load another model later on using AppendAsync? Do I need every time to reset the scene.gravity?

Is there a better way to load .babylon files at runetime?

Your .babylon file probably contains a gravity property. Would it be feasible to change it there?

I didn’t know there was one, I’m exporting from 3ds max and I found the Collisions panel in the general Babylon Properties which does have the Gravity Vector.
Maybe the importer could have a property to say “ignore gravity” when loading an external scene?
Is there already?

Thanks a lot.

if it is defined in the .babylon file it will be applied. Adding flags for everything will make the loader very hard to maintain :slight_smile:

What you can do is imply remove it, or set it to the value you want. A simpler solution would be to set the gravity after the .babylon file is loaded (in the onSuccess callback, or after the promise has resolved, depending on the function you use).

Thanks, what you say makes sense.
I’m setting it after the file is fully loaded.
My project has dynamic loading of resources happening so I need to re-set it when loading new models.

Is the .babylon extension supposed to be used for scenes or it makes sense to use it also for single models that I’ll dynamically load later on?

What is considered the best format for production? a binary one like glb?

Sorry if my questions sound dumb :slight_smile:
Thanks.

the .babylon format was created to serialize an entire scene, including everything around it, including gravity and collisions. You can technically load only a part of it (using the ImportMesh function, providing the name of the mesh you want to load), but you will still download the entire file in order to load a single mesh.

There are other formats - glTF (and its binary sibling glb) are great formats! you can see if it makes sense for you to use them. it depends on the tool exporting your meshes and what you want to export

1 Like