Jump to content

[NewbieQuestion] My Box won't fall ( Gravity )


Drenghel
 Share

Recommended Posts

Hi Babylonians !

I'm just getting started in this new beatiful that is WebGl and BJS, I'm going through the documentation/ tutorials / Demos for the last two days but I can't find a way to make my mesh fall.
It works with the free camera, the collisions also works.

Here's a snippet of relevant code I wrote, tell me what I forgot please. I don't think I forgot to include any physic library ( since 2.1 it seems to be packed with BJS ) but I may be wrong and the tutorials done with the older version confuse me a bit :(


 

				var scene = new BABYLON.Scene(engine);				scene.gravity = new BABYLON.Vector3(0, -0.01, 0);				scene.workerCollisions = true;				scene.collisionsEnabled = true;				var sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);				sphere.position = new BABYLON.Vector3(5, 2, -5);				sphere.applyGravity = true;				sphere.ellipsoid = new BABYLON.Vector3(0.5, 1.0, 0.5);				sphere.checkCollisions = true;				var ground = BABYLON.Mesh.CreateGround("ground1", 100, 100, 2, scene);				var groundMaterial = new BABYLON.StandardMaterial("groundMaterial", scene);				groundMaterial.diffuseTexture = new BABYLON.Texture("grass.bmp", scene);				ground.material = groundMaterial;				ground.checkCollisions = true;				var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(-10, 2.5, 0), scene);				camera.setTarget(sphere.position);				camera.keysUp = [90];				camera.keysRight = [68];				camera.keysLeft = [81];				camera.keysDown = [83];				camera.checkCollisions = true;				camera.applyGravity = true;				//Set the ellipsoid around the camera (e.g. your player's size)				camera.ellipsoid = new BABYLON.Vector3(1, 0.3, 1);				camera.attachControl(canvas, false);

Thanks a lot in advance, I feel stupid not to find what's wrong, plealse "unstupidify" me :D
( And yeah frenchie here so weird english ^^' )

Link to comment
Share on other sites

Heya!

 

Welcome :)

Actually, the physics engine is not shipped with Babylon... It's the way to use it (called a plugin)!

I'm not a pro for the collision engine in Babylon, but I think that if you want meshes to use gravity, you have to use a physics engine (cannon or oimo as you wish).

 

However, you can use 'moveWithCollision': it is this method that uses ellipsoid and so on.

 

For next time, and in order to help you more easily, you can try to reproduce your problem in the playground. This way, we can update it at will, test our solution and get back to you quicker :)

Good luck!

Link to comment
Share on other sites

Hi! always nice to have fresh users on their quest for the next webgl game :-)

There are two ways to use collisions and physics in babylon. It has a native collision system, that supports gravity on free cameras only. It also supports mesh-to-mesh collision when you wish to avoid meshes "merging" into one another. As Temechon said, the moveWithCollisions function of any mesh will move it in a specific direction until it collides with a different mesh. you can see a gravity simulation in this playground - http://playground.babylonjs.com/#NVWUF . What I am doing is simply applying the scene'S gravity constantly to the box on each frame. But this is not the way the collision detection system was meant to be used. It is actually more for the camera and less for meshes. To prevent from going through walls and such.

To allow meshes physically colliding with one another using mass and friction and other fun things, you will need one of the two physics engines Babylon supports. Here is almost the same playground with Oimo physics turned on - http://playground.babylonjs.com/#NVWUF#1

Try understanding what'S happening here. I think I will soon write an updated tutorial about the collisions system and physics

On a side note - I am not sure why we can't add the gravity support to the meshes, let me have a look :-). Maybe 2.2 will include moving meshes with gravity as well.  

Link to comment
Share on other sites

Actually, the physics engine is not shipped with Babylon... It's the way to use it (called a plugin)!

I'm not a pro for the collision engine in Babylon, but I think that if you want meshes to use gravity, you have to use a physics engine (cannon or oimo as you wish).

 

However, you can use 'moveWithCollision': it is this method that uses ellipsoid and so on.

 

For next time, and in order to help you more easily, you can try to reproduce your problem in the playground. This way, we can update it at will, test our solution and get back to you quicker :)

Good luck!

 

It's the "Physics engine (thanks to oimo.js)" that made me think that, but I get it know ^^ and I'm not used to something like oimo or cannon being so well integrated, I thought of it like another completely different library .

I will show you it on a play ground next time, good piece a advice :)

 

Hi! always nice to have fresh users on their quest for the next webgl game :-)

 

There are two ways to use collisions and physics in babylon. It has a native collision system, that supports gravity on free cameras only. It also supports mesh-to-mesh collision when you wish to avoid meshes "merging" into one another. As Temechon said, the moveWithCollisions function of any mesh will move it in a specific direction until it collides with a different mesh. you can see a gravity simulation in this playground - http://babylonjs-playground.azurewebsites.net/#NVWUF . What I am doing is simply applying the scene'S gravity constantly to the box on each frame. But this is not the way the collision detection system was meant to be used. It is actually more for the camera and less for meshes. To prevent from going through walls and such.

To allow meshes physically colliding with one another using mass and friction and other fun things, you will need one of the two physics engines Babylon supports. Here is almost the same playground with Oimo physics turned on - http://babylonjs-playground.azurewebsites.net/#NVWUF#1

 

Try understanding what'S happening here. I think I will soon write an updated tutorial about the collisions system and physics

 

On a side note - I am not sure why we can't add the gravity support to the meshes, let me have a look :-). Maybe 2.2 will include moving meshes with gravity as well.  

 

This, this is what I needed to know !

That physic engine will be really helpful ! To be honest my first project is to recreate a simple game based on Pachinko, I hardly imagine that without a proper physic simulation ^^

I'll dissect that playground right-away, and try to find some doc for the engine ( now that I know the name of the methods, should be a piece of cake :) )

 

EDIT :

Any advice about Oimo versus cannon ?

---

Anyway thanks to both of you, I'll set this topic on resolved soon, just need the time to be sure I won't get stuck while writing the first 5 lines ^^

Regards Drenghel

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...