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

扎·蒂尼森

我正在学校进行子弹碰撞的项目。如果直线是直线,但带有子弹扩展选项,则效果很好。因此,给出了角度,也给出了子弹的直线点,但是我如何从子弹碰撞的角度和距离创建一个随机点。这是子弹碰撞的代码。

 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;
    }
}
ControlAltDel

这只是三角函数:平移极坐标和欧几里得坐标。

给定一个distance d,一个base angle b(您要尝试的方向/射击)和一个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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

获取与OrbitControl目标的距离和角度

如何计算数据框中两个连续点之间的距离和角度?

如何计算从点A到点B的给定距离和角度的弹丸运动的初始速度?

创建R函数以查找两个点之间的距离和角度

角度7和角度材料如何获取mat-select的选定选项文本而不是其值

如何获取具有角度指令和角度引导程序的按钮组的ui视图

在keras LSTM中计算向量之间的距离和角度

如何在Android中获取两点之间的距离?

阈值距离后如何获取新的点列表?

围绕给定的轴和角度的旋转点正确旋转对象

椭圆和角度旋转后的直线相交的点

我如何在 Google 地图地图上设置随机生成的点(同时指定距离)?

如何同时使用角度1.3.4和角度2

如何随机显示画线的距离?

如何将一个点以指定角度平移到指定距离?

从边界矩形和角度获取旋转的矩形坐标

获取 webelement 内的随机点

获取两点之间的距离/位移

获取两点之间的距离

如何使用电子和角度与NamedPipe连接

如何使用PANDAS插值数据和角度

如何在打字稿和角度中使用$ scope。$ on?

如何在Swift中确定滑动的速度和角度

如何找到图像中对象的中心和角度?

如何从点计算角度?

如何使用php使用纬度和经度获取点之间的距离?

如何获取距 postGIS 中某个点一定距离处的边界框坐标?

蟒蛇。如何从ax,y点列表和偏移距离获取偏移样条曲线的x,y坐标

材料和角度 2