Forums › Adobe After Effects Expressions › step move based on distance of an object and "an effector"
-
step move based on distance of an object and "an effector"
-
Mikhail Vasilev
December 2, 2022 at 9:57 pmHi guys!
task:
we have an object and we have an effector
when object comes closer to the center of the effector it starts to throttle more
something like this but it does not work(posterizeTime effect does not properly work too)magnet radius = 500; //red circle
a = linear( length,0, magnet radius, 1 , 30 ) ; // when we come closer to red circle we change posterizeTime framesPerSecond ;
posterizeTime(a) ;Ive been trying to solve this for 5 days but have no luck
Tried Dan Ebberts’ random approaches but it does not work the way i need
I came up with the idea to divide a whole path to equal sections(min step) and then kind of feed new step value to the function, feed it with min Step rate
but it does not workis there any ideas how to do it using only expressions?
Thanks Dads! ) -
Max Haller
December 4, 2022 at 4:08 amHere is one possible way to achieve the desired effect using the linear() function and the posterizeTime() effect:
// Set the magnet radius and the minimum posterization rate
const magnetR adius = 500;
const minRate = 1;
// Calculate the distance of the object from the center of the effector
const distance = length(thisComp.layer("Effector").position, thisComp.layer("Object").position);
// Calculate the posterization rate using the linear() function
const rate = linear(distance, 0, magnetRadius, minRate, 30);
// Apply the posterization effect using the calculated rate
posterizeTime(rate);
The above expression uses the position of two layers, “Effector” and “Object”, to calculate the distance between them. The distance is then used to calculate the posterization rate, which is passed to the posterizeTime() effect. The closer the object is to the center of the effector, the higher the posterization rate will be. You can adjust the magnet radius and the minimum rate to achieve the desired behavior.
You can also use other mathematical functions, such as the pow() function, to calculate the posterization rate in a non-linear way. For example:
// Calculate the posterization rate using the pow() function
const rate = pow(distance / magnetRadius, 2) * 30 + minRate;
This expression uses the distance and the magnet radius to calculate the posterization rate in a quadratic fashion, which means that the rate increases faster as the distance gets smaller.
I hope this helps! Let me know if you have any questions.
-
Mikhail Vasilev
December 4, 2022 at 2:58 pmthanks Max
it looks like it should work but it gives same jumpy motion i had before
something wrong with posterizeTime effect/expression command
it just does not work properly, that why i tried Den Ebberts random stuff to handle this jumpy thing. I even made some progress but my steps are random and not depend on distance between Obj. and Effectorhere is an example with your expression
i tried both posterizeTime effect and expression
tried it in cs6 and cc2022
same jumpy thing…
any other ideas?
it should be something that stops expression for sometime(while we are on the plateau) than feed new new numbers(plateau length/hold time) to the function -
Mikhail Vasilev
December 4, 2022 at 3:34 pmalso “const” declaration does not work for me
had to clear it -
Mikhail Vasilev
December 4, 2022 at 9:04 pmhere is my best result so far
—start = 0;
end = 0;
j = 0;
endPos = 0;
tDur = thisComp.frameDuration ; // by one frame
//time generation!!!
while ( time >= end) // it sets new milestone for the time then time follow it and reach it, became larger and we have new ‘ end ‘ jump
{
j += 1;
seedRandom(j,true);
// start = end;
//end += Math.abs( Math.floor( random(tDur,tDur) ) ) // it sets time jumps\sectors
end += Math.abs( random(tDur*j*2,tDur*j*2) ) // it sets time jumps\sectors
endPos += Math.abs( Math.floor( random(2*j,2*j) ) ) ;// it sets pos +
}
—
it gives this kind of graph
but it does not depend on distance between objects -
Dan Ebberts
December 4, 2022 at 11:39 pmI don’t really understand what you’re trying to do with the random stuff, but this example might be helpful:
E = thisComp.layer("Effector");
radius = E("Contents")("Ellipse Path 1")("Size")[0]/2;
minFactor = .1;
f = thisComp.frameDuration;
p1 = E.position;
p2 = position;
tAccum = 0;
for (t = inPoint; t <= time; t += f){
d = length(p1.valueAtTime(inPoint+tAccum),p2.valueAtTime(inPoint+tAccum));
if (d < radius){
factor = linear(d,0,radius,minFactor,1);
tAccum += f*factor;
}else{
tAccum += f;
}
}
position.valueAtTime(tAccum); -
Mikhail Vasilev
December 5, 2022 at 4:06 pmhmm…interesting..some alien stuff here )
Thanks Dan, will explore it today -
Mikhail Vasilev
December 6, 2022 at 8:06 pmThanks Dan,
tried it todayin your version it kind of speeds up the object rather than changing its “stay still time”
I also try to do it without any keyframes. Position will be driven by expression as well
your version is actually usable for me but not exactly what i needhere is an image of the Object position graph which will illustrate it better
i need this position graph to be generated with expressions only(no effects no keyframes) -
Dan Ebberts
December 6, 2022 at 11:01 pmMaybe something like this:
E = thisComp.layer("Effector");
radius = 500;
stepSize = [10,0];
minHold = .1;
maxHold = .5;
offset = [10,0];
curOffset = [0,0];
t = inPoint;
while (t <= time){
d = length(E.position,position+curOffset);
t += linear(d,0,radius,maxHold,minHold);
curOffset += offset;
}
value + curOffset - offset; -
Mikhail Vasilev
December 7, 2022 at 9:19 amDan, this is amazing! This is acxactly what i need
Where is your damn book about Ae.expressions? )
You have to write this book
Every your peace of code is very interesting to studyIm mostly graphic/motion designer and i would love to work on this book with you!
I Enjoy to mix math and art but im not that experienced as you, i mean in math
here is some of my stuff if you interested
https://www.behance.net/Mikhail1983You know, this thick 300 pages book with a lot of divers examples, cool illustrations and animations/
Log in to reply.