subprotocol / verlet-js

A simple Verlet physics engine written in javascript
Other
3.76k stars 461 forks source link

Ability to stop composites getting 'mashed' up? #12

Open photonstorm opened 11 years ago

photonstorm commented 11 years ago

Is there a way to stop a composite from getting mashed-up when you slam it against a wall with a lot of force? For example the particles can collapse on themselves and either seemingly vanish (from a visual point of view) or are combining on-top of themselves.

I've tried the new bounds check in the Pull Request but it happens for that also. I've tried setting stiffness to 1 but it doesn't seem to prevent it. Am I missing something obvious?

Also any pointers on how to handle collision between two composite objects?

subprotocol commented 11 years ago

There are a few strategies that will help with the mashing problem:

  1. Increase accuracy by using more steps. ie- try sim.frame(32). It is set to 16 currently.
  2. Some objects by nature are more susceptible to mashing than others. Using more constraints can help.
  3. Angular constraints may help has it would try to correct inverted linkages.
  4. Cap the velocity of all particles

Depending on the effect you're looking for, 4 is probably the most bullet proof way to handle this case. AngularConstraints are computationally more expensive than DistanceConstraints currently. However adding a few AngularConstraints to the inner skeleton of an object should help too.

As for collision detection:

Perfect inelastic collisions would be really difficult (impossible?) to implement in verlet. However you may be able to do something similar if you created a type of distant constraint that repels particles when they are within a threshold distance from each-other. A good name for this type of constraint could be: RepulsiveConstraint. This is something I have been wanting to implement but I've been hung up on other things =)

photonstorm commented 11 years ago

Thanks. I had tried increasing the steps but it didn't seem to make a huge difference. Re: #4 is there any rule (logic/math) I can apply to the velocity of the particles to ensure they are capped off properly? I don't want to just throw in random values and hope for the best if there's an accurate way of working it out.

Thanks for the RepulsiveConstraint idea. I've been re-reading Bitterli's classic post (http://www.gamedev.net/page/resources/_/technical/math-and-physics/a-verlet-based-approach-for-2d-game-physics-r2714) to see if there was a way to adapt verlet-js to use his rigid-body approach, but haven't got too far yet.

subprotocol commented 11 years ago

Yes, it would be similar to DistantConstraint which actually does both contracting and repulsing. As a start you could copy DistantConstraint, and when the distance is > than the distance value, have the RepulsiveConstraint do nothing.