Created
July 1, 2015 11:25
-
-
Save jarkkosyrjala/a67d7bf1d30847fe280c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package geom { | |
import flash.events.Event; | |
import flash.geom.Point; | |
import flash.utils.getTimer; | |
/** | |
* Simple class for orbiting a point | |
* @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Point.html | |
* | |
* Also has methods for updating value using ENTER_FRAME event | |
* or directly calling update function with delta time | |
* @example The following code uses the class to rotate a ball around a point in the middle of the stage | |
* | |
* <listing version="3.0"> | |
* package { | |
* import flash.display.Shape; | |
* import flash.display.Sprite; | |
* import flash.events.Event; | |
* import flash.geom.Point; | |
* | |
* import geom.OrbitingPoint; | |
* | |
* [SWF(frameRate="60", width="500", height="500", backgroundColor="#FFFFFF")] | |
* public class OrbitingPointExample extends Sprite { | |
* private var ball:Shape; | |
* private var orbiter:OrbitingPoint; | |
* public function OrbitingPointExample() { | |
* super(); | |
* ball = new Shape(); | |
* ball.graphics.beginFill(0xFF0000, 1); | |
* ball.graphics.drawCircle(0, 0, 50); | |
* ball.graphics.endFill(); | |
* addChild(ball); | |
* var ballPoint:Point = new Point() | |
* | |
* orbiter = new OrbitingPoint( | |
* new Point(stage.stageWidth * 0.5, stage.stageHeight * 0.5), | |
* Math.PI, //Half circle in 1 second | |
* 60, | |
* 0); | |
* this.addEventListener(Event.ENTER_FRAME, update); | |
* } | |
* | |
* private function update(e:Event):void { | |
* //Update using deltaTime | |
* //orbiter.update(0.016); | |
* //or by calling the enterFrameHandler | |
* orbiter.enterFrameHandler(); | |
* //values are now updated so we can update the ball coordinates | |
* ball.x = orbiter.x; | |
* ball.y = orbiter.y; | |
* } | |
* } | |
* } | |
* </listing> | |
*/ | |
public class OrbitingPoint extends Point { | |
public var middlePoint:Point; | |
public var rotationPerSecond:Number; | |
public var radius:Number; | |
/** Current angle in radians */ | |
public var currentAngle:Number; | |
protected var previousFrameTime:Number; | |
/** | |
* Point object orbiting around middle point. | |
* @param middlePoint point to orbit | |
* @param rotationPerSecond rotation per second in radians | |
* @param radius how far from middle point this point rotates | |
* @param startAngle angle in radians where orbiting starts | |
*/ | |
public function OrbitingPoint ( | |
middlePoint:Point, | |
rotationPerSecond:Number=1, | |
radius:Number=30, | |
startAngle:Number=0) { | |
this.middlePoint=middlePoint; | |
this.rotationPerSecond=rotationPerSecond; | |
this.currentAngle=startAngle; | |
this.radius=radius; | |
super(middlePoint.x+radius*Math.cos(currentAngle),middlePoint.y+radius*Math.sin(currentAngle)); | |
} | |
/** | |
* Update angle with using flash.events.Event.ENTER_FRAME event. | |
* This function automatically calculates the deltaTime so that rotation rate is frame rate independent | |
*/ | |
public function enterFrameHandler(e:Event=null):void { | |
if(!previousFrameTime) { //when called for the first time | |
previousFrameTime=getTimer(); | |
update(0); | |
return; | |
} | |
//calculate deltaTime and call update | |
update((getTimer()-previousFrameTime)*0.001); //* 0.001; | |
previousFrameTime=getTimer(); | |
} | |
/** | |
* Updates currentAngle and x and y values based on passed time. | |
* This method can be used independently or is alternatively called by enterFrameHandler | |
* @param deltaTime seconds passed from the previous update | |
*/ | |
public function update(deltaTime:Number):void { | |
currentAngle+=rotationPerSecond*deltaTime; | |
//Could use Point.polar method to calculate the polar point, | |
// but using trigonometry is faster and we also avoid creating | |
// new Point object on each update | |
this.setTo(middlePoint.x+radius*Math.cos(currentAngle),middlePoint.y+radius*Math.sin(currentAngle)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment