如何通过鼠标单击移动实体?

时间:2014-05-15 12:29:31

标签: c++ ogre ogre3d

好吧,我现在设法找出问题,但现在出现了另一个问题,我的机器人似乎自己移动到另一个点,我不知道它来自哪里。这是我的代码

他的代码使这个机器人移动到我点击地形的位置。

 bool DemoApp::nextLocation(void){
mDestination = mtoward; 
mRobotDir = mDestination - mRobotNode[0]->getPosition();
mDistance = mRobotDir.normalise();
return true;
}

 bool DemoApp::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
 if (mRobotDir == Ogre::Vector3::ZERO) {
    if (nextLocation()) {
        // Set walking animation
        mAnimationState = mRobot[0]->getAnimationState("Walk");
        mAnimationState->setLoop(true);
        mAnimationState->setEnabled(true);              
    }//if
}else{
    Ogre::Real move = mWalkSpeed * evt.timeSinceLastFrame;
    mDistance -= move;
    if (mDistance <= 0.0f){                 
        mRobotNode[0]->setPosition(mDestination);
        mRobotDir = Ogre::Vector3::ZERO;                
        // Set animation based on if the robot has another point to walk to. 
        if (!nextLocation()){
            // Set Idle animation                     
            mAnimationState = mRobot[0]->getAnimationState("Idle");
            mAnimationState->setLoop(true);
            mAnimationState->setEnabled(true);
        }else{
            // Rotation Code will go here later
            Ogre::Vector3 src = mRobotNode[0]->getOrientation() * Ogre::Vector3::UNIT_X;
            if ((1.0f + src.dotProduct(mRobotDir)) < 0.0001f) {
                mRobotNode[0]->yaw(Ogre::Degree(180));                      
            }else{
                Ogre::Quaternion quat = src.getRotationTo(mRobotDir);
                mRobotNode[0]->rotate(quat);
            } // else
        }//else
    }else{
        mRobotNode[0]->translate(mRobotDir * move);
    } // else
} // if
mAnimationState->addTime(evt.timeSinceLastFrame);
}

这是鼠标点击的光线投射代码

               Ogre::Terrain* pTerrain = mTerrainGroup->getTerrain(0, 0);
               Ogre::Viewport* vp = this->mWindow->getViewport(0);
               Ogre::Ray mouseRay = mCamera->getCameraToViewportRay(static_cast<float>(mMouse->getMouseState().X.abs)/mMouse->getMouseState().width, static_cast<float>(mMouse->getMouseState().Y.abs)/mMouse->getMouseState().height);

                     std::pair <bool, Ogre::Vector3> result;
                     result = pTerrain->rayIntersects(mouseRay, true, 0);
            if (result.first = true)
            {

                mtoward = result.second - mRobotNode[0]->_getDerivedPosition();
                mRobotNode[0]->translate(mtoward, Ogre::Node::TS_LOCAL);
            }

0 个答案:

没有答案