I have a script I found and modified from here, to make tokens face the direction they move so they are always facing the right way.
it works wonderful but the sudden "snap" to a new direction is a bit jarring when they have limited directional light on dynamic light maps.
So I was attempting to 'rotate' the token over a second until its facing the correct way, and seem to have hit a wall.
First problem is token can have over 360 degrees of rotation(?). I think I solved it 'adding up over time" by checking to see if its over 360, if so subtract 360 which seems to fix it ever giving me like 410 as the angle.
Next was rotating it the proper way, say they turned 10 degrees, make sure it doesn't do a 350 degree rotation making it look like the spun around backwards by comparing old angle to new, and picking the smaller distance of the 2 to reach it. (for example going from 5 to 355 I didn't want them doing a full spin to try to go to the right facing)
My main problem now is actually doing a "while" or such.
I tried using a 'setInterval' inside a 'while', dividing the new angle by 10, then doing a small rotate every 1/10th of a second until angle=needed angle.
But that was messy, caused sandbox to sometime choke thinking it was hung, and sometimes not do anything. (think sometimes my math was off).
This is what I have so far, anyone give me a push in the right direction of how to take the angle needed to face the new way, and turn it that way over a set period of time?
Some of the variables arent used, they was the ones I was doing to calcuate the right direction to rotate it.
it works wonderful but the sudden "snap" to a new direction is a bit jarring when they have limited directional light on dynamic light maps.
So I was attempting to 'rotate' the token over a second until its facing the correct way, and seem to have hit a wall.
First problem is token can have over 360 degrees of rotation(?). I think I solved it 'adding up over time" by checking to see if its over 360, if so subtract 360 which seems to fix it ever giving me like 410 as the angle.
Next was rotating it the proper way, say they turned 10 degrees, make sure it doesn't do a 350 degree rotation making it look like the spun around backwards by comparing old angle to new, and picking the smaller distance of the 2 to reach it. (for example going from 5 to 355 I didn't want them doing a full spin to try to go to the right facing)
My main problem now is actually doing a "while" or such.
I tried using a 'setInterval' inside a 'while', dividing the new angle by 10, then doing a small rotate every 1/10th of a second until angle=needed angle.
But that was messy, caused sandbox to sometime choke thinking it was hung, and sometimes not do anything. (think sometimes my math was off).
This is what I have so far, anyone give me a push in the right direction of how to take the angle needed to face the new way, and turn it that way over a set period of time?
Some of the variables arent used, they was the ones I was doing to calcuate the right direction to rotate it.
on("change:graphic", function(obj, prev) { if(obj.get("name") == "NR" || state.Tokenface == 0) return; if(obj.get("left") != prev.left || obj.get("top") != prev.top) { if(obj.get("layer") == "objects") { var movex = obj.get("left") - prev.left; var movey = obj.get("top") - prev.top; var degs; var olddeg = obj.get("rotation"); var olddeg = olddeg - olddeg%1 if(movey != 0) { degs = Math.atan(movex/movey) * 57.29577; if(movey < 0) degs = 360-degs%360; else degs = 180-degs%360; } else if(movex < 0) degs = 270; else degs = 90; degs = degs - degs%1; if (degs > 360) degs = degs-360; if (olddeg > 360) olddeg = olddeg-360; turnmin = Math.min(degs, olddeg); turnmax = Math.max(degs, olddeg); turnang = turnmax-turnmin; if (olddeg > degs) turnang = 0-turnang; obj.set("rotation", degs); } } });