Da Fish in Sea

These are the voyages of Captain Observant

Revolving Planet

| Comments

Here’s my second attempt at getting a planet to revolve… this time I used trig to push it around in a circle, instead of just rotating its container as I did before. Here is the result:

Note: It uses the Sphere class for the ‘planet’.

If you don’t see a revolving sphere above, then you need to install the flash player 10, which you can get here:

http://labs.adobe.com/downloads/flashplayer10.html

The Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package  {
    import flash.display.Sprite;
    import flash.events.Event;

    public class Threed2 extends Sprite {

        private var centerX:Number = 200;
        private var centerY:Number = 200;
        private var centerZ:Number = 0;
        private var sphere:Sphere;
        private var radius:Number = 200;
        private var angle:Number = 0;
        private var speed:Number = 0.05;
        private var planets:Array;

        public function Threed2() {
            init();
        }
        private function init():void {
            planets = [];
            //angle is rotation angle around centre
            angle = 0;
            //add a planet
            addPlanet(0x00ff00,100,200);
            addEventListener(Event.ENTER_FRAME, function(e:Event):void {
                    revolvePlanets();
                });
        }

        private function revolvePlanets():void {
            for(var i:int = 0; i < planets.length; i++){
                    var planet:Sphere = planets[i];
                    angle+=speed;
                    planet.x = centerX + Math.cos(angle)*radius;
                    planet.z = centerZ + Math.sin(angle)*radius;
            }
        }

        private function addPlanet(pcolor:uint=0xff0000, psize:Number=30,orbit:Number=150):void {
            //create 'planet'
            var planet:Sphere = new Sphere(psize, pcolor);
            planet.x = orbit;
            planet.y = orbit;
            addChild(planet);
            planets.push(planet);
        }
    }
}

// Copyright (c) 2008 David Wilhelm
// MIT license: http://www.opensource.org/licenses/mit-license.php