소행성 게임에서 인공 지능을 만들려고했지만 멈추었습니다. 게임의 다른 모든 것들이 설정됩니다 (즉, 소행성이 생성되어 임의 방향으로 가면서 충돌 탐지, 우주선 및 총알이 생성됩니다). 인공 지능은 매 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)를 사용하여 : 여기
내가 지금까지 사용하고 내 코드의 조각이다 . 나는 삼각법을 사용하려고 노력해 왔지만 그렇지 않을 때 타격을 가할 것이라고 생각하고 있습니다. 그때는 칠 것 아닌지의 여부를 결정하기 위해 IF 문을 사용하려고했습니다 float equation1a = (dy/vy) - (dx/vx);
float equation1 = ((dy/dx) - (vy/vx));
하지만 난 그것에 대해 이동하는 방법을 잘 모르겠어요 : 내가 사용하려고했습니다 방정식은 정확히. 이 IF 문 다음에 나는 AI로 들어가서 소행성을 돌리거나 쏘거나 피하려고한다.
도움 주셔서 감사합니다.
EDIT : 우주선이나 소행성의 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
이것은 수업 프로젝트입니까? 방금 전에 비슷한 질문을 보았습니다. http://stackoverflow.com/questions/9773255/c-sharp-collision-test-of-a-ship-and-asteriod –
AI 부품은 배가 밖으로 나가야 할 때 들어옵니다 길/촬영. 그리고 네, 그것은 수업 프로젝트의 일부입니다, 미안했을 것입니다. 내 강사가 우리에게 틀린 수식을 주었기 때문에 꽤 혼란 스러웠지만 지금은 작동하고있는 것처럼 보입니다 –