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

时间:2016-10-25 14:43:55

标签: java math 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;
    }
}

1 个答案:

答案 0 :(得分:1)

这只是三角学:翻译极坐标和欧几里德坐标。

给定一个distance d,一个base angle b(您尝试去/拍摄的方向)和一个spread angle s(基准角度的范围/误差),公式为< / p>

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

相关问题