Jonny Henly
Jonny Henly

Reputation: 4213

AS3 Object with Initial Position Entering Elliptical Orbit



So far I am able to make an object called satelite rotate around another object called turret in an elliptical manner. The only thing is that before the satelite starts rotating around the turret it has an initial position (Figure 1). However after the first frame the satelite is moved from it's initial position to somewhere else(Figure 2). I'm trying to have the satelite enter the elliptical rotation from it's initial position, not for it to be moved to another position and then start the elliptical rotation from that new position.

Figure 1 . . . . . . . . . . . . . . . . . . . Figure 2

Initial position at first frame After first frame resulting position http://s1.postimage.org/xscznk8bz/After_Initial_Position.png

Code :

private var fixedPoint1:int = 0;
private var fixedPoint2:int = 0;
private var currentDegrees:Number = 0;

private function onEnterFrame(event:Event):void
{   
    var dx:Number = turret.x - satelite.x; // This code deals with turret rotation
    var dy:Number = turret.y - satelite.y; // So does this code

    var angle:Number = Math.atan2(dy, dx); // As does this code

    turret.rotation = (angle * 180 / Math.PI); // Finally done with turret rotation code

    if (firstRun) {
        currentDegrees = angle * 180 / Math.PI;
        fixedPoint1 = satelite.x;
        fixedPoint2 = satelite.y;
        firstRun = false;
    }

    currentDegrees += 1;

    var radians:Number = currentDegrees * Math.PI / 180;
    var posX:Number = turret.x + Math.cos(radians) * (fixedPoint1);
    var posY:Number = turret.y + Math.sin(radians) * (fixedPoint2);

    satelite.x = posX;
    satelite.y = posY;      
}

I kind of understand why it's moving the satelite from it's initial position to it's elliptical position based off the equations used. I just can't figure out how to edit the equations or variables to have the ellipse "start from" (maybe encompass is a better word) the initial position.

Thank you for any help provided.

Update :

This code works, but it resembles a circular orbit instead of an elliptical orbit.
Thank you Markus Jarderot!

private function onEnterFrame(event:Event):void
{   
        var dx:Number = turret.x - satellite.x;
        var dy:Number = turret.y - satellite.y;

        var angle:Number = Math.atan2(dy, dx);

        turret.rotation = (angle * 180 / Math.PI);

        if (firstRun) {
             currentAngle = (180 * Math.PI/180); // Start out at pi
                                                 // instead of 0 for some reason?

            fixedPointX = turret.x - satellite.x; // xRadius = distance between turret.x and satellite.x
            fixedPointY = turret.y - satellite.y; // yRadius = distance between turret.y and satellite.y

            firstRun = false;
        }

        currentAngle += Math.PI / 180.0; // one degree
        if (currentAngle > Math.PI) currentAngle -= 2.0 * Math.PI;

        var cosAngle : Number = Math.cos(currentAngle);
        var sinAngle : Number = Math.sin(currentAngle);

        satellite.x = turret.x + cosAngle * fixedPointX + sinAngle * fixedPointY;
        satellite.y = turret.y - sinAngle * fixedPointX + cosAngle * fixedPointY;
}

Upvotes: 2

Views: 664

Answers (2)

Markus Jarderot
Markus Jarderot

Reputation: 89171

That looks like an incomplete version of a circular path.

var dx : Number = satellite.x - turret.x;
var dy : Number = satellite.y - turret.y;

var angle : Number = Math.atan2(dy, dx);

turrent.rotation = angle;

if (firstRun) {
    currentAngle = 0; // radians relative to fixed point

    fixedPointX = dx;
    fixedPointY = dy;

    firstRun = false;
}

currentAngle += Math.PI / 180.0; // one degree, or 1/360th cycle.
if (currentAngle > Math.PI) currentAngle -= 2.0 * Math.PI;

var cosAngle : Number = Math.cos(currentAngle);
var sinAngle : Number = Math.sin(currentAngle);

satellite.x = turret.x + cosAngle * fixedPointX + sinAngle * fixedPointY;
satellite.y = turret.y - sinAngle * fixedPointX + cosAngle * fixedPointY;

This is using a standard rotation matrix based on the moving angle. It will rotate in a circle around the turret, starting at the initial position.

If you want an elliptical orbit, you need to have one more reference point: Either another point along the orbit, or the initial velocity vector.

Demo: http://jsfiddle.net/MizardX/dRFay/

Upvotes: 1

Alex Costea
Alex Costea

Reputation: 91

Calculate an ellipse that is centered on turret and where satellite's initial position is on the ellipse curve then on each frame modify the ellipse's properties and shrink it to your desired final ellipse. This way the satellite will smoothly enter orbit. You can shrink/grow the coordinates with acceleration for a faster transition. So what you basically do is apply a transition on the ellipse until you reach you desired dimensions and you stop.

Upvotes: 1

Related Questions