Threedy

Cannonjs: sphere doesn't fall down

Recommended Posts

Edit:

Problem fixed. After some experimenting, I realized that the gravity should be passed like this: world.gravity = vector.

 

Hi,

I have the following simple example from the wiki page of Cannon JS... I expected the ball to fall, but it didn't and only prints ''Sphere position: 0,100,10''. Can anyone understand why? I then thought (based from the wiki) that I had to use Rigidbody (CANNON.RigidBody) but that wasn't recognized. 

Any tip is greatly appreciated.

Quote

    // Setup our world 
    var world = new CANNON.World({
       gravity: new CANNON.Vec3(0, -10,0) // m/s² 
    });
     
    // Create a sphere 
    var radius = 1; // m 
    var sphereBody = new CANNON.Body({
       mass: 5, // kg 
       position: new CANNON.Vec3(0, 100, 10), // m 
       shape: new CANNON.Sphere(radius)
    });
    world.addBody(sphereBody);
     
    // Create a plane 
    var groundBody = new CANNON.Body({
        mass: 0 // mass == 0 makes the body static 
    });
    var groundShape = new CANNON.Plane();
    groundBody.addShape(groundShape);
    world.addBody(groundBody);
     
    var fixedTimeStep = 1.0 / 60.0; // seconds 
    var maxSubSteps = 3;
     
    // Start the simulation loop 
    var lastTime;
    (function simloop(time){
      requestAnimationFrame(simloop);
      if(lastTime !== undefined){
         var dt = (time - lastTime) / 1000;
         world.step(fixedTimeStep, dt, maxSubSteps);
      }
      console.log("Sphere position: " + sphereBody.position);
      lastTime = time;
    })();

 

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.