【发布时间】:2011-11-17 13:10:02
【问题描述】:
我正在使用 DirectX 用 C++ 制作游戏。我画了一个基本的人工智能。我希望 AI 在一个正方形中移动,例如:
- AI 向上移动 Z 轴直到达到 25,
- 然后 AI 沿 X 轴移动 25,
- 然后沿 Z 轴向下 25,
- 然后沿 X 轴返回,直到完成正方形形状的完整移动。
这是我目前所拥有的;这使得 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 );
【问题讨论】:
-
我正在努力让我的头脑绕过 if 循环的其余部分,以使 AI 在方形中移动,而不仅仅是向前和向后
-
您将需要提供比这更多的代码。所有变量代表什么?这个代码片段是如何运行的(在循环中?递归调用?)
-
您不应该将您的布尔值与
true和false进行比较,它们可以直接在条件语句中使用:if (ghost_moves_forward)。您还在 else 中进行了无用的测试:如果ghost_moves_forward为真,则不能为假,因此您可以在 else 子句中删除if。 -
@Luc Touraille 也许副作用在 DK10 的代码中特别不稳定。那么测试实际上可能会做一些不平凡的事情。
标签: c++ directx artificial-intelligence