ForgeableSum Posted August 19, 2016 Share Posted August 19, 2016 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 More sharing options...
samme Posted August 20, 2016 Share Posted August 20, 2016 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). ForgeableSum 1 Link to comment Share on other sites More sharing options...
ForgeableSum Posted August 20, 2016 Author Share Posted August 20, 2016 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. Link to comment Share on other sites More sharing options...
samme Posted August 20, 2016 Share Posted August 20, 2016 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 More sharing options...
ForgeableSum Posted August 20, 2016 Author Share Posted August 20, 2016 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. Link to comment Share on other sites More sharing options...
samme Posted August 20, 2016 Share Posted August 20, 2016 I know it sounds crazy but body.rotation and sprite.rotation are different. The assignment in Arcade.Body is wrong but Arcade.Body#preUpdate is correct. Link to comment Share on other sites More sharing options...
samme Posted August 22, 2016 Share Posted August 22, 2016 Ah I found it, there's Math.getShortestAngle() in Phaser 2.7. Link to comment Share on other sites More sharing options...
Recommended Posts