Star Orbit Demo
Flash 3D demo about objects orbiting stars
Flash 3D demo about objects orbiting stars
package com.cet.parallax {
//_________________________________________________________________
// imports
import com.bit101.components.*;
import flash.display.*;
import flash.events.*;
import flash.filters.GlowFilter;
import flash.text.*;
import org.papervision3d.cameras.*;
import org.papervision3d.core.geom.*;
import org.papervision3d.materials.*;
import org.papervision3d.materials.special.*;
import org.papervision3d.objects.*;
import org.papervision3d.objects.primitives.*;
import org.papervision3d.objects.special.*;
import org.papervision3d.render.*;
import org.papervision3d.scenes.*;
import org.papervision3d.view.*;
//_________________________________________________________________
// class
public class PV3DView extends Sprite {
//_________________________________________________________________
// variables
//--- gui
private var radius :Slider;
private var speed :Slider;
private var sizer :Slider;
private var alignX :Slider;
private var alignY :Slider;
//--- 3D objects
private var holder :DisplayObject3D;
private var sun :Sphere;
private var earth :Sphere;
//--- 3D scene elements
private var earthView :Viewport3D;
private var earthCam :Camera3D;
private var scene :Scene3D;
private var renderer :BasicRenderEngine;
//--- math
private var rot :Number;
private var rotRad :Number;
//--- crap
private var overView :Viewport3D;
//_________________________________________________________________
// constructor
public function PV3DView() {
init3D();
addEventListener(Event.ENTER_FRAME, loop3D);
}
//_________________________________________________________________
// setup papervision3D
private function init3D():void {
rot = -989;
scene = new Scene3D();
renderer = new BasicRenderEngine();
earthCam = new Camera3D();
earthCam.focus = 16;
earthCam.x = earthCam.y = 0;
earthCam.z -= 2000;
earthView = new Viewport3D(800, 600);
earthView.x = 0;
earthView.y = 0;
addChild(earthView);
overView = new Viewport3D(1, 1);
addChild(overView);
holder = new DisplayObject3D;
var sunSkin:MovieMaterial = new MovieMaterial(new textureSun);
sun = new Sphere(sunSkin, 600, 18, 16);
var earthSkin:MovieMaterial = new MovieMaterial(new textureEarth);
earth = new Sphere(earthSkin, 80, 18, 16);
earth.z = rot;
holder.addChild(earth);
holder.addChild(sun);
scene.addChild(holder);
var toolBox:Sprite = new Sprite();
toolBox.graphics.beginFill(0x333333);
toolBox.graphics.drawRect(0, 0, 130, 200);
toolBox.graphics.endFill();
toolBox.x = 670;
toolBox.y = 400;
addChildAt(toolBox, 2);
var font:TextFormat = new TextFormat("Arial", 12, 0xffffff);
var txtRad:TextField = new TextField();
txtRad.defaultTextFormat = font;
txtRad.autoSize = TextFieldAutoSize.LEFT;
txtRad.text = "earth's orbital radius";
txtRad.x = 8;
txtRad.y = 2;
toolBox.addChild(txtRad);
radius = new Slider();
radius.setSliderParams(1000, 1500, 1000);
radius.x = 10;
radius.y = 25;
radius.draw();
toolBox.addChild(radius);
var txtSpeed:TextField = new TextField();
txtSpeed.defaultTextFormat = font;
txtSpeed.autoSize = TextFieldAutoSize.LEFT;
txtSpeed.text = "earth's orbital speed";
txtSpeed.x = 8;
txtSpeed.y = 37;
toolBox.addChild(txtSpeed);
speed = new Slider();
speed.setSliderParams(0, 3, 1);
speed.x = 10;
speed.y = 59;
speed.draw();
toolBox.addChild(speed);
var txtSize:TextField = new TextField();
txtSize.defaultTextFormat = font;
txtSize.autoSize = TextFieldAutoSize.LEFT;
txtSize.text = "planet's size";
txtSize.x = 8;
txtSize.y = 73;
toolBox.addChild(txtSize);
sizer = new Slider();
sizer.setSliderParams(0.5, 2.5, 1);
sizer.x = 10;
sizer.y = 94;
sizer.draw();
toolBox.addChild(sizer);
var txtAngleX:TextField = new TextField();
txtAngleX.defaultTextFormat = font;
txtAngleX.autoSize = TextFieldAutoSize.LEFT;
txtAngleX.text = "angle X";
txtAngleX.x = 8;
txtAngleX.y = 110;
toolBox.addChild(txtAngleX);
alignX = new Slider();
alignX.setSliderParams(-90, 90, 0);
alignX.x = 10;
alignX.y = 130;
alignX.draw();
toolBox.addChild(alignX);
var txtAngleY:TextField = new TextField();
txtAngleY.defaultTextFormat = font;
txtAngleY.autoSize = TextFieldAutoSize.LEFT;
txtAngleY.text = "angle Y";
txtAngleY.x = 8;
txtAngleY.y = 145;
toolBox.addChild(txtAngleY);
alignY = new Slider();
alignY.setSliderParams(-90,90, 0);
alignY.x = 10;
alignY.y = 165;
alignY.draw();
toolBox.addChild(alignY);
}
//_________________________________________________________________
// setup papervision3D
private function loop3D(e:Event):void {
sun.rotationY++;
earth.rotationY++;
earth.scaleX = earth.scaleY = earth.scaleZ = sizer.value;
holder.rotationX = alignX.value;
holder.rotationZ = alignY.value;
orbit();
renderer.renderScene(scene, earthCam, earthView);
}
//_________________________________________________________________
// planet orbit
private function orbit():void {
rot += speed.value;
rotRad = (rot-90) * (Math.PI/180);
earth.z = (radius.value * Math.cos(rotRad) ) * -1;
earth.x = radius.value * Math.sin(rotRad) * -1;
}
}
}
//_________________________________________________________________
// end of file