void CollisionTools::calculateY(Ogre::SceneNode *n, const bool doTerrainCheck, const bool doGridCheck, const float gridWidth, const Ogre::uint32 queryMask) { Ogre::Vector3 pos = n->getPosition(); float x = pos.x; float z = pos.z; float y = pos.y; Ogre::Vector3 myResult(0,0,0); Ogre::MovableObject *myObject=NULL; float distToColl = 0.0f; float terrY = 0, colY = 0, colY2 = 0; if( raycastFromPoint(Ogre::Vector3(x,y,z),Ogre::Vector3::NEGATIVE_UNIT_Y,myResult,myObject, distToColl, queryMask)){ if (myObject != NULL) { colY = myResult.y; } else { colY = -99999; } } //if doGridCheck is on, repeat not to fall through small holes for example when crossing a hangbridge if (doGridCheck) { if( raycastFromPoint(Ogre::Vector3(x,y,z)+(n->getOrientation()*Ogre::Vector3(0,0,gridWidth)),Ogre::Vector3::NEGATIVE_UNIT_Y,myResult, myObject, distToColl, queryMask)){ if (myObject != NULL) { colY = myResult.y; } else { colY = -99999; } } if (colY<colY2) colY = colY2; } // set the parameter to false if you are not using ETM or TSM if (doTerrainCheck) { #ifdef ETM_TERRAIN // ETM height value terrY = mTerrainInfo->getHeightAt(x,z); #else // TSM height value terrY = getTSMHeightAt(x,z); #endif if(terrY < colY ) { n->setPosition(x,colY+_heightAdjust,z); } else { n->setPosition(x,terrY+_heightAdjust,z); } } else { if (!doTerrainCheck && colY == -99999) colY = y; n->setPosition(x,colY+_heightAdjust,z); } }
Ogre::Entity* CollisionTools::collidesWithObject(const Ogre::Vector3& fromPoint, const Ogre::Vector3& toPoint, const float collisionRadius, const Ogre::uint32 queryMask) { Ogre::Vector3 normal = toPoint - fromPoint; float distToDest = normal.normalise(); Ogre::Vector3 myResult(0, 0, 0); Ogre::Entity* myObject = NULL; float distToColl = 0.0f; if (raycastFromPoint(fromPoint, normal, myResult, myObject, distToColl, queryMask)) { distToColl -= collisionRadius; if (distToColl <= distToDest) return myObject; } return NULL; }
bool CollisionTools::collidesWithEntity(const Ogre::Vector3& fromPoint, const Ogre::Vector3& toPoint, const float collisionRadius, const float rayHeightLevel, const Ogre::uint32 queryMask) { Ogre::Vector3 fromPointAdj(fromPoint.x, fromPoint.y + rayHeightLevel, fromPoint.z); Ogre::Vector3 toPointAdj(toPoint.x, toPoint.y + rayHeightLevel, toPoint.z); Ogre::Vector3 normal = toPointAdj - fromPointAdj; float distToDest = normal.normalise(); Ogre::Vector3 myResult(0, 0, 0); Ogre::MovableObject* myObject = NULL; float distToColl = 0.0f; if (raycastFromPoint(fromPointAdj, normal, myResult, myObject, distToColl, queryMask)) { distToColl -= collisionRadius; return (distToColl <= distToDest); } else { return false; } }