Jump to content

Floof
 Share

Recommended Posts

You should integrate a physics plugin like Oimo.js (if you downloaded Babylon from GitHub you have it already)
This will allow you to create physics impostors for your objects, in your HTML file you will have to add something like: <script src="js/your_subfolder/Oimo.js"></script>
Then inside of your Javascript you have to add:

scene.enablePhysics(new BABYLON.Vector3(0, -20, 0), new BABYLON.OimoJSPlugin());

at your create scene funcion and now for ever Object that should be influenced by gravity (like your camera or any mesh) you have to create a physics impostor like this:

bouncy_ball.setPhysicsState({ impostor: BABYLON.PhysicsEngine.SphereImpostor, mass: 1 });

It will immediately have physics and collide with other objects that have an impostor, if you set mass to 0 the object will still have physics and be able to collide with others, but it will be static (so not infected by gravity).

 

finally, to make your object jump, you can give it an impulse, if it has an impostor, like this:

var force_vector = new BABYLON.Vector3(0, 20, 0);        bouncy_ball.applyImpulse(force_vector,bouncy_ball.position);

One other tip: if you want to move your object by code, so for example let's say you have a character controller class and you try something like:

var onKeyDown_A = function(){player.position.x -= 0.1;}

this won't work if a physics impostor is controlling your object, but you can temporarily deactivate the impostor like this:

var onKeyDown_A = function(){    player.setPhysicsState(BABYLON.PhysicsEngine.NoImpostor);    player.position.x -= 0.1;    player.setPhysicsState({ impostor: BABYLON.PhysicsEngine.SphereImpostor, mass: 1 });}
Link to comment
Share on other sites

thanks for your answer.

It seems that it's only working on meshes.

With the camera object, it's not possible to call setPhysicsState, with the camera object, which method should I call to apply a physical state? (I'm trying to do a FPS sort of level where the player can jump). Thanks

Link to comment
Share on other sites

I had the same problem, and it's kinda a hard one.

 

One thing you can try is saving a variable of the location of player, then applying gravity. Then compare the distances, and if the player should be standing, (aka not pressing buttons) and the distance is small, make the location the old one.

 

The only problem with this way is often you slide when you start pressing arrow keys again...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...