forked from: Orbit
forked from Orbit (diff: 138)
@paulstamp I wrote this as an aid to an external project where I needed to calculate the direction of rotation for an object entering orbit.
ActionScript3 source code
/**
* Copyright paulstamp1 ( http://wonderfl.net/user/paulstamp1 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/auaw
*/
// forked from paulstamp1's Orbit
/**
_____ _____ _____ __ _____ _____ _____ _____ _____
| _ | _ | | | | | __|_ _| _ | | _ |
| __| | | | |__ |__ | | | | | | | | __|
|__| |__|__|_____|_____| |_____| |_| |__|__|_|_|_|__|
@paulstamp
I created this as an aid to an external project where I needed to calculate
the rotational direction of an object entering orbit.
*/
package {
import flash.geom.Point;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
private var _planetA:Planet;
private var _planetB:Planet;
private var _planetC:Planet;
private var _planetD:Planet;
private var _planetE:Planet;
private var _planetF:Planet;
private var _clockwise:Boolean;
private var _orbitHeight:Number = 30;
private var _speed:Number = 2;
public function FlashTest() {
// write as3 code here..
_planetA = new Planet( 20 );
_planetA.x = 465>>1;
_planetA.y = 465>>1;
addChild( _planetA );
_planetB = new Planet( 10 );
addChild( _planetB );
_planetC = new Planet( 10 );
_planetC.x = 20;
_planetC.y = 280;
_planetC.rotation = 10;
addChild( _planetC );
_planetD = new Planet(10);
_planetD.x = 400;
_planetD.y = 20;
_planetD.rotation = 170;
addChild( _planetD );
_planetE = new Planet(10);
_planetE.x = 20;
_planetE.y = 400;
_planetE.rotation = -20;
addChild( _planetE );
_planetF = new Planet(10);
_planetF.x = 400;
_planetF.y = 300;
_planetF.rotation = -170;
addChild( _planetF );
addEventListener( Event.ENTER_FRAME, update );
}
private function calculatePositionAtRotation( value:Number ):Point
{
var rad:Number = value * Math.PI / 180;//degrees to rads
var radius:Number = 30 + _orbitHeight;
var difX:Number = _planetA.x - _planetB.x;
var difY:Number = _planetA.y - _planetB.y;
var velocityX:Number = difX + radius * Math.cos(rad);
var velocityY:Number = difY + radius * Math.sin(rad);
return new Point( _planetB.x + velocityX, _planetB.y + velocityY );
}
private function update( event:Event ):void
{
var speed:Number = _clockwise ? _speed : -_speed;
_planetB.orbit += speed;
var rad:Number = _planetB.orbit * Math.PI / 180;//degrees to rads
var radius:Number = 30 + _orbitHeight;
var difX:Number = _planetA.x - _planetB.x;
var difY:Number = _planetA.y - _planetB.y;
var velocityX:Number = difX + radius * Math.cos(rad);
var velocityY:Number = difY + radius * Math.sin(rad);
_planetB.x += velocityX;
_planetB.y += velocityY;
//show line to planet
this.graphics.clear();
this.graphics.lineStyle( 2, 0x000099 );
this.graphics.moveTo( _planetB.x, _planetB.y );
this.graphics.lineTo( _planetA.x, _planetA.y );
//next point
this.graphics.lineStyle( 2, 0x990000 );
var nextPoint:Point = calculatePositionAtRotation( _planetB.orbit + (20*speed));
this.graphics.moveTo( _planetB.x, _planetB.y );
this.graphics.lineTo( nextPoint.x, nextPoint.y );
//end connection
this.graphics.lineStyle( 2, 0x009999 );
this.graphics.lineTo( _planetA.x, _planetA.y );
//angle for planetB to face planetA
pointAtPlanet( _planetB, _planetA );
pointAtPlanet( _planetC, _planetA );
//pointAtPlanet( _planetD, _planetA );
//pointAtPlanet( _planetE, _planetA );
pointAtPlanet( _planetF, _planetA );
}
public function pointAtPlanet( planetA:Planet, planetB:Planet ):void
{
var angleRads:Number = getAngle( planetA.x, planetA.y, planetB.x, planetB.y );
var angDegs:Number = angleRads*180/Math.PI;
planetA.text = Math.round( angDegs ).toString();
planetA.rotation = angDegs;
}
public function getAngle (x1:Number, y1:Number, x2:Number, y2:Number):Number
{
var dx:Number = x2 - x1;
var dy:Number = y2 - y1;
return Math.atan2(dy,dx);
}
}
}
import flash.text.TextFormatAlign;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Sprite;
class Planet extends Sprite
{
public var radius:Number;
private var _orbit:Number;
private var _txtContainer:Sprite = new Sprite();
private var _txt:TextField = new TextField();
public function Planet( radius:Number )
{
this.orbit = 0;
this.radius = radius;
graphics.beginFill( 0x009900 );
graphics.drawCircle( 0,0, radius );
graphics.endFill();
graphics.lineStyle( 2, 0x000000 );
graphics.moveTo( 0,0);
graphics.lineTo( radius, 0 );
_txt = new TextField();
_txt.defaultTextFormat = new TextFormat( "_sans", 8, 0xFFFFFF, false, false, false, null, null, TextFormatAlign.CENTER );
_txt.width = radius<<1;
_txt.height = radius<<1;
_txt.x =-radius;
_txt.y =-radius>>1;
_txtContainer.addChild( _txt );
addChild( _txtContainer );
}
public function get orbit():Number
{
return _orbit;
}
public function set orbit( value:Number ):void
{
_orbit = value;
this.rotation = value;
}
public function set text( value:String ):void
{
_txt.text = value;
}
override public function set rotation( value:Number ):void
{
_txtContainer.rotation = -value;
super.rotation = value;
}
}
