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

Elastic force to original position #342

Open fabiodr opened 7 years ago

fabiodr commented 7 years ago

Cannon can do something like in this demo, applying a constant force in direction to original anchor position/rotation, with some elaticity on arrival?

I tried something like zero gravity, canceling it in preStep(), following what was done here: http://schteppe.github.io/cannon.js/demos/callbacks.html

But angular momentum and inverse proportion to distance makes it go long away the anchor and never come back, so it is clear that it's not the way to go. It's more like the inverse of gravity, the larger the distance, larger the force, but have to slow down on the way back.

https://youtu.be/RLEEVGxu_k8?t=3m2s

schteppe commented 6 years ago

If it’s about applying a custom force, Cannon.js can definitely do it, as you have already figured out. The question is what that force is...

I was first thinking about damping. Either damping via body.linearDamping or a critically damped spring force. A critically damped spring is worth trying, it will give you that slowdown on the way back. If I was experimenting with VR UI, I would definitely play around with critically damped springs since they are used frequently in 2D UIs. You can create such spring using CANNON.Spring, and set the damping coefficient to 2*sqrt(stiffness*mass). Read more at https://en.m.wikipedia.org/wiki/Damping_ratio

Sorry about the extremely late reply..