-
Notifications
You must be signed in to change notification settings - Fork 711
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
Comments
Are the bodies in contact? Maybe they lose the contact when you apply the force? |
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 If there's nothing that immediately comes to mind I'll do some more testing and report back. |
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 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. |
Great! Thanks a heap - it's all working well now :) |
No problemo buddy. Let's keep the issue open to remind me about porting the p2.js |
Fix: Friction disappears when manually applying gravity schteppe#224
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 callworld.step()
. I could rearrange my code structure to usepreStep
but it would be quite inconvenient.For reference, here's how I'm manually applying gravity:
Thanks! Any hints at all would be appreciated.
The text was updated successfully, but these errors were encountered: