Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Script] Rotation properties fix?

1367727941
Konrad J.
Pro
API Scripter
I hesitate to call this much of a script compared to what others have made! :)  I'm new to Javascript and can't guarantee anything.  Basically this script fixes a problem that I seem to be seeing in the rotation property.  It can give values greater than 360deg, less than -360deg, and fairly consistently when you start the rotation going left it will give you a minus number in the first 90deg (0 to -90) and then give you a positive number as you go past -90 of 270 and less, this was the one that would cause the most problems. Its a very simple few lines of script.  If you see a better way to do it then please let me know.  probably making it into a function would be best, then you would just pass it the current rotation of the object and it would pass back a "fixed" rotation.  I think I'll do that. For now here it is. on("change:graphic:rotation", function(obj, prev) {       var rot=obj.get("rotation");   if (rot < 0){       rot=360-Math.abs(rot%360);   }   rot=Math.abs(rot%360);   obj.set("rotation",rot);   });  
Cool! I would probably, as you said, turn this into a function that you just call whenever you want to get the rotation...e.g. trueRotation(obj.get("rotation")) rather than saving it back to the server with set() since that will just cause unnecessary work for all the clients.
1367778517
Konrad J.
Pro
API Scripter
Thanks Riley for the encouragement.  I did make it into a function.  And a bunch more functions.  And I am proud to say I can now move a token 1" in the direction it is facing from a chat window command. :) Why would I want to do this?  Two words...Car Wars. In Car Wars there are these things called Maneuvers.  You move a token in the direction its moving currently.  Move it 1" and drift to the right .5".  Do the same thing and rotate it 45 degs, etc.  So instead of having to fiddle with moving a token exactly to the spot you want with your mouse and a turning template I thought I could create some chat commands that would do them for you. It sounds like it should be easy, but its not.  The trig isn't too hard, but first the rotation property wasn't giving the results I wanted.  Next to easily calculate where the token should go with harder maneuvers I'm using sin(rotation) and cos(rotation) to find a point on the circle.  But those formulas only work if zero degrees starts at the x=1, y=0 grid and goes counterclockwise.  Rotation starts at the x=0, y=-1 grid and goes clockwise.  And to top it all off the y grids are reversed, so +y is down and -y is up.  The trig formula returns it the reverse for the y.  Anyways probably more than anyone wants to hear about this.  Who knows I may be making it more complex than it needs to be, I good at that! :) Next I need to calculate 1" forward and .5" to the right, but no rotation.  I've got it figured out, just need to code it. Here is an example of what I'm talking about.
Cool! I hope that this isn't what you just lost your work on :-(
1367785351
Konrad J.
Pro
API Scripter
It was, but its back again and probably better. :)  I've got an idea for rotating from various corners instead of the middle and all these functions are the base to it so it should work well.  Rotating from the corners is needed for most wargames as well as Car Wars.