Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Friction disappears when manually applying gravity #224

Open
josephrocca opened this issue Aug 15, 2015 · 5 comments
Open

Friction disappears when manually applying gravity #224

josephrocca opened this issue Aug 15, 2015 · 5 comments

Comments

@josephrocca
Copy link

I have a feeling I'm missing some fundamental understanding here, but when I edit the force on an object manually (e.g. by setting body.force.y += ... to manually apply gravity), the body suddenly loses all friction.

Is this normal behavior? I should note that I'm not using preStep, but am instead simply adding the forces before I call world.step(). I could rearrange my code structure to use preStep but it would be quite inconvenient.

For reference, here's how I'm manually applying gravity:

   var g = this.settings.gravity;
   for(var i = 0; i < this.world.bodies.length; i++) {
      var b = this.world.bodies[i];
      if(b.type === CANNON.Body.DYNAMIC) {
         b.force.y += b.mass*g;
      }
   }
   //then...
   this.world.step(this.settings.timeStep);

Thanks! Any hints at all would be appreciated.

@schteppe
Copy link
Owner

Are the bodies in contact? Maybe they lose the contact when you apply the force?

@josephrocca
Copy link
Author

Thanks for the quick reply man :) Yep, they're in contact. It's a bunch of cubes that fall onto a heightfield. When I apply gravity using world.gravity they fall, bounce and grind to a halt. When I manually apply gravity, they fall, bounce and slide around for a fair while before eventually settling in the low points of the heightfield.

If there's nothing that immediately comes to mind I'll do some more testing and report back.

@schteppe
Copy link
Owner

Oh, I know now.

When the friction is applied, the max friction force (mu * normalForce) is approximated with mu * mass * gravityVector.length(). When gravity is zero, then the max friction force is also set to zero.

I solved this problem in p2.js by adding a .frictionGravity property to the world, that you can set to a custom value that will override gravityVector.length() when calculating the friction. I guess this should be ported to cannon.js as well.

You can get around this problem by setting the gravity vector to something non-zero and then subtract that value from each body.force before each step(). Then apply your custom gravity.

   var g = this.settings.gravity;
   this.world.gravity.set(0,g,0);
   for(var i = 0; i < this.world.bodies.length; i++) {
      var b = this.world.bodies[i];
      if(b.type === CANNON.Body.DYNAMIC) {
         b.force.y -= b.mass*g; // this will make the net gravity zero

         // apply your custom gravity here

      }
   }
   //then...
   this.world.step(this.settings.timeStep);

Sorry about this mess, should have thought of this earlier.

@josephrocca
Copy link
Author

Great! Thanks a heap - it's all working well now :)

@schteppe
Copy link
Owner

No problemo buddy. Let's keep the issue open to remind me about porting the p2.js .frictionGravity stuff :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants