actionscript
How To Orbit In AS3
Making some display object orbit around some other display object in ActionScript3.0 is easy.
Here is basic orbit function where all the magic happens:
Function parameters:
planet - MovieClip around which another MovieClip is orbiting
sat (satellite) - MovieClip which orbits
radius - distance from satellite to planet
speed - orbit speed (can be positive or negative value)
Orbiting direction depends on speed value. Positive speed value will move satellite in counter-clockwise direction and negative speed value will move satellite in clockwise direction.
Advanced approach can use satellites as new planets, just as in star system you can have star in a center, some planets orbiting around star and than you can add moons around those planets orbiting with different speed and on different distances from planets.
Click on image to download example.

*_*
Here is basic orbit function where all the magic happens:
function orbit(planet:MovieClip, sat:MovieClip, radius:Number, speed:Number):void
{
var currentDegrees:Number = 0;
this.addEventListener(Event.ENTER_FRAME, doEveryFrame);
function doEveryFrame(event:Event):void
{
currentDegrees += speed;
var radians:Number = getRadians(currentDegrees);
var posX:Number = planet.x + Math.sin(radians) * radius;
var posY:Number = planet.y + Math.cos(radians) * radius;
sat.x = posX;
sat.y = posY;
}
function getRadians(degrees:Number):Number
{
return degrees * Math.PI / 180;
}
}
Function parameters:
planet - MovieClip around which another MovieClip is orbiting
sat (satellite) - MovieClip which orbits
radius - distance from satellite to planet
speed - orbit speed (can be positive or negative value)
Orbiting direction depends on speed value. Positive speed value will move satellite in counter-clockwise direction and negative speed value will move satellite in clockwise direction.
Advanced approach can use satellites as new planets, just as in star system you can have star in a center, some planets orbiting around star and than you can add moons around those planets orbiting with different speed and on different distances from planets.
Click on image to download example.

*_*
actionscript
animations
AS3.0
code
downloads
examples
flash
game development
games
math
open source
programming
tutorials

Post a Comment
4 Comments
Great short and simple code. Perfect for my needs! Thank you very much!
ReplyDeleteHow do I even get this to work, what variables do I need to add/change? Also your example is carried out on a new scene, so the code is presumably entered on that scene, but if I want this orbit to take place within an object, do I need to do anything differently?
ReplyDeleteHi Pake, thanks for your comment.
ReplyDeleteThis is just a function which allows your orbital movement. You can paste code within main timeline if you work with Flash IDE or as part of a class, not sure what you prefer.
Did you downloaded example? It has function code on first frame of Timeline and function call. Parameters of function call are names of movie clips dropped on Stage. You can also use linkage names of library items, if you wish.
Hope this helps. If not, email me, I'll send you my FLA ;)
wow, pretty cool snippet, thanks a bunch for sharing some knowledge :)
ReplyDeleteThanks for sharing your thoughts !