Forums › Adobe After Effects Expressions › Rotating the Shortest Way Possible
-
Rotating the Shortest Way Possible
-
Frankie Jones
December 8, 2022 at 3:14 pmHi,
How can you ease between 2 values the shortest way possible i.e. ObjA is at 2 degrees and ObjB is at 350degrees, how do you make ObjA rotate 2,10,359,358… etc. rather than 2,3,4,5,6…. The code below weights between 2 values: Value A when Slider is 0, Value B when Slider is 1. Something I can’t figure needs to be added to make it Rotate forwards or backwards depending on which is shortest way:
// On Rotation
var total = 0, totalWeight = 0, remWeight = 0;
var rot = thisComp.layer("Target").transform.rotation);
if (hasParent) rot = parent.rotation;
var fade = effect("Slider Control")("Slider"); // Slider is 0 to 1
total += fade*rot; totalWeight += fade;
remWeight = 1 - totalWeight;
totalWeight==0 ? value : linear(remWeight,total/totalWeight,value);</li-code> -
Filip Vandueren
December 8, 2022 at 9:21 pmThe easiest fix is to use Orientation on a 3D layer.
-
Filip Vandueren
December 9, 2022 at 8:58 amI couldn’t really deduce what you were doing with the weight variables.
Here’s an example that bases itself off of the rotation keyframes already present and always takes the shortest route (always <=180°)
nk = nearestKey(time);
prevKey = key ( nk.time >time ? nk.index-1||1 : nk.index );
nextKey = key( Math.min(numKeys, prevKey.index + 1) );
delta = (nextKey.value - prevKey.value)%360;
shortRoute = Math.abs(delta)>180 ? (Math.abs(delta)-360)*Math.sign(delta) : delta;
// longRoute = Math.abs(delta)<180 ? (Math.abs(delta)-360)*Math.sign(delta) : delta;
linear(time, prevKey.time, nextKey.time, prevKey.value, prevKey.value + shortRoute);
Viewing 1 - 3 of 3 posts
Log in to reply.