游戏计算

时间:2012-01-04 21:27:14

标签: iphone objective-c angle ellipse

所以我有一个椭圆:

RADIUS_X = 100.0f;
RADIUS_Y = 30.0f;

我想沿着这个椭圆均匀分布3个物体,当我移动物体时,其他2个物体也会移动相同的距离。

在我为X和Y设置相同的半径之前,每次移动一个物体时,我都会得到该物体角度的角度差以及之前的角度,然后我会使用这个差异将其添加到其他两个对象。这完全没问题,因为

(X,Y) = (cos(angleDifference)*RADIUS_X ,sin(angleDifference)*RADIUS_Y)..

但是现在我的RADIUS_XRADIUS_Y不一样,但效果并不理想。

以下代码:

RADIUS_Y = 30.0f;
RADIUS_X = 100.0f;

float oldAngle = [Math arcTangent:[mainVisual getYCenter] X:[mainVisual getXCenter]]; // This just gets the angle of the main object.           
float newAngle = -1.57; // This is the angle I want the main visual to move to
float tempAngle = newAngle - oldAngle; // I use tempAngle as the difference.

// Get it's (X,Y) coordinate with the new centered angle.
float yPosition = [Math sin:newAngle]*RADIUS_Y;
float xPosition = [Math cos:newAngle]*RADIUS_X;
// Set the main visual to the center.
[self setPosition:mainVisual :yPosition :xPosition];

 // for even movement along the circle.
 // iconObjectList = list of visuals(objects).
for (int i=0; i < [iconObjectList count]; i++) 
{
    Visual* v = [[iconObjectList objectAtIndex:i]getVisual];
    if(v != mainVisual) // Because I have already set the main visual
    {
        float xPos = [v getXCenter];
        float yPos = [v getYCenter];
        float angle = [Math arcTangent:yPos X:xPos]; //This just gets the angle
        angle += tempAngle; // This is where I added the difference to the current visuals angle. 

        // The code below is where i believe the problem is.
        float yPosition = [Math sin:angle]*RADIUS_Y;
        float xPosition = [Math cos:angle]*RADIUS_X;
        [self setPosition:v :yPosition :xPosition];
    }
}

1 个答案:

答案 0 :(得分:1)

也许你只是忘了添加中心点:

float yPosition = xPos + [Math sin:angle] * RADIUS_Y;
float xPosition = yPos + [Math cos:angle] * RADIUS_X;

<强> [编辑]

这是你设置椭圆的3个顶点(interaction = 3)的方法:

        var gangle:Number = 0;
        private function draw():void
        {
            var rx:Number = 50;
            var ry:Number = 25;
            var interaction:Number = 3;
            var ang:Number = (360/interaction) * Math.PI / 180; // convert to radians

            for (var i:int = 0; i < interaction; i++)
            {
                var a:Number = gangle + ang * i;
                var xx:Number = Math.cos(a) * rx;
                var yy:Number = Math.sin(a) * ry;

                // xx and yy are your new position for each Vertex
            }

            gangle += .1;  // performs an animation each frame.
        }

也许你的代码中存在角度问题。也许这个例子可能会启发你。但它不在Objective-c中,对不起。