首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运动代码c++

运动代码c++
EN

Stack Overflow用户
提问于 2011-11-17 13:10:02
回答 6查看 3.6K关注 0票数 0

我正在用C++制作一个使用DirectX的游戏。我有一个基本的人工智能绘图。我想让人工智能在正方形中移动,例如:

  • AI沿Z轴向上移动到25,
  • 然后AI沿X轴移动25,
  • 再向下Z轴25,
  • 然后沿着X轴返回,直到它完成一个正方形的完整运动。

这是到目前为止我所得到的,这使得AI在Z轴上移动25,然后沿着Z轴一次又一次地向下移动25。

代码语言:javascript
复制
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;
}

提前谢谢你。

编辑:

代码语言:javascript
复制
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_Xghost_Z作为AI的翻译职位。

代码语言:javascript
复制
D3DXMatrixTranslation( &g_matLocal, ghost_X, 0, ghost_Z );
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-11-17 13:26:35

假设鬼魂从startx位置开始,startz

代码语言:javascript
复制
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;
}
票数 7
EN

Stack Overflow用户

发布于 2011-11-17 13:24:05

在正方形中而不是上下移动时,您有四种不同的运动状态,而不是只有两种,因此bool不足以存储当前的运动状态。一旦到达运动状态的结束条件,您就进入下一个移动状态(运算符++%在实现该状态时可能会很方便)。

票数 1
EN

Stack Overflow用户

发布于 2011-11-17 13:24:19

你需要考虑一个三维空间和坐标几何来理解和编程一个三维运动。

所以现在,它只是来回移动,因为运动是在x轴上表示的。

在2D空间上

代码语言:javascript
复制
X component of ghost movement + y component of ghost movement.

在3D上,你需要把这三者都用一个三维坐标系来表示。

喜欢

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8167615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档