Floof Posted November 14, 2014 Share Posted November 14, 2014 Hi everyone ! I am currently working on this Babylonjs level http://www.peru-lab.com/quetzal/bishopAnd I would like to add a jump fonction for the player, using the left mouse click for example. Can you help me please ? Quote Link to comment Share on other sites More sharing options...
Dinkelborg Posted November 14, 2014 Share Posted November 14, 2014 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 });} WombatTurkey 1 Quote Link to comment Share on other sites More sharing options...
Floof Posted November 14, 2014 Author Share Posted November 14, 2014 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 Quote Link to comment Share on other sites More sharing options...
Dinkelborg Posted November 14, 2014 Share Posted November 14, 2014 Did you create an impostor for the camera? And if you did, you could just create an invisible mesh and make it the camera's parent. Quote Link to comment Share on other sites More sharing options...
joshcamas Posted November 14, 2014 Share Posted November 14, 2014 Cool world btw! Quote Link to comment Share on other sites More sharing options...
Floof Posted November 17, 2014 Author Share Posted November 17, 2014 Thanks Joshcamas for your advice and your support. The jump works now using space bar But I can't stop sliding on the mountains, because it's not a flat "ground", I suppose..Do you think I can fix that ? http://www.peru-lab.com/quetzal/bishop2 Quote Link to comment Share on other sites More sharing options...
joshcamas Posted November 17, 2014 Share Posted November 17, 2014 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... Quote Link to comment Share on other sites More sharing options...
Dinkelborg Posted November 17, 2014 Share Posted November 17, 2014 Thanks Joshcamas for your advice and your support.So it seems joshcamas gave you a solution, why would you not share it here? Quote Link to comment Share on other sites More sharing options...
joshcamas Posted November 17, 2014 Share Posted November 17, 2014 I think he might have been confused and read your post Dinkel, and thought it was me. :S Quote Link to comment Share on other sites More sharing options...
Dinkelborg Posted November 18, 2014 Share Posted November 18, 2014 lol ... I got confused by his confusion then joshcamas 1 Quote Link to comment Share on other sites More sharing options...
Floof Posted November 19, 2014 Author Share Posted November 19, 2014 Sorry for the delay guys...Thank you very much for your answer. Maybe sliding is not a big problem finally... The most important thing is the jump,and it works Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.