Hi guys, I’ve got an issue with a Drift() recursive function I wrote not updating in the _view each time around. It updates the final position after all the loops have finished, but i want it to update each time around so it drifts to a stop. I have 2 trace statements to monitor the data in real time and can see when I lift my mouse button up (“stop”) and the newangle data as it feeds through the Orbit() function with each loop. What am I missing? Is it a timing thing?
package
{
import away3d.cameras.SpringCam;
import away3d.containers.ObjectContainer3D;
import away3d.containers.Scene3D;
import away3d.containers.View3D;
import away3d.lights.PointLight;
import away3d.materials.ColorMaterial;
import away3d.primitives.Cube;
import away3d.primitives.SkyBox;
import away3d.primitives.Sphere;
import flash.display.Sprite;
import flash.events.Event;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import away3d.debug.AwayStats;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Vector3D;
/**
* Show me da cube
* @author Eric Hylland
*/
public class Main extends Sprite
{
private var _view:View3D;
private var _sphere:Sphere;
private var _cube:Cube;
private var _light:PointLight;
private var _environment:ObjectContainer3D;
private var _camera:SpringCam;
private var _scene:Scene3D;
private var _skybox:SkyBox;
private var _viewradius:Number = new Number(400);
private var _viewangle:Number = new Number(0);
private var _viewpoint:Vector3D = new Vector3D(0, 0, -(_viewradius));
public function Main():void
{
// init view
_camera = new SpringCam();
_view = new View3D();
_view.camera = _camera;
_camera.z = -150;
_camera.damping = 200;
_camera.stiffness = 200;
_camera.mass = 400;
_view.backgroundColor = 0xDDDDDD;
this.addChild(_view);
_view.addEventListener(MouseEvent.MOUSE_DOWN, TrackMouse, false, 0, false);
_view.addEventListener(MouseEvent.MOUSE_UP, StopMouse, false, 0, false);
// add away3d debugger
addChild(new AwayStats(_view));
// init light
_light = new PointLight();
_light.x = 0;
_light.y = -1000;
_light.z = -1000;
_light.color = 0xFFFFFF;
_view.scene.addChild(_light);
// create cube with simple color material
var material:ColorMaterial = new ColorMaterial(0x009EE1);
material.lights = [ _light ];
_camera.positionOffset = _viewpoint;
_sphere = new Sphere(material, 50, 20, 20, true);
_cube = new Cube(material, 40, 40, 100, 1, 1, 1, true);
_sphere.y = 0;
_sphere.x = -100;
_camera.target = _sphere;
_cube.y = 0;
_cube.x = 100;
_view.scene.addChild(_sphere);
_view.scene.addChild(_cube);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, onStageResize);
// add event listener
this.addEventListener(Event.ENTER_FRAME, handleEnterFrame, false, 0, true);
}
private function onStageResize(event:Event):void
{
_view.width = stage.stageWidth;
_view.height = stage.stageHeight;
}
private function handleEnterFrame(event:Event):void
{
// rotate the cube
_view.render();
}
private function TrackMouse(ev:MouseEvent):void
{
lastPos.x = _view.mouseX;
lastPos.y = _view.mouseY;
_view.addEventListener(MouseEvent.MOUSE_MOVE, comparePos, false, 0, true);
}
private function StopMouse(ev:MouseEvent):void
{
_view.removeEventListener(MouseEvent.MOUSE_MOVE, comparePos);
trace("stop");
Drift();
}
private var lastPos:Point = new Point(0, 0);
private function comparePos(ev:MouseEvent):void
{
var xDif:Number = _view.mouseX - lastPos.x;
var yDif:Number = _view.mouseY - lastPos.y;
lastPos.x = _view.mouseX;
lastPos.y = _view.mouseY;
Orbit(xDif, yDif);
}
private var azUpLimit:Number = new Number(200);
private var azDnLimit:Number = new Number(-100);
private function Orbit(newangle:Number, azi:Number):Boolean
{
momentum = newangle;
trace (newangle);
var newSpot:Number = _viewangle - (newangle* .007);
_viewpoint.x = Math.cos(newSpot) * _viewradius ;
_viewpoint.z = Math.sin(newSpot) * _viewradius ;
if ((_viewpoint.y + azi) < azUpLimit && (_viewpoint.y + azi) > azDnLimit)
{
_viewpoint.y = _viewpoint.y + azi;
}
_viewangle = newSpot;
return true;
}
private var driftAmount:Number = new Number(.97);
private var momentum:Number = new Number(0);
private function Drift():void
{
if(Math.abs(momentum) > .01)
{
Orbit(momentum, _viewpoint.y);
momentum = momentum * driftAmount;
Drift();
}
}
}
}