我正在用C++制作一个使用DirectX的游戏。我有一个基本的人工智能绘图。我想让人工智能在正方形中移动,例如:
这是到目前为止我所得到的,这使得AI在Z轴上移动25,然后沿着Z轴一次又一次地向下移动25。
if (ghost_moves_forward == true)
{
ghost_Z -= ghost_movement;
}
else if (ghost_moves_forward == false)
{
ghost_Z += ghost_movement;
}
if (ghost_Z >= 25)
{
ghost_moves_forward = true;
}
if (ghost_Z <= -25)
{
ghost_moves_forward = false;
}提前谢谢你。
编辑:
float ghost_X = 0; //make the AI move along the x axis
float ghost_Z = 0; // makes the AI move along the Z axis
int ghost_movement = 1; // speed the AI moves
bool ghost_moves_forward = true; // true when AI moves forward, false when its moving sideways
bool ghost_moves_sideways = true;// true when moving sideways, false when moving forwards我使用ghost_X和ghost_Z作为AI的翻译职位。
D3DXMatrixTranslation( &g_matLocal, ghost_X, 0, ghost_Z );发布于 2011-11-17 13:26:35
假设鬼魂从startx位置开始,startz
static int square_state = 0;
// bottom left to top left is done
if(z <= startz-25 && square_state == 0)
square_state = 1;
// top left to top right is done
if(x >= startx+25 && square_state == 1)
square_state = 2;
// top right to bottom right is done
if(z >= startz && square_state == 2)
square_state = 3;
// bottom right to bottom left is done
if(x <= startx && square_state == 3)
square_state = 0;
switch(square_state)
{
case 0: // bottom left to top left
z -= movement;
break;
case 1: // top left to top right
x += movement;
break;
case 2: // top right to bottom right
z += movement;
break;
case 3: // bottom right to bottom left
x -= movement;
break;
}发布于 2011-11-17 13:24:05
在正方形中而不是上下移动时,您有四种不同的运动状态,而不是只有两种,因此bool不足以存储当前的运动状态。一旦到达运动状态的结束条件,您就进入下一个移动状态(运算符++和%在实现该状态时可能会很方便)。
发布于 2011-11-17 13:24:19
你需要考虑一个三维空间和坐标几何来理解和编程一个三维运动。
所以现在,它只是来回移动,因为运动是在x轴上表示的。
在2D空间上
X component of ghost movement + y component of ghost movement.在3D上,你需要把这三者都用一个三维坐标系来表示。
喜欢
x+y+z (done through matrices)在这里阅读本节以获得一个示例
c++ first person camera in directx
还有更多关于http://www.directxtutorial.com/tutorial9/b-direct3dbasics/dx9b5.aspx的信息
或者在这里http://msdn.microsoft.com/en-us/library/windows/apps/hh452775(v=vs.85).aspx
https://stackoverflow.com/questions/8167615
复制相似问题