> For the complete documentation index, see [llms.txt](https://frangiordano96.gitbook.io/ae-expressions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://frangiordano96.gitbook.io/ae-expressions/quick-tips/after-effects-expression-force-field.md).

# After Effects Expression - Force Field

This Expression will move the planets around the Spaceship based on the field's strength.

{% embed url="<https://youtu.be/7E8iyIWD6TA>" %}

#### Apply this Expression on each planet's position:

{% code overflow="wrap" %}

```javascript
//Getting the strength value of the force field
var reflectRadius = thisComp.layer("SPACESHIP").effect("DEFLECT RADIUS")("Slider");

//Calculating the total distance between the layers and the distance on each axis
var lengthX = value[0] - thisComp.layer("SPACESHIP").transform.position[0];
var lengthY = value[1] - thisComp.layer("SPACESHIP").transform.position[1];
var distance = length(value, thisComp.layer("SPACESHIP").transform.position);

//Calculating the angle between the layers,
//this will allow us to get how much force will be applied into each axis

var angle = Math.atan2(lengthY, lengthX);
var cosine = Math.cos(angle);
var sine = Math.sin(angle);
var moveRadius = ease(distance, reflectRadius, 0, reflectRadius, 0);

//Applying the final value
[value[0]+moveRadius*cosine, value[1]+moveRadius*sine];
```

{% endcode %}
