Jump to content

Calculate tween rotation time based on angular velocity


ForgeableSum
 Share

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 :).

 

 

Link to comment
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). 

Link to comment
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 .

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...