我正在学校进行子弹碰撞的项目。如果直线是直线,但带有子弹扩展选项,则效果很好。因此,给出了角度,也给出了子弹的直线点,但是我如何从子弹碰撞的角度和距离创建一个随机点。这是子弹碰撞的代码。
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
(您要尝试的方向/射击)和一个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] 删除。
我来说两句