简体   繁体   中英

ActionScript 3 Trig Equations to create Ellipse

I am working with this function that moves an object around a center point in a perfect circle and am trying to modify it to move it in an oval shape that is 1/2 as high as it is wide?

Basically, I have a set speed

var myVelocity:Number = 0.25;

then I calculate my Sine and Cosine based on the speed

var myCos:Number = Math.cos(myVelocity);
var mySin:Number = Math.sin(myVelocity);

then I figure the distance of the the object from a fixed center points along each axis and

var x1:Number = myBall.x - centerX;
var y1:Number = myBall.y - centerY;
var x2:Number = myCos * x1 - mySin * y1;
var y2:Number = myCos * y1 + mySin * x1;
myBall.x = centerX + x2;
myBall.y = centerY + y2;

I have another function that figures x and y based upon myBall.x = centerX + cos(angle) * radius; that is easy enough to modify the radius to become an ellipse, but is there an easy way to mod the one above to become an ellipse? I want to do this to be more efficient with the code and reduce the number of math function calls

This question is nearly verbatim to what Keith Peters covers in Foundation Actionscript 3.0: Making Things Move!

I've whipped up a quick snippet for you from the book.

import flash.display.Shape;
import flash.events.Event;

var box:Shape = new Shape();
box.graphics.beginFill(0xFF0000);
box.graphics.drawRect(0, 0, 50, 50);
box.graphics.endFill();
addChild(box);
var angle:Number = 0;
var centerX:Number = stage.stageWidth/2 - (box.width/2);
var centerY:Number = stage.stageHeight/2 - (box.height/2)
var radiusX:Number = 200;
var radiusY:Number = 100;
var speed:Number = .1;
stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

function loop(evt:Event):void
{
    box.x = centerX + Math.sin(angle) * radiusX;
    box.y = centerY + Math.cos(angle) * radiusY;
    angle += speed;
}

Result: http://www.swfupload.com/view/155573.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM