schteppe / cannon.js

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

Question: Impulse forces #27

Open unphased opened 12 years ago

unphased commented 12 years ago

Is there a maximum limit on impulse forces?

I'm trying to create the most simple way to connect together the CSS3 3D (pixel) coordinates with the coordinates I send to Cannon.js, but this means that for something to be visible it has to be quite big, and also large values of gravity are necessary.

I'm currently setting a gravity of +1000 in the Z direction (it is backwards because CSS3 coordinate system is left-handed), making a radius 100, mass 50 sphere, and placing that on top of a kinematic rigid body which consists of a single plane which is controlled by user input.

I'm noticing with these values (and setting 5 solver iterations) that the sphere exhibits quite bouncy behavior and seems to penetrate the planar surface quite a bit if I don't move it gently. If i move it too fast the ball can tunnel through.

If there is some sort of maximum force that is applied that could explain this fully. Of course I will keep testing and hopefully find what I'm doing wrong but I wanted to see if you had any tips. Have you tested using pixel units, i.e. objects of size on the order of 100 rather than 1.

Update: I tried 10 iterations and it's significantly better but still tunnels quite easily. I imagine setting my manipulable ground plane as a super thick rectangular block would improve this.

unphased commented 12 years ago

Much less squishy at mass=5.

schteppe commented 12 years ago

It's nice to hear that your game is progressing :) First of all, I would not recommend you to use screen coordinates in your physics system. Moving between different screen sizes would need some physics tweaking, which you only want to do once... At most.

Suggestion: Use a physics/screen coordinate conversion whenever you want to interact with the screen. An example can be found in the mobile gravity demo.

If you really want to use screen coordinates, you're left alone with the tweaking. There are a few parameters that you need to tweak; gravity, mass of objects, number of iterations, constraint stiffness (world.solver.k), constraint damping (world.solver.d) and possibly the time step (you're stuck at 60Hz in the browser but doing 2 steps per frame would improve simulation quality). I wish that all these parameters could be tweaked in the Demo framework but that is not the case (yet).

Also, the other SPOOK parameters are related to stiffness and damping and needs to be recalculated whenever you change k or d. The world class constructor contains code for calculating these. I hope this can be done automatically in the future.

unphased commented 12 years ago

You're definitely right about the coordinates getting messed up on screen resize, I hadn't actually thought about that but that has already started to become a bit of an issue as well! I should definitely not screw with the physics world when the screen changes.

I will say though that with some parameter tweaking (high mass objects seem to not interact with kinematic bodies too well -- are penetration resolution impulses dependent on the mass? If not they should be!!) the behavior obtained is very good. I'm not very familiar with SPOOK but from what I can tell it does an admirable job.

schteppe commented 12 years ago

Penetration response does not depend on the mass at the moment, but it would definitely be awesome if it was. That would scale the SPOOK parameters automatically without having to think :P

You can see the contact penalty as a spring. If overlap, a spring force tries to separate the objects. The SPOOK parameters adjusts stiffness (k) and damping (d) of the spring. The masses (m1 & m2) of the two contacting objects are given, the question is how to calculate k and d from this. Or perhaps one should use the reduced mass when calculating constraint forces? Not sure at the moment, but I'll think about it :)