C++ 如何通过鼠标单击移动实体?
好的,我现在设法解决了这个问题,但现在又出现了另一个问题,我的机器人似乎自己移动到了另一个点,我不知道它从哪里来。这是我的密码 他的代码使这个机器人移动到我在地形上点击的位置C++ 如何通过鼠标单击移动实体?,c++,ogre,ogre3d,C++,Ogre,Ogre3d,好的,我现在设法解决了这个问题,但现在又出现了另一个问题,我的机器人似乎自己移动到了另一个点,我不知道它从哪里来。这是我的密码 他的代码使这个机器人移动到我在地形上点击的位置 bool DemoApp::nextLocation(void){ mDestination = mtoward; mRobotDir = mDestination - mRobotNode[0]->getPosition(); mDistance = mRobotDir.normalise(); return
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);
}
bool DemoApp::nextLocation(void){
mdestation=mtoward;
mRobotDir=mdestation-mRobotNode[0]->getPosition();
mDistance=mRobotDir.normalise();
返回true;
}
bool DemoApp::frameRenderingQueued(const Ogre::FrameEvent&evt)
{
if(mRobotDir==Ogre::Vector3::ZERO){
if(nextLocation()){
//设置行走动画
mAnimationState=mRobot[0]->getAnimationState(“行走”);
mAnimationState->setLoop(真);
mAnimationState->setEnabled(真);
}//如果
}否则{
Ogre::Real move=mWalkSpeed*evt.TimesInclastFrame;
mDistance-=移动;
if(mDistance setPosition(mDestination));
mRobotDir=Ogre::Vector3::ZERO;
//根据机器人是否有另一个行走点设置动画。
如果(!nextLocation()){
//设置空闲动画
mAnimationState=mRobot[0]->getAnimationState(“空闲”);
mAnimationState->setLoop(真);
mAnimationState->setEnabled(真);
}否则{
//旋转代码稍后将转到这里
Ogre::Vector3 src=mRobotNode[0]->getOrientation()*Ogre::Vector3::UNIT_X;
如果((1.0f+src.dotProduct(mRobotDir))<0.0001f){
mRobotNode[0]->偏航(食人魔::度(180));
}否则{
Ogre::四元数quat=src.getRotationTo(mRobotDir);
mRobotNode[0]->旋转(quat);
}//否则
}//否则
}否则{
mRobotNode[0]->translate(mRobotDir*move);
}//否则
}//如果
mAnimationState->addTime(evt.TimesInclastFrame);
}
这是我鼠标点击的raycast代码
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);
}
食人魔:地形*pTerrain=mTerrainGroup->getTerrain(0,0);
Ogre::Viewport*vp=this->mWindow->getViewport(0);
Ogre::Ray mouseRay=mCamera->getCameraToViewportRay(静态投影(mMouse->getMouseState().X.abs)/mMouse->getMouseState().width,静态投影(mMouse->getMouseState().Y.abs)/mMouse->getMouseState().height);
std::配对结果;
结果=pTerrain->光线相交(mouseRay,true,0);
如果(result.first=true)
{
mtoward=result.second-mRobotNode[0]->_getDerivedPosition();
mRobotNode[0]->translate(mtoward,Ogre::Node::TS_LOCAL);
}
是否使用某些值初始化
mtoward
?如果没有,则它可能包含随机(未初始化)数据。您需要添加一个返回false;
逻辑到nextLocation()
。谢谢您的帮助,请设法调整代码并使移动工作正常。谢谢。