# Calculate tween rotation time based on angular velocity

## Recommended Posts

So, I'm trying to calculate the amount of time to tween an angle based on its angular velocity. I know, that sentence is weird, let me explain.

You have a gameObject with a physics body and an angular velocity of X. Over a network, you're trying communicate how much that ship is turning at what speed to another client, but the other client isn't using physics bodies (just tweening the rotation). i.e. I need to translate an angular velocity turn to a tween turn. The formula I have is:

Time = distance / angularVelocity;

I get the distance by finding the difference of the current angle and new angle:

var angleDistance = Math.abs(gameObject.rotation - newAngle);

So turnTime should be:

var turnTime = Math.abs((angleDistance / Math.abs(angularVelocity)) * 1000);

What am I doing wrong here? angularVelocity is based on radians per second, correct? If my formula is correct, there must be something else causing the turn to not look right - perhaps the server tick rate. Someone please check my math :).

##### Share on other sites

Hi feudalwars,

body.angularVelocity, body.maxAngular, and body.rotation are actually in degrees, not radians (the docs are wrong there). sprite.rotation is in radians.

I'd thought there was a Phaser function giving the smallest difference of two angles but I can't find it now. When angles wrap the absolute difference may not be the smallest one: e.g., from 359° to 1° should be +2° (clockwise), not -358° (anticlockwise).

##### Share on other sites

Are you saying it should be:

var angleDistance = Math.abs(gameObject.angle - newAngle);
var turnTime = Math.abs((angleDistance / Math.abs(angularVelocity)) * 1000);

It doesn't seem to work.

##### Share on other sites

That looks about right. I would use body.rotation just to be consistent. And you can take the absolute value just once.

``````body = gameObject.body;

var angleDistance = body.rotation - newAngle;
var turnTime      = Math.abs((angleDistance / body.angularVelocity) * 1000);``````

But I think the sign of angleDistance must matter somehow since it implies the direction of the turn.

Maybe try to make a quick example w/ http://phaser.io/examples/v2/arcade-physics/angular-velocity .

##### Share on other sites

body.rotation and gameObject.rotation are the same (just checked).

At any rate, something must be off as my turns are either too slow or too fast (turn is choopy).

EDIT: Actually, they are definitely too slow.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.