schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.67k stars 709 forks source link

Heightfield > Player > Velocity #302

Open digitaledge opened 8 years ago

digitaledge commented 8 years ago

Hello again,

finally i finished some character controller using cannon.js. Problem is that i cant get the character to stop sliding on the heightfield.

I did add a contactmaterial like in the given example: this.playerMaterial_cm = new CANNON.ContactMaterial(this.playerMaterial, this.playerMaterial, { friction: 0.6, restitution: 0.1, contactEquationStiffness: 1e8, contactEquationRelaxation: 3, frictionEquationStiffness: 1e8, frictionEquationRegularizationTime: 3 });

this.playerMaterial is used for the heightfield and player body.

My world settings is: this.world.gravity = new CANNON.Vec3(0, -9.8, 0); this.world.broadphase = new CANNON.NaiveBroadphase(); this.world.solver.iterations = 20; this.world.solver.tolerance = 0;

The player body has a mass of 5. When its set like this.body.velocity.set(0, this.body.velocity.y, 0), velocity.y is never zero.

Any idea what i am missing?

Regards Stefan

schteppe commented 8 years ago

I've tried a few hacks to stop a dynamic character controller from gliding in slopes. But it's pretty tricky and I haven't found a good solution for it yet.

One approach is to add more friction... But since friction is a velocity constraint and not a position constraint, the character will always glide a little depending on the scene. You'll also have to add more force/velocity when you want the character to walk.

Another approach is to change the linearFactor in the x/z directions to 0 when stopping. This will make it impossible to move the character in the walking directions.

A dynamic character controller will always have some flaws. If you aim for a character controller found in games, you really want a Kinematic Character controller. In many implementations, it uses some form of raycasting to find where the ground is, and then just places the character there. This far, I haven't found any implementations in JavaScript. But I started making a 2D implementation for p2.js.

And btw.. the velocity will probably never be exact zero, due to the way Cannon works.

digitaledge commented 8 years ago

Hum it seems like friction does not take any effect. Is that maybe because i am running cannon on node,js?

digitaledge commented 8 years ago

Just figured out... when i disable angularDamping the sliding is completely gone. It seems like friction stops working when i set angularDamping to 1.

With an value of 0.9999 friction works and theres no sliding, But the character isnt stabilized anymore.

Is there a way to do that without angularDamping?

schteppe commented 8 years ago

Wait what? angularDamping doesn't do anything unless you have rotation enabled? How do you set up your character body?

And yea, node.js should not be the reason for friction not behaving.

digitaledge commented 8 years ago

I posted the code here: https://codeshare.io/f1lWa

It seems like when angularDamping is = 1 friction doesnt work correctly because the material collides most time only on the box borders.

I think a solution could be to not use angulardamping but limit the rotation on the y axis somehow, to allow about 5-10 degrees of freedom. But i have no clue how to do that.