# Angular Velocity by Vector - 2D

This is originally a problem in programming, but since almost no one on Stackoverflow know how to solve this I went here instead; https://stackoverflow.com/questions/23003612/javascript-angular-velocity-by-vector-2d

I want to convert X and Y velocities to angular velocity, this is the formula I am currently using to calculate the initial velocity by the x and y values and then turn it into angular velocity for my circle object:

Av = Sqrt(Vx^2 + Vy^2) / R

Angularvelocity = Squareroot of (Velocity x^2 + Velocity y^2) / Circle's radius

This is how it simulates in my programming: http://jsfiddle.net/yzb9P/2/ (Click to change the balls position)

Now since a square root can't be negative, this won't work when the ball is supposed to rotate anti-clockwise. So, I need a signed version of the initial velocity that also can be negative, how do I calculate that?

• Hi Murplx, and welcome to scicomp! On the stack exchange network, we strongly discourage cross-posting the exact same question. We advise that you delete all duplicate posts and only keep one post in the forum most relevant to your question.
– Paul
Apr 12 '14 at 21:18

A cross product will tell you both the magnitude and the sign of your angular velocity. In general, angular velocity is defined by a vector as

$$\vec{\omega} = \dfrac{\vec{r}\times\vec{v}}{|\vec{r}|^2}$$

where $\vec{\omega}$ is the angular velocity, $\vec{r}$ is the vector from the center of rotation to the point under consideration, and $\vec{v}$ is the velocity vector.

In two dimensions this becomes

$$\omega = \dfrac{r_x v_y - r_y v_x}{r_x^2+r_y^2}$$

where a positive value corresponds to counter-clockwise rotation and negative means clockwise.

Edit:
After looking at your JSfiddle some more, it looks like your implementation has the more serious issue of giving rotation even when the ball hits straight down on a flat surface. If you have non-zero velocity, you're computing a non-zero angular velocity without regard to the vector directions involved. The cross product will eliminate this effect. I believe this is what you want: http://jsfiddle.net/yzb9P/7/

Edit 2:
If you want your ball to roll more realistically, there is another bug in your posted code. Try this version, the ball rolls down a slope without stopping unnaturally. I only edited the update() function. http://jsfiddle.net/yzb9P/8/

Edite 3:
A final update with improved implementation of elasticity for the ball: http://jsfiddle.net/yzb9P/11/

• THANK YOU VERY MUCH!!! Just one last question, goto line 188, and change the elasticy (0.8) to 0.1, why does the ball go through the line? Apr 12 '14 at 5:52
• I do not have that problem so I have no idea what the issue might be. It still runs fine for me even if ball.e=1. I will say that setting 0<ball.e<1 adds some damping to the system which slows the ball down and makes things more stable. Also, this code does some "physics-like" things by using a very simple time stepping scheme and without properly solving the equations involved with elastic collisions. This may look ok most of the time, but will likely fail in certain cases. Apr 12 '14 at 15:14
• jsfiddle.net/yzb9P/9 This is how I want it to be, but with corrected and adjustable elasticy, the concept of elasticy in jsfiddle.net/yzb9P/8 is wrong, because the ball goes right through if ball.e = 0 Apr 12 '14 at 15:21
• Is there anyway to correctly then calculate the elastic collision with ball.e? Apr 12 '14 at 15:23
• Oh, I see. I misread your previous comment and used 1 instead of 0.1. This is related to the stability and accuracy of your time stepping method. How to fix that may be a much bigger question than I can answer here. Let me see what's really going on. Apr 12 '14 at 15:35