Ogre::Vector2 Mesh::getGridPosition(const Ogre::Vector2 &Position) { if (mOptions.MeshSize.Width == 0 && mOptions.MeshSize.Height == 0) { return Position; } if (!isPointInGrid(Position)) { return Ogre::Vector2(-1,-1); } Ogre::AxisAlignedBox WordMeshBox = mEntity->getWorldBoundingBox(); // Get our mesh grid rectangle: (Only a,b,c corners) // c // | // | // | // a-----------b Ogre::Vector3 a = WordMeshBox.getCorner(Ogre::AxisAlignedBox::FAR_LEFT_BOTTOM), b = WordMeshBox.getCorner(Ogre::AxisAlignedBox::FAR_RIGHT_BOTTOM), c = WordMeshBox.getCorner(Ogre::AxisAlignedBox::NEAR_LEFT_BOTTOM); // Transform all corners to Ogre::Vector2 array Ogre::Vector2 Corners2D[3] = {Ogre::Vector2(a.x, a.z), Ogre::Vector2(b.x, b.z), Ogre::Vector2(c.x, c.z)}; // Get segments AB and AC Ogre::Vector2 AB = Corners2D[1]-Corners2D[0], AC = Corners2D[2]-Corners2D[0]; // Find the X/Y position projecting the Position point to AB and AC segments. Ogre::Vector2 XProjectedPoint = Position-AC, YProjectedPoint = Position-AB; // Fint the intersections points Ogre::Vector2 XPoint = Math::intersectionOfTwoLines(Corners2D[0],Corners2D[1],Position,XProjectedPoint), YPoint = Math::intersectionOfTwoLines(Corners2D[0],Corners2D[2],Position,YProjectedPoint); // Find lengths Ogre::Real ABLength = AB.length(), ACLength = AC.length(), XLength = (XPoint-Corners2D[0]).length(), YLength = (YPoint-Corners2D[0]).length(); // Find final x/y grid positions in [0,1] range Ogre::Real XFinal = XLength / ABLength, YFinal = YLength / ACLength; return Ogre::Vector2(XFinal,YFinal); }
bool Mesh::isPointInGrid(const Ogre::Vector2 &Position) { Ogre::AxisAlignedBox WordMeshBox = mEntity->getWorldBoundingBox(); // Get our mesh grid rectangle: // c-----------d // | | // | | // | | // a-----------b Ogre::Vector3 a = WordMeshBox.getCorner(Ogre::AxisAlignedBox::FAR_LEFT_BOTTOM), b = WordMeshBox.getCorner(Ogre::AxisAlignedBox::FAR_RIGHT_BOTTOM), c = WordMeshBox.getCorner(Ogre::AxisAlignedBox::NEAR_RIGHT_BOTTOM), d = WordMeshBox.getCorner(Ogre::AxisAlignedBox::NEAR_LEFT_BOTTOM); // Transform all corners to Ogre::Vector2 array Ogre::Vector2 Corners2D[4] = {Ogre::Vector2(a.x, a.z), Ogre::Vector2(b.x, b.z), Ogre::Vector2(c.x, c.z), Ogre::Vector2(d.x, d.z)}; // Determinate if Position is into our rectangle, we use a line intersection detection // because our mesh rectangle can be rotated, if the number of collisions with the four // segments AB, BC, CD, DA is one, the Position point is into the rectangle, else(if number // of collisions are 0 or 2, the Position point is outside the rectangle. int NumberOfCollisions = 0; // Find a point wich isn't be inside the rectangle Ogre::Vector2 DestPoint = Corners2D[0] + (Corners2D[1]-Corners2D[0])*2; for (int k = 0; k < 3; k++) { if (Math::intersectionOfTwoLines(Corners2D[k], Corners2D[k+1], Position, DestPoint) != Ogre::Vector2::ZERO) { NumberOfCollisions ++; } if (k == 2) { if (Math::intersectionOfTwoLines(Corners2D[3], Corners2D[0], Position, DestPoint) != Ogre::Vector2::ZERO) { NumberOfCollisions ++; } } } if (NumberOfCollisions == 1) { return true; } return false; }
void Tiles::initTile(Ogre::SceneManager *s,std::string name,std::string mesh,std::string mat, Ogre::Vector3 v,int x,int y,int h,int tileD) { float d = tileD/100.0; std::cout<<name<<" ("<<v.x<<","<<v.y<<","<<v.z<<")"<<std::endl; sn = s->getRootSceneNode()->createChildSceneNode( name+"node",v); e = s->createEntity(name,Ogre::SceneManager::PT_CUBE); sn->attachObject(e); sn->scale(Ogre::Vector3(1,d*(h+1),1)); e->setMaterialName(mat); e->setCastShadows(false); Ogre::AxisAlignedBox box = e->getBoundingBox(); box.scale(Ogre::Vector3(1,d*(h+1),1)); float dy = box.getCorner(Ogre::AxisAlignedBox::FAR_LEFT_BOTTOM ).y + tileD; sn->translate(0,-dy,0); u = NULL; this->x=x; this->y=y; this->h=h; }