# Modeling the spinning of an object in JavaScript

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.

• Friction and other forces slow the spinning of the top at a constant rate, so if the initial angular velocity was 5 r/s, you could have after 1s, v=4, after 2s, v=3, etc. $(v_i-at)$ – Simply Beautiful Art Sep 9 '16 at 22:35
• Thank you. I have a feeling that tangential acceleration will need to be calculated to find the correct initial angular velocity, but I'm not sure what formula(s) are needed. My searches have led me to several problems involving torque and rotational inertia, but with objects rotating around some other object instead of the object rotating on it's own center of gravity. I will do some research for "top" problems. – Raphael Rafatpanah Sep 9 '16 at 22:41
• Always separate the rotational motion from other rotational motion and any other linear motion. So the motion of spinning, the spiral motion, and the speed as it moves are separately dealt with, usually using $v_t=v_i-at$ – Simply Beautiful Art Sep 9 '16 at 22:47
• I think you have to put an elastic between the mouse and the clicked point of your wheel (elastic simply means $F = -dist^2$ or something like that). You also have to assign a mass to your wheel, and some friction. A major problem is that the clicked point on the wheel has a 3d coordinate, while the mouse only has a 2d coordinate, so you have to say "where is the mouse pointer in the 3d space" for defining $dist$ and $F$ – reuns Sep 9 '16 at 22:52
• By 'elastic', he means like a rubber band. As you pull farther, the recoil is greater by $F=(\Delta x)^2$. And after finding initial velocity $v_i$ (from $(\Delta x)^2$, the velocity after some time $t$ is $v_t=v_i-at$, where $a$ is some experimental constant that makes it look nice. – Simply Beautiful Art Sep 9 '16 at 23:21

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$.