### Flash Labs NO.2 : Newton’s Law of Gravitation

22Aug08

It’s high time to support my physics by computers. Today I experimented with Newton’s Law of Gravitation for 3D and as far as it goes to one object it works great. However I didn’t managed to apply the force to the second object (The Sun).The movie which you can see above is a gif, cause of WordPress politics… The original animation looks much better.

Here’s my source code:

```/* NEWTON'S GRAVITATION v. 0.1
The rule that makes the day and night
F = GMm/r^2

Two movieclips one called "planet" and the second called "sun" both should be exported for AS2
To get the best results get "planet" and the "sun" to the center of the movieclip
To make the planet move in third dimension I used the scale trick and I combine it with the Newton's formula.
Well we all know that Newton laws are symmetrical, so that the sun should also move a little, but I had no luck adjusting the params so I excluded it.

coder: Ranza
web: https://masteranza.wordpress.com
time: 00:26:31
*/

var sun:MovieClip = _root.attachMovie("sun","sun",1);
sun._x =  Stage.width /2;
sun._y = Stage.height /2;
sun.mass = 20;

var planet:MovieClip = _root.attachMovie("planet","planet",2);
planet.Yspeed = -0.5;
planet.Xspeed = -0.1;
planet.Zspeed = -0.2;
planet.mass = 5;
planet._x = Stage.width /5;
planet._y = Stage.height /2;
planet.onEnterFrame = _root.PlanetPhysics;

function PlanetPhysics(){
//Never mind about the G 😉 the speed is just for fun 😉
sqrdistance = Math.pow(this._x-sun._x,2) + Math.pow(this._y - sun._y ,2) + Math.pow((100 - this._xscale),2);
this.Xacc = ((sun._x - this._x)/Math.sqrt(sqrdistance)) * this.mass * sun.mass / sqrdistance;
this.Yacc = ((sun._y - this._y )/Math.sqrt(sqrdistance)) * this.mass * sun.mass / sqrdistance;
this.Zacc = ((100 - this._xscale)/Math.sqrt(sqrdistance)) * this.mass * sun.mass / sqrdistance;
this.Xspeed += this.Xacc;
this.Yspeed += this.Yacc;
this.Zspeed += this.Zacc;
this._x += this.Xspeed;
this._y += this.Yspeed;
this._alpha = this._xscale = this._yscale += this.Zspeed ;

//The objects which are closer have to be on top
if (this._xscale > 100){
this.swapDepths(2);
}
else{
this.swapDepths(1);
}
}
```

By the way, I’ve invented a logo, shortly I will explain it’s symbolic meanings.