0

我有两点,一个起始位置和一个目标位置(动态)。我想像在一级方程式比赛中一样培养球员。即第二个在第一个的右边和后面,第三个在第二个的左边和后面,依此类推。我已经确定了角度,所以他们面向目标点。

我不知道如何相对于轴上的线移动。我认为我的距离将它侧向移动,但我不是 100% 确定.. 我也太愚蠢了,无法弄清楚如何垂直于新点,即使它可能只是在某处添加了一个减号。

好吧,我希望有人可以帮助我解决这个问题,非常感谢。

注意:代码使用 Pawn,一种类似 C 的脚本语言。

    new x1 = RaceCheckpoints[0][0]//startpoint x
    new y1 = RaceCheckpoints[0][1]//startpoint y
    new x2 = RaceCheckpoints[1][0]//goalpoint x
    new y2 = RaceCheckpoints[1][1]//goalpoint y
    new dist = 2;
    new pos = 0;
    new x3, y3, x4, y4, a, b, norm;
    x3 = (x1 + x2) / 2;
    y3 = (y1 + y2) / 2;
    a = y1 - y2;
    b = x2 - x1;
    norm = sqrt(a*a + b*b);
    a = a / norm;
    b = b / norm;
    x3 = x3 + a * -dist;
    y3 = y3 + b * -dist;
    x4 = x3 + a * 2 * dist;
    y4 = y3 + b * 2 * dist;
    for(new i;i<MAX_PLAYERS;i++)
    {
        if(RaceParticipant[i] != 0)
        {
            if(IsPlayerInAnyVehicle(i)) PlayerVehicles[i]=GetPlayerVehicleID(i);
            else PlayerVehicles[i]=0;
            if (pos = 0)//left lane
            {
            SetPlayerPosFindZ(playerid, x3, y3, RaceCheckpoints[0][2]+10);
            new angle = atan2(y2 - x3, x2 - y3) * 180 / PI;
            SetPlayerFacingAngle(i,angle);
            pos++;
            }

            if (pos = 1)//right lane
            {
            SetPlayerPosFindZ(playerid, x4, y4, RaceCheckpoints[0][2]+10);
            new angle = atan2(y2 - x4, x2 - y4) * 180 / PI;
            SetPlayerFacingAngle(i,angle);
            pos--;
            }

        }
    }
4

1 回答 1

0

假设您的目标直接位于 x 方向。那么,起点和目标之间的向量为 (0, 1),它与 ​​x 轴之间的角度当然为零。假设每辆车都有一行ix和一列iy,第一辆车有行和列 0。

那么任何一辆车到第一辆车的距离是

xx = - ix * dx - iy * dd;
yy =  - iy * dy;

其中dx和是汽车dy之间dd的度量:

    --------000-------------------
     |      000                dd
     |      000        111--------
     dx     000        111
     |      000        111
     |                 111
     |                 111
    --------222
            222
            222        333
            222        333
            222        333
            |          333
            |          333
            |          |
            |--- dy ---|

现在假设您的目标位于其他地方,并且起点和目标之间的向量是 ( vx, vy)。该向量与 x 轴之间的夹角为a。你必须旋转你的xxand yy

xx' = cos(a) * xx - sin(a) * yy
yy' = sin(a) * xx + cos(a) * yy

你也可以用矩阵表示法写这个:

{P'} = [C] * {P}

其中{P}{P'}是您的未旋转点和旋转点,[C]是旋转矩阵:

      |  cos(a)     - sin(a)  |
[C] = |                       |
      |  sin(a)       cos(a)  |

你的角度是

a = atan2(vy, vx)

但你真的不需要这里的角度。如果将 vor 向量 ( vx, vy) 归一化,使其成为单位向量,vx并且vy已经是旋转的余弦和正弦。

最后一步是将起点添加到旋转位置。将所有这些放在一起(在 C 中,而不是 Pawn 中):

double dx = 8.0;        // x distance between 1st and 3rd car
double dy = 5.0;        // y distance between 1st and 2nd car
double dd = 1.5;        // x distance between 1st and 2nd car

double sx = 118.0;      // start point, i.e. position of 1st car
double sy = 6.0;

double gx = 240.0;      // goal point
double gy = 60.0;

int ncar = 8;           // number of cars

double vx = gx - sx;    // vector between start and goal
double vy = gy - sy;
double vv;

double acos;            // sine and cosine of the angle between
double asin;            // (vx, vy) and (1, 0)

double cx[ncar];        // car positions
double cy[ncar];

int i;

vv = sqrt(vx*vx + vy * vy);             // normalise vector
acos = vx / vv;                         // determine rotation cosines
asin = vy / vv; 

for (i = 0; i < ncar; i++) {
    int ix = i / 2;                     // grid index row
    int iy = i % 2;                     // grid index column

    double xx = - ix * dx - iy * dd;    // unrotated car pos,
    double yy =  - iy * dy;             // 1st car a (0, 0)

    cx[i] = sx + acos * xx - asin * yy;
    cy[i] = sy + asin * xx + acos * yy;
}
于 2014-03-08T07:09:37.970 回答