在3中,我提到检测到碰撞时候,后移一个单位,摄像机会抖动,现在通过记录摄像机上步移动偏移量,如果检测到碰撞则反移回来,可以解决这个问题。
下面是修改后的代码:
Code
bool frameRenderingQueued(const FrameEvent& evt)
{
if( ExampleFrameListener::frameRenderingQueued(evt) == false )
return false;
// clamp to terrain
static Ray updateRay;
updateRay.setOrigin(mCamera->getPosition());
updateRay.setDirection(Vector3::NEGATIVE_UNIT_Y);
raySceneQuery->setRay(updateRay);
RaySceneQueryResult& qryResult = raySceneQuery->execute();
RaySceneQueryResult::iterator i = qryResult.begin();
if (i != qryResult.end() && i->worldFragment)//把摄像机定在地形10个单位高的地方。
{
mCamera->setPosition(mCamera->getPosition().x,
i->worldFragment->singleIntersection.y + 10,
mCamera->getPosition().z);
}
//创建球形查询器,第二个参数表示掩码,默认情况下为-1
Vector3 oldPos = mCamera->getPosition();
SphereSceneQuery * pQuery=mSceneMgr->createSphereQuery(Sphere(mCamera->getPosition(),10),0x01);
SceneQueryResult QResult=pQuery->execute();
for (std::list<MovableObject*>::iterator iter = QResult.movables.begin(); iter != QResult.movables.end();++iter)
{
MovableObject* pObject=static_cast<MovableObject*>(*iter);
if(pObject)
{
if(pObject->getMovableType()=="Entity")
{
Entity* ent = static_cast<Entity*>(pObject);
//if(ent->getName()=="Head")
//{
// //m_node->setScale(10,10,10);
// pObject->getParentNode()->scale(0.5,0.5,0.5);
// break;
//}
//Ogre::Vector3 v = mCamera->getPosition();
//Ogre::Vector3 d = mCamera->getDirection();
//v = v + d*(-1);
//mCamera->setPosition(v);
mCamera->moveRelative( - mTranslateVector);
}
}
}
return true;
}
bool frameRenderingQueued(const FrameEvent& evt)
{
if( ExampleFrameListener::frameRenderingQueued(evt) == false )
return false;
// clamp to terrain
static Ray updateRay;
updateRay.setOrigin(mCamera->getPosition());
updateRay.setDirection(Vector3::NEGATIVE_UNIT_Y);
raySceneQuery->setRay(updateRay);
RaySceneQueryResult& qryResult = raySceneQuery->execute();
RaySceneQueryResult::iterator i = qryResult.begin();
if (i != qryResult.end() && i->worldFragment)//把摄像机定在地形10个单位高的地方。
{
mCamera->setPosition(mCamera->getPosition().x,
i->worldFragment->singleIntersection.y + 10,
mCamera->getPosition().z);
}
//创建球形查询器,第二个参数表示掩码,默认情况下为-1
Vector3 oldPos = mCamera->getPosition();
SphereSceneQuery * pQuery=mSceneMgr->createSphereQuery(Sphere(mCamera->getPosition(),10),0x01);
SceneQueryResult QResult=pQuery->execute();
for (std::list<MovableObject*>::iterator iter = QResult.movables.begin(); iter != QResult.movables.end();++iter)
{
MovableObject* pObject=static_cast<MovableObject*>(*iter);
if(pObject)
{
if(pObject->getMovableType()=="Entity")
{
Entity* ent = static_cast<Entity*>(pObject);
//if(ent->getName()=="Head")
//{
// //m_node->setScale(10,10,10);
// pObject->getParentNode()->scale(0.5,0.5,0.5);
// break;
//}
//Ogre::Vector3 v = mCamera->getPosition();
//Ogre::Vector3 d = mCamera->getDirection();
//v = v + d*(-1);
//mCamera->setPosition(v);
mCamera->moveRelative( - mTranslateVector);
}
}
}
return true;
}