ActionScript 3 Trig Equations to create Ellipse

為{幸葍}努か 提交于 2020-01-17 02:15:02

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/4986998/actionscript-3-trig-equations-to-create-ellipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!