ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Magnetic field expression

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Benjamin Chellaram
Magnetic field expression
on Sep 10, 2019 at 8:10:27 am

Hello.

I'm trying to create a magnetic field using expressions in after effects. I have a lot of square shape layers spread out in my comp (around 100 layers or so, lets call these particles for now) and I want to create a magnetic field around a null. This is what I want the particles to do when the null is flying across the screen

for position:
I want the particles to repel from the null, but with a maximum and minimum distance, and amount.
I've looked at expressions found online and tested them out (I've also tried to tweak them to the best of my limited expressions abilities).

Position Expression 1:

avoidPos = thisComp.layer("Avoid_layer").position;
maxDisplacement = thisComp.layer("Controls").effect("max displacement")("Slider");
minDistance = thisComp.layer("Controls").effect("min distance")("Slider");
startDistance = thisComp.layer("Controls").effect("StartDistance")("Slider");

vec = avoidPos - position;
directionVec = normalize( vec );

displaceAmt = ease( startDistance , 0 , minDistance , 0, maxDisplacement );
displacementVec = displaceAmt * directionVec;
position + displacementVec


This expression only gets me so far. all the particles are offset som reason, they do not start and end in the same position when the animation starts/end.
I've even tried to create an "if" statement to start the animation when the Avoid_layer is in reach / proximity (based on the slider values), but it sort of "snaps" to position.


I've also tried a different expression:

Position Expression 2:

p = thisComp.layer("Avoid_layer").position;
r = thisComp.layer("Controls").effect("Repulsion")("Slider"); //repulsion constant
dist = length(position,p);
dir = normalize(position - p);
offset = dir*r/(dist*dist);
value + offset


This sort of does what I want, but there are less paramaters to control. It does not do any snapping, which is good, but I cant control the paramaters in the same way like position expression 1. A mix between these two is probably what I am looking for. I've also looked at Newton 3, which looks awesome, but I dont want a ton of keyframes / baked animations.


Lastly, I also want to rotate each particle (I want them to look at the null) with a set proximity value (slider), but with a magnetic field. Meaning, I dont want them all to be rotated from the beginning or snap in to rotational position when the null is within reach / proximity.

this is what I've found online (but this expressions forces the particles to look at the null at all times, not when its only within reach / proximity)

rotation expression:

Line = length(toWorld(anchorPoint), thisComp.layer("Avoid_layer").transform.position);
delta = toWorld(anchorPoint) - thisComp.layer("Avoid_layer").toWorld([0,0,0]);
radiansToDegrees(Math.atan2(delta[0],-delta[1]));


It's a little bit hard to put in to words what I really want and please let me know if anything is unclear.
I've also attached a small screenshot of what my comp looks like to easier understand what I want.

https://imgur.com/p13fDsx

Thank you!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]