Modeling the spinning of an object in JavaScript

229 Views Asked by At

I am trying to model the spinning of an object of uniform mass in JavaScript (viewable here).

Currently, the object rotates when clicked and dragged.

I am trying to find the formulas required to achieve a realistic spinning effect once the user has "let go."

So far I've calculated the initial angular velocity of the object like so:

 (rotation in degrees of final grab - rotation in degrees of initial grab)
                                    /
          ((timestamp of final grab - timestamp of initial grab) * 1000)

The browser's console displays the angular velocity once you've let go of your grab.

The problem

The problem is that this is not very realistic for several reasons:

  1. If you drag left slowly and then drag right very fast and let go, it becomes obvious that just setting the wheel to rotate according to the calculated angular velocity is not very realistic. What is the correct formula to describe the object's initial angular velocity after letting go?
  2. I would also like to incorporate friction so that the wheel slows down realistically. What is the correct formula to describe the object's angular velocity over time?

I really appreciate any help.

1

There are 1 best solutions below

1
On BEST ANSWER

If you le go while your grab has horizontal velocity $v$, then the angular velocity should be $\omega=\frac vr$, where $r$ is the radius.

Deceleration due to friction typically has a quadratic component ($\dot\omega=-c\cdot \omega\left|\omega\right|$) from air resistance, but there you should be careful with the discrete steps for large $\omega$. Beyond that, there is a constant component ($\dot\omega =-c\operatorname{sgn}(\omega)$), which causes a linear decrease of $\omega$.