void TerrainManager::updateCameraHeight(const Ogre::FrameEvent& evt) { // clamp to terrain Ogre::Vector3 camPos = mCamera->getPosition(); Ogre::Ray ray; ray.setOrigin(Ogre::Vector3(camPos.x, mTerrainPos.y + 10000, camPos.z)); ray.setDirection(Ogre::Vector3::NEGATIVE_UNIT_Y); Ogre::TerrainGroup::RayResult rayResult = mTerrainGroup->rayIntersects(ray); Ogre::Real distanceAboveTerrain = 50; Ogre::Real fallSpeed = 300; Ogre::Real newy = camPos.y; if (rayResult.hit) { if (camPos.y > rayResult.position.y + distanceAboveTerrain) { mFallVelocity += evt.timeSinceLastFrame * 20; mFallVelocity = std::min(mFallVelocity, fallSpeed); newy = camPos.y - mFallVelocity * evt.timeSinceLastFrame; } newy = std::max(rayResult.position.y + distanceAboveTerrain, newy); mCamera->setPosition(camPos.x, newy, camPos.z); } }
void CombatCamera::ViewportRay(double screen_x, double screen_y, Ogre::Ray& ray) const { double out_origin[3]; double ray_direction[3]; ViewportRay(screen_x, screen_y, out_origin, ray_direction); ray.setOrigin(Ogre::Vector3(out_origin[0], out_origin[1], out_origin[2])); ray.setDirection(Ogre::Vector3(ray_direction[0], ray_direction[1], ray_direction[2])); }
// raycast from a point in to the scene. // returns success or failure. // on success the point is returned in the result. bool CollisionTools::raycastFromPoint(const Vector3 &point, const Vector3 &normal, Vector3 &result,unsigned long &target,float &closest_distance, const uint32 queryMask) { // create the ray to test static Ogre::Ray ray; ray.setOrigin(point); ray.setDirection(normal); return raycast(ray, result, target, closest_distance, queryMask); }
bool CollisionTools::raycastFromPoint(const Ogre::Vector3 &point, const Ogre::Vector3 &normal, Ogre::Vector3 &result,int &FaceIndex,Ogre::MovableObject* &target, float &closest_distance, const Ogre::uint32 queryMask) { // create the ray to test static Ogre::Ray ray; ray.setOrigin(point); ray.setDirection(normal); return raycast(ray, result,FaceIndex, target, closest_distance, queryMask); }
bool XYOrbitViewController::intersectGroundPlane( Ogre::Ray mouse_ray, Ogre::Vector3 &intersection_3d ) { //convert rays into reference frame mouse_ray.setOrigin( target_scene_node_->convertWorldToLocalPosition( mouse_ray.getOrigin() ) ); mouse_ray.setDirection( target_scene_node_->convertWorldToLocalOrientation( Ogre::Quaternion::IDENTITY ) * mouse_ray.getDirection() ); Ogre::Plane ground_plane( Ogre::Vector3::UNIT_Z, 0 ); std::pair<bool, Ogre::Real> intersection = mouse_ray.intersects(ground_plane); if (!intersection.first) { return false; } intersection_3d = mouse_ray.getPoint(intersection.second); return true; }
float CollisionTools::getTSMHeightAt(const float x, const float z) { float y=0.0f; static Ogre::Ray updateRay; updateRay.setOrigin(Ogre::Vector3(x,9999,z)); updateRay.setDirection(Ogre::Vector3::NEGATIVE_UNIT_Y); mTSMRaySceneQuery->setRay(updateRay); Ogre::RaySceneQueryResult& qryResult = mTSMRaySceneQuery->execute(); Ogre::RaySceneQueryResult::iterator i = qryResult.begin(); if (i != qryResult.end() && i->worldFragment) { y=i->worldFragment->singleIntersection.y; } return y; }
BOOL CCameraController::UpdateCameraControl() { if (m_bLocked) { return TRUE; } Vector3 vtNewPos = m_pBindObject->GetPosition(); if((m_vtBindObjPos == vtNewPos)&&(!m_bPositionUpate)&&!m_bMoveCameraFar&&!m_bMoveCameraNear) { return TRUE; } if(m_bMoveCameraNear) { m_fDistance -= 0.05; } if(m_bMoveCameraFar) { m_fDistance += 0.05; } m_vtBindObjPos = vtNewPos; m_vecDstPos = m_vecUnitDir * m_fDistance + m_vtBindObjPos; Ogre::Vector3 RayDir = m_vecDstPos-vtNewPos; RayDir.normalise(); Ogre::Ray SearchRay; SearchRay.setOrigin(vtNewPos); SearchRay.setDirection(RayDir); g_Debuginfo.PrintfInfo("m_vecDstPos x:%.2f-y:%.2f-z:%.2f!||", m_vecDstPos.x,m_vecDstPos.y,m_vecDstPos.z); Ogre::TerrainGroup::RayResult rayResult = CGameApp::GetGameApp()->m_World.m_pTerrainGroup->rayIntersects(SearchRay); if(rayResult.hit) { if((rayResult.position-vtNewPos).length()<m_fDistance) { m_vecDstPos = rayResult.position; m_vecDstPos .y += 1; g_Debuginfo.PrintfInfo("hit -- rayResult x:%.2f--y:%.2f--z:%.2f!\n", rayResult.position.x,rayResult.position.y,rayResult.position.z); } else { g_Debuginfo.PrintfInfo("hit --rayResult x:1--y:1--z:1!\n"); } } else { g_Debuginfo.PrintfInfo("unhit --rayResult x:0--y:0--z:0!\n"); } m_pCamera->lookAt(vtNewPos+Vector3(0,1.5,0)); m_pCamera->setPosition(m_vecDstPos); m_bPositionUpate = FALSE; return TRUE; }
//------------------------------------------------------------------------------------- bool SampleApp::frameRenderingQueued(const Ogre::FrameEvent& evt) { bool ret = BaseApplication::frameRenderingQueued(evt); if (!mFly) { // clamp to terrain Ogre::Vector3 camPos = mActiveCamera->getPosition(); Ogre::Ray ray; ray.setOrigin(Ogre::Vector3(camPos.x, 10000, camPos.z)); ray.setDirection(Ogre::Vector3::NEGATIVE_UNIT_Y); Ogre::TerrainGroup::RayResult rayResult = mLoader->getTerrainGroup()->rayIntersects(ray); Ogre::Real distanceAboveTerrain = 1.4f; Ogre::Real fallSpeed = 200; Ogre::Real newy = camPos.y; if (rayResult.hit) { if (camPos.y > rayResult.position.y + distanceAboveTerrain) { mFallVelocity += evt.timeSinceLastFrame * 10; mFallVelocity = std::min(mFallVelocity, fallSpeed); newy = camPos.y - mFallVelocity * evt.timeSinceLastFrame; } newy = std::max(rayResult.position.y + distanceAboveTerrain, newy); mActiveCamera->setPosition(camPos.x, newy, camPos.z); } } if (!mLoader->getTerrainGroup()->isDerivedDataUpdateInProgress()) { if (mTerrainImported) { mLoader->getTerrainGroup()->saveAllTerrains(true); mTerrainImported = false; } } mImpulse = btVector3(0,0,0); if(mKeyboard->isKeyDown(OIS::KC_I)) mImpulse += btVector3(0,0,1); if(mKeyboard->isKeyDown(OIS::KC_K)) mImpulse += btVector3(0,0,-1); if(mKeyboard->isKeyDown(OIS::KC_J)) mImpulse += btVector3(-1,0,0); if(mKeyboard->isKeyDown(OIS::KC_L)) mImpulse += btVector3(1,0,0); mCharacterBody->applyCentralImpulse(mImpulse); //Update Bullet world. Globals::phyWorld->stepSimulation(evt.timeSinceLastFrame, 10); Globals::dbgdraw->setDebugMode(mKeyboard->isKeyDown(OIS::KC_F3)); Globals::dbgdraw->step(); //for(unsigned int ij = 0;ij < mLoader->mPGHandles.size();ij++) //{ // mLoader->mPGHandles[ij]->update(); //} return ret; }
std::pair<bool, Real> rayCollide(const Ogre::Ray& ray, Ogre::MovableObject* movable, bool accurate, CullingMode cullingMode, bool allowAnimable) { // Get local space axis aligned bounding box const Ogre::AxisAlignedBox& aabb = movable->getBoundingBox(); // Matrix4 to transform local space to world space const Ogre::Matrix4& localToWorld = movable->_getParentNodeFullTransform(); // Matrix4 to transform world space to local space Ogre::Matrix4 worldToLocal = localToWorld.inverse(); // Matrix3 to transform world space normal to local space Ogre::Matrix3 worldToLocalN; worldToLocal.extract3x3Matrix(worldToLocalN); // Convert world space ray to local space ray // Note: // By preserving the scale between world space and local space of the // direction, we don't need to recalculate the distance later. Ogre::Ray localRay; localRay.setOrigin(worldToLocal * ray.getOrigin()); localRay.setDirection(worldToLocalN * ray.getDirection()); // Intersect with axis aligned bounding box, but because we transformed // ray to local space of the bounding box, so this test just like test // with oriented bounding box. std::pair<bool, Real> ret = localRay.intersects(aabb); // Do accurate test if hitted bounding box and user required. if (ret.first && accurate) { if (movable->getMovableType() == Ogre::EntityFactory::FACTORY_TYPE_NAME || allowAnimable && movable->getMovableType() == Ogre::AutoAnimationEntityFactory::FACTORY_TYPE_NAME) { Ogre::Entity* entity = static_cast<Ogre::Entity*>(movable); if (!entity->_isAnimated()) { // Static entity // Get the entity mesh const Ogre::MeshPtr& mesh = entity->getMesh(); // Get the collision mode CollisionModelPtr collisionModel = CollisionModelManager::getSingleton().getCollisionModel(mesh); ret = doPicking(localRay, *collisionModel, cullingMode); } else if (allowAnimable) { // Animation entity bool addedSoftwareAnimation = false; if (entity->getSoftwareAnimationRequests() <= 0) { entity->addSoftwareAnimationRequest(false); entity->_updateAnimation(); addedSoftwareAnimation = true; } CollisionModel collisionModel; collisionModel.addEntity(entity); collisionModel.build(true); ret = doPicking(localRay, collisionModel, cullingMode); if (addedSoftwareAnimation) { entity->removeSoftwareAnimationRequest(false); } } } } return ret; }