如何从距离和角度获取随机点?

How to get random point from distance and angle?

我在学校工作,负责一个子弹碰撞项目。 如果这条线是直的,它就可以正常工作,但一个选项是 bulletspread。 所以角度是给定的,子弹所在的直线点也是给定的,但是我怎样才能从角度和子弹碰撞的距离创建一个随机点。 这是子弹碰撞的代码。

 private void setEndPoint()
 {
    endPoint = new Point(beginPoint.x, beginPoint.y);

    switch (direction)
    {
        default:
            break;
        case UP:
            while (endPoint.y > 0)
            {
                if (tileEmptyCheck(endPoint.x, endPoint.y))
                {
                    endPoint.y -= 1;
                }
                else
                {
                    fire();
                    break;
                }
            }
            break;
        case LEFT:
            while (endPoint.x > 0)
            {
                if (tileEmptyCheck(endPoint.x, endPoint.y))
                {
                    endPoint.x -= 1;
                }
                else
                {
                    fire();
                    break;
                }
            }
            break;
        case DOWN:
            while (endPoint.y < map.getHeightInTiles() * GameState.TILESIZE)
            {
                if (tileEmptyCheck(endPoint.x, endPoint.y))
                {
                    endPoint.y += 1;
                }
                else
                {
                    fire();
                    break;
                }
            }
            break;
        case RIGHT:
            while (endPoint.x < map.getWidthInTiles() * GameState.TILESIZE)
            {
                if (tileEmptyCheck(endPoint.x, endPoint.y))
                {
                    endPoint.x += 1;
                }
                else
                {
                    fire();
                    break;
                }
            }
            break;
        case RIGHT_UP:
            while (endPoint.y > 0 && endPoint.x < map.getWidthInTiles() * GameState.TILESIZE)
            {
                if (tileEmptyCheck(endPoint.x, endPoint.y))
                {
                    endPoint.y -= 1;
                    endPoint.x += 1;
                }
                else
                {
                    fire();
                    break;
                }
            }
            break;
        case RIGHT_DOWN:
            while (endPoint.y < map.getHeightInTiles() * GameState.TILESIZE &&
                    endPoint.x < map.getWidthInTiles() * GameState.TILESIZE)
            {
                if (tileEmptyCheck(endPoint.x, endPoint.y))
                {
                    endPoint.y += 1;
                    endPoint.x += 1;
                }
                else
                {
                    fire();
                    break;
                }
            }
            break;
        case LEFT_DOWN:
            while (endPoint.y < map.getHeightInTiles() * GameState.TILESIZE &&
                    endPoint.x > 0)
            {
                if (tileEmptyCheck(endPoint.x, endPoint.y))
                {
                    endPoint.y += 1;
                    endPoint.x -= 1;
                }
                else
                {
                    fire();
                    break;
                }
            }
            break;
        case LEFT_UP:
            while (endPoint.y > 0 &&
                        endPoint.x > 0)
            {
                if (tileEmptyCheck(endPoint.x, endPoint.y))
                {
                    endPoint.y -= 1;
                    endPoint.x -= 1;
                }
                else
                {
                    fire();
                    break;
                }
            }
            break;
    }
}

这就是三角函数:转换极坐标和欧氏坐标。

给定一个 distance d、一个 base angle b(您尝试 go/shoot 的方向)和一个 spread angle s(距离底角的范围/误差),公式是

double rand = random.nextDouble();
int x = d * sin (b - s / 2 + s * (rand));
int y = d * cos (b - s / 2 + s * (rand));

因此,例如,如果底角为 Pi / 2(90 度),但散布可以从 3 * Pi / 8(67.5 度)变为 5 * Pi / 8(112.5 度),则

b = Pi / 2

s = (5 - 3) * Pi / 8 = 2 * Pi / 8 = Pi / 4