Back to "flash Basic Theory Class-Catalog"
Orbital movement
Finally, let's look at a realistic example where we can create a simple planetary system with the sun and the Earth. Create a mass of 10,000 of the sun and a mass of 1 planets. Next, let the planet move away from the sun for a distance and give it a velocity perpendicular to the sun. As shown in Figure 12-3.
Figure 12-3 Setting the stage
If the given mass, the distance and the speed are very suitable, then can let the planet into orbit. See document Class Orbit.as. You need to explain the code in Init, and a little change is to set the Numparticles variable to 2.
private function init():void {
particles = new Array();
var sun:Ball = new Ball(100, 0xffff00);
sun.x = stage.stageWidth / 2;
sun.y = stage.stageHeight / 2;
sun.mass = 10000;
addChild(sun);
particles.push(sun);
var planet:Ball = new Ball(10, 0x00ff00);
planet.x = stage.stageWidth / 2 + 200;
planet.y = stage.stageHeight / 2;
planet.vy = 7;
planet.mass = 1;
addChild(planet);
particles.push(planet);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
To give you an example, see orbitdraw.as, draw a track line, you can get some interesting patterns.
Elastic motion
You might want to use elastic motion as another particle of gravity. Yes, the elastic movement is my favorite! Recall the example of spring chain and elastic motion between objects in the eighth chapter. Later we will see a wider range of applications, where the particles are moving in an elastic motion, like those in the gravitational process.
The inspiration comes from the node Garden [node Garden] authored by Jared Tarbell in Www.levitated.net, as shown in Figure 12-4. The overall idea is to use a region to contain certain nodes (particles), and each node will interact with the adjacent nodes to produce a reaction. This is my idea of using elastic motion as a reaction force.
Figure 12-4 Node Garden of Jared Tarbell