我一直在尝试为小行星游戏制作人工智能,但已陷入困境。游戏中的其他一切都会建立起来(例如,小行星创建并沿着随机方向前进,碰撞检测,船和子弹)。AI每隔150ms循环一次,只能告诉船左转,右转,向上移动并拍摄。检测对象是否会在2D空间中发生碰撞
这里是我的代码片段,我使用至今:
// Convert points to positive
float asteroid_x = Math.Abs(asteroid.X);
float asteroid_y = Math.Abs(asteroid.Y);
float ship_x = Math.Abs(ship.X);
float ship_y = Math.Abs(ship.Y);
// Calculate difference in X and Y using positive values
float dx = asteroid_x - ship_x;
float dy = asteroid_y - ship_y;
// Get velocities of asteroid
float vx = asteroid.VX;
float vy = asteroid.VY;
使用这些变量(DX,DY,VX,VY)我需要计算,如果小行星会打船。我一直在尝试使用三角函数,但它一直认为它会在没有时触及。我一直在试图使用的公式为:
float equation1a = (dy/vy) - (dx/vx);
float equation1 = ((dy/dx) - (vy/vx));
我则一直在试图使用IF语句,以确定是否它会打或没有,但我不知道如何去做正常。在这次IF声明之后,我想进入AI来转动和射击小行星或逃避它。
感谢您的任何帮助。
编辑:船舶或小行星的X和Y值也可以是负值,船在起点(0,0)上开始。
EDIT2:这一切变得有点进一步,现在又拿出这样的:
// Determine if asteroid will collide
int turns_ahead = 100;
bool collision = false;
int least_turns = 500;
for (int i = 0; i < turns_ahead; i++)
{
PointF asteroid_pos_next = NextPosition(new PointF(asteroid.X, asteroid.Y), new PointF(asteroid.VX, asteroid.VY), i);
if (asteroid.CollidesWith(asteroid_pos_next.X, asteroid_pos_next.Y, ship.R))
{
collision = true;
}
}
而接下来的位置使用此功能计算出:
// -----------------------------------------------------------------
// Function to find position of object in X turns (turns_ahead)
// -----------------------------------------------------------------
private static PointF NextPosition(PointF current_pos, PointF velocity, int turns_ahead)
{
float next_x_pos = current_pos.X + (velocity.X * turns_ahead);
float next_y_pos = current_pos.Y + (velocity.Y * turns_ahead);
// IFs to deal with screen wrap-around
if (next_x_pos < -Form1.maxX)
{
next_x_pos = Form1.maxX;
if (next_y_pos < 0)
next_y_pos += Form1.maxY;
else
next_y_pos -= Form1.maxY;
}
else if (next_x_pos > Form1.maxX)
{
next_x_pos = -Form1.maxX;
if (next_y_pos < 0)
next_y_pos += Form1.maxY;
else
next_y_pos -= Form1.maxY;
}
if (next_y_pos < -Form1.maxY)
{
next_y_pos = Form1.maxY;
if (next_x_pos < 0)
next_x_pos += Form1.maxX;
else
next_x_pos -= Form1.maxX;
}
else if (next_y_pos > Form1.maxY)
{
next_y_pos = -Form1.maxY;
if (next_x_pos < 0)
next_x_pos += Form1.maxX;
else
next_x_pos -= Form1.maxX;
}
PointF next_pos = new PointF(next_x_pos, next_y_pos);
return next_pos;
}
的collidesWith是一个函数在其他地方用于检测子弹/船是否撞击到小行星。我不确定我的下一个位置函数是否正确,但是如果小行星包围X/Y值似乎卡住了maxX/maxY值?
这与AI完全没有关系(根本不是)。 – ziggystar 2012-03-19 20:29:27
这是一个班级项目吗?我刚才看到一个类似的问题:http://stackoverflow.com/questions/9773255/c-sharp-collision-test-of-a-ship-and-asteriod – 2012-03-19 20:34:58
当船只必须搬出时,AI部分进来/拍摄。是的,它是一个课程项目的一部分,对不起,我应该说。我的讲师给我们提供了一些错误的公式,所以很容易混淆,尽管 – 2012-03-19 20:38:18