[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
[1454] | 4 | * |
---|
[1505] | 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
[1454] | 22 | * Author: |
---|
| 23 | * Felix Schulthess |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
[1393] | 28 | |
---|
[1410] | 29 | #include "OrxonoxStableHeaders.h" |
---|
| 30 | #include "Navigation.h" |
---|
| 31 | |
---|
[1393] | 32 | #include <OgreOverlayManager.h> |
---|
[1395] | 33 | #include <OgreStringConverter.h> |
---|
[1410] | 34 | |
---|
| 35 | #include "GraphicsEngine.h" |
---|
| 36 | // TODO: remove the SpaceShip and CameraHandler dependencies |
---|
[1393] | 37 | #include "objects/SpaceShip.h" |
---|
[1566] | 38 | #include "objects/Projectile.h" |
---|
[1399] | 39 | #include "objects/CameraHandler.h" |
---|
[1410] | 40 | #include "RadarObject.h" |
---|
| 41 | #include "RadarOverlayElement.h" |
---|
[1393] | 42 | #include "HUD.h" |
---|
[1456] | 43 | #include "core/Debug.h" |
---|
[1564] | 44 | #include "util/Math.h" |
---|
[1393] | 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
| 48 | using namespace Ogre; |
---|
| 49 | |
---|
| 50 | Navigation::Navigation(OverlayContainer* container){ |
---|
| 51 | container_ = container; |
---|
| 52 | focus_ = NULL; |
---|
| 53 | init(); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | Navigation::Navigation(OverlayContainer* container, RadarObject* focus){ |
---|
| 57 | container_ = container; |
---|
| 58 | focus_ = focus; |
---|
| 59 | init(); |
---|
| 60 | } |
---|
| 61 | |
---|
[1564] | 62 | Navigation::~Navigation() |
---|
| 63 | { |
---|
| 64 | OverlayManager::getSingleton().destroyOverlayElement(this->navText_); |
---|
| 65 | OverlayManager::getSingleton().destroyOverlayElement(this->navMarker_); |
---|
| 66 | OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_); |
---|
| 67 | } |
---|
| 68 | |
---|
[1393] | 69 | void Navigation::init(){ |
---|
[1394] | 70 | // create nav text |
---|
[1564] | 71 | navText_ = static_cast<TextAreaOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("TextArea", "navText")); |
---|
[1394] | 72 | navText_->show(); |
---|
[1399] | 73 | navText_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
| 74 | navText_->setDimensions(0.001, 0.001); |
---|
[1394] | 75 | navText_->setPosition(0.02, 0.02); |
---|
| 76 | navText_->setFontName("Console"); |
---|
[1399] | 77 | navText_->setCharHeight(20); |
---|
[1396] | 78 | navText_->setCaption(""); |
---|
[1564] | 79 | navText_->hide(); |
---|
[1394] | 80 | container_->addChild(navText_); |
---|
| 81 | |
---|
| 82 | |
---|
[1393] | 83 | // create nav marker ... |
---|
[1564] | 84 | navMarker_ = static_cast<PanelOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("Panel", "NavMarker")); |
---|
| 85 | aimMarker_ = static_cast<PanelOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("Panel", "aimMarker")); |
---|
[1393] | 86 | navMarker_->setMetricsMode(GMM_PIXELS); |
---|
[1564] | 87 | aimMarker_->setMetricsMode(GMM_PIXELS); |
---|
[1393] | 88 | navMarker_->hide(); |
---|
[1564] | 89 | aimMarker_->hide(); |
---|
[1566] | 90 | aimMarker_->setMaterialName("Orxonox/NavCrosshair"); |
---|
| 91 | aimMarker_->setDimensions(20, 20); |
---|
| 92 | aimMarker_->setUV(0.0, 0.0, 1.0, 1.0); |
---|
[1393] | 93 | container_->addChild(navMarker_); |
---|
[1564] | 94 | container_->addChild(aimMarker_); |
---|
[1410] | 95 | } |
---|
[1393] | 96 | |
---|
[1410] | 97 | void Navigation::update(){ |
---|
[1564] | 98 | if (!focus_) |
---|
| 99 | return; |
---|
[1400] | 100 | |
---|
[1399] | 101 | updateMarker(); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void Navigation::updateMarker(){ |
---|
[1564] | 105 | int windowW = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
| 106 | int windowH = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
| 107 | |
---|
[1399] | 108 | // set text |
---|
[1457] | 109 | int dist = (int) getDist2Focus()/100; |
---|
[1399] | 110 | navText_->setCaption(Ogre::StringConverter::toString(dist)); |
---|
| 111 | |
---|
[1564] | 112 | Ogre::Camera* navCam = SpaceShip::getLocalShip()->getCamera()->cam_; |
---|
[1399] | 113 | // transform to screen coordinates |
---|
[1566] | 114 | Vector3 pos = navCam->getProjectionMatrix() * navCam->getViewMatrix() * focus_->getPosition(); |
---|
| 115 | Vector3 aimpos = navCam->getProjectionMatrix() * navCam->getViewMatrix() * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(), Projectile::getSpeed(), focus_->getPosition(), focus_->getOrientedVelocity()); |
---|
[1564] | 116 | |
---|
[1399] | 117 | float xPosRel = 0.5*pos.x+0.5; |
---|
| 118 | float yPosRel = 1-(0.5*pos.y+0.5); |
---|
[1566] | 119 | float xAimPosRel = 0.5*aimpos.x+0.5; |
---|
| 120 | float yAimPosRel = 1-(0.5*aimpos.y+0.5); |
---|
[1564] | 121 | int xPos = (int) (xPosRel*windowW); |
---|
| 122 | int yPos = (int) (yPosRel*windowH); |
---|
[1566] | 123 | int xAimPos = (int) (xAimPosRel*windowW); |
---|
| 124 | int yAimPos = (int) (yAimPosRel*windowH); |
---|
[1564] | 125 | int xFromCenter = xPos-windowW/2; |
---|
| 126 | int yFromCenter = yPos-windowH/2; |
---|
| 127 | |
---|
[1399] | 128 | // is object in view? |
---|
[1564] | 129 | Vector3 navCamPos = SpaceShip::getLocalShip()->getPosition(); |
---|
| 130 | Vector3 currentDir = SpaceShip::getLocalShip()->getDir(); |
---|
| 131 | Vector3 currentOrth = SpaceShip::getLocalShip()->getOrth(); |
---|
| 132 | float radius = getAngle(navCamPos, currentDir, focus_->getPosition()); |
---|
| 133 | bool isRight = (currentDir.crossProduct(currentOrth)).dotProduct(focus_->getPosition() - navCamPos)>0; |
---|
| 134 | bool isAbove = currentOrth.dotProduct(focus_->getPosition() - navCamPos)>0; |
---|
[1399] | 135 | bool outOfView = (xPosRel<0 || xPosRel>1 || yPosRel<0 || yPosRel>1); |
---|
| 136 | // if object is behind us, it is out of view anyway: |
---|
[1564] | 137 | if(!outOfView && radius > Ogre::Math::PI / 2) outOfView = true; |
---|
[1399] | 138 | |
---|
| 139 | if(outOfView){ |
---|
[1411] | 140 | // object is not in view |
---|
[1399] | 141 | navMarker_->setMaterialName("Orxonox/NavArrows"); |
---|
| 142 | navMarker_->setDimensions(16,16); |
---|
[1566] | 143 | aimMarker_->hide(); |
---|
[1564] | 144 | float phiUpperCorner = atan((float)(windowW)/(float)(windowH)); |
---|
[1411] | 145 | // from the angle we find out on which edge to draw the marker |
---|
[1399] | 146 | // and which of the four arrows to take |
---|
[1411] | 147 | float phiNav = atan((float) xFromCenter / (float) yFromCenter); |
---|
| 148 | |
---|
| 149 | if(isAbove && isRight){ |
---|
| 150 | // top right quadrant |
---|
| 151 | if(-phiNav<phiUpperCorner){ |
---|
[1444] | 152 | //COUT(3) << "arrow up\n"; |
---|
[1564] | 153 | navMarker_->setPosition(-tan(phiNav)*windowH/2+windowW/2, 0); |
---|
[1399] | 154 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
---|
| 155 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 156 | navText_->setTop(navMarker_->getHeight()); |
---|
| 157 | } |
---|
[1411] | 158 | else { |
---|
[1444] | 159 | //COUT(3) << "arrow right\n"; |
---|
[1564] | 160 | navMarker_->setPosition(windowW-16, tan((3.14-2*phiNav)/2)*windowW/2+windowH/2); |
---|
[1411] | 161 | navMarker_->setUV(0.5, 0.5, 1.0, 1.0); |
---|
| 162 | navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth()); |
---|
| 163 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | if(!isAbove && isRight){ |
---|
| 167 | // bottom right quadrant |
---|
| 168 | if(phiNav<phiUpperCorner) { |
---|
[1444] | 169 | //COUT(3) << "arrow down\n"; |
---|
[1564] | 170 | navMarker_->setPosition(tan(phiNav)*windowH/2+windowW/2, windowH-16); |
---|
[1399] | 171 | navMarker_->setUV(0.0, 0.5, 0.5, 1.0); |
---|
| 172 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 173 | navText_->setTop(navMarker_->getTop()-navMarker_->getHeight()); |
---|
| 174 | } |
---|
| 175 | else { |
---|
[1444] | 176 | //COUT(3) << "arrow right\n"; |
---|
[1564] | 177 | navMarker_->setPosition(windowW-16, tan((3.14-2*phiNav)/2)*windowW/2+windowH/2); |
---|
[1399] | 178 | navMarker_->setUV(0.5, 0.5, 1.0, 1.0); |
---|
| 179 | navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth()); |
---|
| 180 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 181 | } |
---|
[1393] | 182 | } |
---|
[1411] | 183 | if(isAbove && !isRight){ |
---|
| 184 | // top left quadrant |
---|
| 185 | if(phiNav<phiUpperCorner){ |
---|
[1444] | 186 | //COUT(3) << "arrow up\n"; |
---|
[1564] | 187 | navMarker_->setPosition(-tan(phiNav)*windowH/2+windowW/2, 0); |
---|
[1399] | 188 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
---|
| 189 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 190 | navText_->setTop(navMarker_->getHeight()); |
---|
| 191 | } |
---|
[1411] | 192 | else { |
---|
[1444] | 193 | //COUT(3) << "arrow left\n"; |
---|
[1564] | 194 | navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW/2+windowH/2); |
---|
[1411] | 195 | navMarker_->setUV(0.0, 0.0, 0.5, 0.5); |
---|
| 196 | navText_->setLeft(navMarker_->getWidth()); |
---|
| 197 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | if(!isAbove && !isRight){ |
---|
| 201 | // bottom left quadrant |
---|
| 202 | if(phiNav>-phiUpperCorner) { |
---|
[1444] | 203 | //COUT(3) << "arrow down\n"; |
---|
[1564] | 204 | navMarker_->setPosition(tan(phiNav)*windowH/2+windowW/2, windowH-16); |
---|
[1399] | 205 | navMarker_->setUV(0.0, 0.5, 0.5, 1.0); |
---|
| 206 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 207 | navText_->setTop(navMarker_->getTop()-navMarker_->getHeight()); |
---|
| 208 | } |
---|
| 209 | else { |
---|
[1444] | 210 | //COUT(3) << "arrow left\n"; |
---|
[1564] | 211 | navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW/2+windowH/2); |
---|
[1399] | 212 | navMarker_->setUV(0.0, 0.0, 0.5, 0.5); |
---|
| 213 | navText_->setLeft(navMarker_->getWidth()); |
---|
| 214 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 215 | } |
---|
[1393] | 216 | } |
---|
| 217 | } |
---|
| 218 | else{ |
---|
[1411] | 219 | // object is in view |
---|
[1399] | 220 | navMarker_->setMaterialName("Orxonox/NavTDC"); |
---|
[1566] | 221 | navMarker_->setDimensions(35, 35); |
---|
[1459] | 222 | navMarker_->setUV(0.0, 0.0, 1.0, 1.0); |
---|
[1399] | 223 | navMarker_->setPosition(xPos-navMarker_->getWidth()/2, yPos-navMarker_->getHeight()/2); |
---|
[1566] | 224 | |
---|
| 225 | aimMarker_->show(); |
---|
| 226 | aimMarker_->setPosition(xAimPos-aimMarker_->getWidth()/2, yAimPos-aimMarker_->getHeight()/2); |
---|
| 227 | |
---|
[1399] | 228 | navText_->setPosition(xPos+navMarker_->getWidth()/2, yPos+navMarker_->getHeight()/2); |
---|
[1393] | 229 | } |
---|
[1410] | 230 | } |
---|
[1394] | 231 | |
---|
[1393] | 232 | void Navigation::cycleFocus(){ |
---|
[1410] | 233 | if(focus_ == NULL){ |
---|
[1564] | 234 | // Get closest object |
---|
| 235 | float distance = (unsigned int) -1; |
---|
| 236 | Vector3 shipPos = SpaceShip::getLocalShip()->getPosition(); |
---|
| 237 | it_ = HUD::getSingleton().getRadarObjects().begin(); |
---|
| 238 | |
---|
| 239 | for (std::list<RadarObject*>::iterator it = HUD::getSingleton().getRadarObjects().begin(); it != HUD::getSingleton().getRadarObjects().end(); ++it) |
---|
| 240 | { |
---|
| 241 | float newdist = (*it)->getPosition().squaredDistance(shipPos); |
---|
| 242 | if (newdist < distance) |
---|
| 243 | { |
---|
| 244 | distance = newdist; |
---|
| 245 | it_ = it; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | if (it_ != HUD::getSingleton().getRadarObjects().end()) |
---|
| 250 | { |
---|
| 251 | focus_ = *it_; |
---|
| 252 | |
---|
| 253 | // move the focused object to the begin of the list, so we will iterate through all other objects when cycling |
---|
| 254 | HUD::getSingleton().getRadarObjects().erase(it_); |
---|
| 255 | HUD::getSingleton().getRadarObjects().insert(HUD::getSingleton().getRadarObjects().begin(), focus_); |
---|
| 256 | it_ = HUD::getSingleton().getRadarObjects().begin(); |
---|
| 257 | ++it_; |
---|
| 258 | } |
---|
[1410] | 259 | } |
---|
[1393] | 260 | else{ |
---|
[1562] | 261 | focus_->resetMaterial(); |
---|
[1564] | 262 | if(it_ != HUD::getSingleton().getRadarObjects().end()){ |
---|
[1456] | 263 | focus_ = *it_; |
---|
| 264 | ++it_; |
---|
| 265 | } |
---|
| 266 | else focus_ = NULL; |
---|
[1393] | 267 | } |
---|
[1564] | 268 | updateFocus(); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | void Navigation::updateFocus(){ |
---|
[1393] | 272 | if(focus_ == NULL){ |
---|
| 273 | navMarker_->hide(); |
---|
[1566] | 274 | aimMarker_->hide(); |
---|
[1399] | 275 | navText_->hide(); |
---|
[1393] | 276 | } |
---|
| 277 | else{ |
---|
| 278 | navMarker_->show(); |
---|
[1399] | 279 | navText_->show(); |
---|
[1562] | 280 | focus_->setColour(ColourValue::White); |
---|
[1393] | 281 | } |
---|
[1410] | 282 | } |
---|
[1393] | 283 | |
---|
[1568] | 284 | void Navigation::releaseFocus() |
---|
| 285 | { |
---|
| 286 | this->focus_ = 0; |
---|
| 287 | this->it_ = HUD::getSingleton().getRadarObjects().end(); |
---|
| 288 | this->updateFocus(); |
---|
| 289 | } |
---|
| 290 | |
---|
[1564] | 291 | float Navigation::getDist2Focus() const { |
---|
[1410] | 292 | if(focus_ == NULL) return(0.0); |
---|
[1450] | 293 | return((focus_->getPosition()-SpaceShip::getLocalShip()->getPosition()).length()); |
---|
[1410] | 294 | } |
---|
[1393] | 295 | } |
---|