您的位置:首页 > 其它

学习OGRE制作简单人物行走demo(二)

2009-08-14 10:55 429 查看
汗,写了一天,居然一点提交,毛都没留下。郁闷。

重来吧。

为了实现通过键盘控制角色移动,首先要获取键盘消息,因为默认情况下已经对键盘鼠标做了处理,所以首先先清除默认的。

创建TutorialFrameListener

class TutorialFrameListener : public ExampleFrameListener, public OIS::MouseListener, public OIS::KeyListener
{
public:
TutorialFrameListener(RenderWindow* win, Camera* cam, SceneManager *sceneMgr)
: ExampleFrameListener(win, cam, true, true)
{
// Populate the camera and scene manager containers
mCamNode = cam->getParentSceneNode();
mSceneMgr = sceneMgr;
mPlayerNode = mSceneMgr->getSceneNode("NinjaNode");
// 设置旋转和移动速度
mRotate = 0.13;
mMove = 50;
// 继续渲染
mContinue = true;
mMouse->setEventCallback(this);
mKeyboard->setEventCallback(this);
mDirection = Vector3::ZERO;
}

bool frameStarted(const FrameEvent &evt)
{
if(mMouse)
mMouse->capture();
if(mKeyboard)
mKeyboard->capture();
mPlayerNode->translate(mDirection * evt.timeSinceLastFrame, Node::TS_LOCAL);
return mContinue;
}
bool frameRenderingQueued(const FrameEvent& evt)
{
return true;
}
// MouseListener
bool mouseMoved(const OIS::MouseEvent &e) { return true; }
bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) { return true; }
// KeyListener
bool keyPressed(const OIS::KeyEvent &e)
{
switch (e.key)
{
case OIS::KC_ESCAPE:
mContinue = false;
break;
case OIS::KC_UP:
case OIS::KC_W:
mDirection.z -= mMove;
break;
case OIS::KC_DOWN:
case OIS::KC_S:
mDirection.z += mMove;
break;
case OIS::KC_LEFT:
case OIS::KC_A:
mDirection.x -= mMove;
break;
case OIS::KC_RIGHT:
case OIS::KC_D:
mDirection.x += mMove;
break;
}
return true;
}
bool keyReleased(const OIS::KeyEvent &e)
{
switch (e.key)
{
case OIS::KC_UP:
case OIS::KC_W:
mDirection.z += mMove;
break;
case OIS::KC_DOWN:
case OIS::KC_S:
mDirection.z -= mMove;
break;
case OIS::KC_LEFT:
case OIS::KC_A:
mDirection.x += mMove;
break;
case OIS::KC_RIGHT:
case OIS::KC_D:
mDirection.x -= mMove;
break;
} // switch
return true;
}
protected:
Real mRotate;          // The rotate constant
Real mMove;            // The movement constant
SceneManager *mSceneMgr;   // The current SceneManager
SceneNode *mCamNode;   // The SceneNode the camera is currently attached to
SceneNode *mPlayerNode;
bool mContinue;        // Whether to continue rendering or not
Vector3 mDirection;     // Value to move in the correct direction
};


添加TutorialApplication::createFrameListener方法

void createFrameListener(void)
{
mFrameListener = new TutorialFrameListener(mWindow, mCamera, mSceneMgr);
mRoot->addFrameListener(mFrameListener);
}


我们就可以对人物进行左右滑动了。

代码步骤是:

1.覆盖ExampleFrameListener的方法

frameStarted, frameRenderingQueued.

我这里在frameRenderingQueued什么也不做,就已经把默认的处理全清除了。

在frameStarted里控制角色节点移动, 而移动的mDirection是通过键盘处理得到的.

如果按ESC键,frameStarted返回了mContinue,这时为false,所以程序退出了。

我们暂时只关心X,Z轴上的偏移.如果要弄飞的效果,就要考虑Y 轴了。

接下来是摄像头的控制,获得游戏一样的效果,可以转动,可以随着角色移动。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: