unity3d 获取路径跟随器的圆弧上的点

xa9qqrwz  于 2022-12-04  发布在  其他
关注(0)|答案(2)|浏览(181)

我正在Unity中尝试使用C#创建一个简单的路径系统。

到目前为止,一切都很完美。但是要创建一个路径跟随器,我需要跟随器每帧移动的位置。路径总是有一个半径,一个起点和一个长度,其余的未知。
现在,我如何计算路径跟随者在每一帧中的位置,即弧上的点?我需要一个函数,它将跟随者在路径上的百分比位置作为参数,并返回场景中的全局位置。下面是一个示例:圆弧的长度L、半径r和起点A都已给定。我试图计算的是随机点D:

我自己计算的那个点是一团乱麻,根本不起作用,尽管我已经做了一些研究。所以我在寻找一个简单易懂的解决方案。

8yparm6h

8yparm6h1#

the length of the arc L, the radius r and the starting point A are given

  • A circles circumference is defined by 2 * r * pi . A whole circumference, calculated using r=5 : fullCircumference = 31.4159265359 .
  • A partical circumference (the arc) is given by L , we know it's L/fullCircumference gives 0.21263598811 which is the fraction that L takes from the whole circumference.
  • We can convert the arc fraction to radians, given that a whole circle has 2PI -> 0.21263598811 * 2 * PI = 1.33603131627 rad
  • Now to the Starting Point A. I assume it's always ON the arc somewhere. Though, if we start with sin(0) and cos(0) we are at "12 o Clock":

    So we need to calculate the offset in radians around the circle.
  • Though you didn't give us B and I can't assume that though the radius alone. So I will just ignore this for now and offset the forumla by 90° (or half pi) to get to A
  • D's x & y would then be defined by: D(sin(x - pi/2) * r, cos(x - pi/2)) * r;
  • And if we enter the 1.336 rad we calculated, it looks about right:

The c# code would look like this (didn't compile though):

Vector2 pointOnCircle(float L, float r){
    
    float fullCircumference = 2f * Mathf.PI * r;
    float partialCircumference = L / fullCircumference;
    float arcRadians = partialCircumference * 2f * Mathf.PI;

    float circularOffset = Mathf.PI / 2f;
    Vector2 D = new Vector2();
    D.x = Mathf.Sin(arcRadians - circularOffset) * r;
    D.y = Mathf.Cos(arcRadians - circularOffset) * r;

    return D;
}
  • So we got C , now you want a random point D between A and C on that arc.
  • We now need to sample a point on the arc using a fraction between 0 and 1 (0 being at A, 1 being at C):
  • The arc's length is arcRadians in radians, so we can just mutiply that with 0..1 :

code:

Vector2 pointOnArc(float L, float r, float fraction){ // fraction is between 0 and 1
    
    float fullCircumference = 2f * Mathf.PI * r;
    float partialCircumference = L / fullCircumference;
    float arcRadians = partialCircumference * 2f * Mathf.PI;

    float circularOffset = Mathf.PI / 2f;
    Vector2 D = new Vector2();
    D.x = Mathf.Sin(fraction * arcRadians - circularOffset) * r;
    D.y = Mathf.Cos(fraction * arcRadians - circularOffset) * r;

    return D;
}

And there you go:

mbyulnm0

mbyulnm02#

Percentage介于0和1之间,length是弧长。您可以将中心移到0,0的中心,也可以偏移中心,使点从所需的位置开始。
如果你想让它在另一边,让半径为负。如果你想让它在另一边,让弧长为负。
终点是从弧的起点到弧的终点的Angular 。Angular 是终点乘以百分比。然后将极坐标(Angular 和半径)转换为笛卡尔坐标(x,y)并将其偏移圆心。

Vector2 getPoint(Vector2 center, float length, float radius, float percentage)
{
    float end = length / radius;
    float angle = end * percentage;

    float x = center.x + (Mathf.Cos(angle) * radius);
    float y = center.y + (Mathf.Sin(angle) * radius);

    return new Vector2(x, y);
}

相关问题