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

3

我正在为学校做一个有关子弹碰撞的项目。如果线是直的,它可以正常工作,但是还有一种选项是子弹扩散。因此,角度已经给定,子弹所在的直线点也已经给定,但是如何从角度和距离创建一个随机点并实现子弹碰撞呢?以下是子弹碰撞的代码:

 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个回答

2
这只是三角学:翻译极坐标和直角坐标。
给定一个距离d,一个基础角度b(你要前往/射击的方向)和一个扩散角度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

基角和扩散角有什么区别? - Ties Theunissen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接