# After Effects Expression - Inertial Rotation

{% embed url="<https://www.instagram.com/p/CKHiSRonI7e/>" %}

![](https://3498745851-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2GTVVGFE-rvBUhhG1c%2F-MQUwIM4umYp7u6KhHBB%2F-MQUx2AJGzzndcIzvJJg%2FREF%20GIF.gif?alt=media\&token=0fa692d2-eaaa-44a1-92ab-9e4c36c0afde)

Apply this code on the Layer's Rotation:

```javascript
let amp = 2.0; //Sets the angle that the layer will rotate on the spring animation
let freq = 2.0; //Sets the velocity that the spring animation will have
let decay = 4.0; //How long the spring animation lasts
let rotateOnMovement = 42; //Maximum angle that the layer will rotate when it's moving
let useAxis = 0; // 0 will use the speed of the X axis and 1 will use the speed of the Y axis
let timeBeforeKey = thisComp.frameDuration/10;
let refValue = position;

if (refValue.velocity[useAxis] != 0) {
	linear(refValue.velocity[useAxis], -3000, 3000, -rotateOnMovement, rotateOnMovement);
} else {
	let n = 0;
	if (refValue.numKeys > 0) {
		n = refValue.nearestKey(time).index;
		if (refValue.key(n).time > time) { n--; }
	}
	if (n == 0) {
		t = 0;
	} else {
		t = time - refValue.key(n).time;
	}
	if (n > 0 && t < 1) {
		let v = refValue.velocityAtTime(refValue.key(n).time - timeBeforeKey)[useAxis];
		value - (v*(amp/100)*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t));
	} else {
		value;
	}
}


/*
Expression by Francisco Giordano, January 2021.
Based on Intertial Bounce Expression by Animoplex - https://gist.github.com/animoplex/aafd6a157282351c8dfeea385d969ef2
Free for personal use and commercial use.
Instagram @ae.fran.giodano - frangiordano96@gmail.com
*/
```
