user2039981
user2039981

Reputation:

JavaScript - Angular Velocity by Vector - 2D

I am making a game in JavaScript, with the Canvas API.

I perform circle to segment collision, and I need to calculate the anglar velocity for the circle by its velocity vector, I use this formula to do it:

ball.av = ball.v.length() / ball.r

// ball.av = angular velocity
// ball.v = velocity vector, contains x and y values
// .length() = returns initial velocity (pythagoras) for example: return Math.sqrt(ball.v.x * ball.v.x + ball.v.y * ball.v.y)
// ball.r = radius

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?

I've heard about that the Wedge product is working for this, and I've read many articles about it, but I still don't understand how to implement it to my code, please help!

Upvotes: 0

Views: 1981

Answers (1)

David John
David John

Reputation: 506

In the general case, if the ball is rolling on a surface then the angular velocity would be the cross product of the velocity with the surface normal over the radius.

ball.av = CrossProduct(surfaceNormal, ball.v) / radius;

But if you are always on a flat surface along the x direction then this simplifies to this:

ball.av = -ball.v.x / ball.r;

Here is crossproduct for you if you don't have it.

float CrossProduct(const Vector2D & v1, const Vector2D & v2) const
{
    return (v1.X*v2.Y) - (v1.Y*v2.X);
}

NOTE: if the ball rolls backwards just add a '-' sign to your calculations or swap the parameters in the crossProduct call but I think they are right as I've written them.

Surface normal is a perpendicular normalized (unit) vector from a surface. In your case surface normal is the normalized vector from the contact point to the centre of the circle.

As a side note to remove the component of gravity into a surface as a ball is rolling do this:

vec gravity;

gravity = gravity - surfaceNormal*dot(surfaceNormal, gravity);

you can then apply the resultant gravity as a ball is rolling down a surface.

Upvotes: 1

Related Questions