[1494] | 1 | /* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
---|
[1454] | 2 | * Author: |
---|
| 3 | * Felix Schulthess |
---|
| 4 | * Co-authors: |
---|
| 5 | * ... |
---|
| 6 | * |
---|
| 7 | */ |
---|
[1393] | 8 | |
---|
[1410] | 9 | #include "OrxonoxStableHeaders.h" |
---|
| 10 | #include "Navigation.h" |
---|
| 11 | |
---|
[1393] | 12 | #include <OgreOverlayManager.h> |
---|
[1395] | 13 | #include <OgreStringConverter.h> |
---|
[1410] | 14 | |
---|
| 15 | #include "GraphicsEngine.h" |
---|
| 16 | // TODO: remove the SpaceShip and CameraHandler dependencies |
---|
[1393] | 17 | #include "objects/SpaceShip.h" |
---|
[1399] | 18 | #include "objects/CameraHandler.h" |
---|
[1410] | 19 | #include "RadarObject.h" |
---|
| 20 | #include "RadarOverlayElement.h" |
---|
[1393] | 21 | #include "HUD.h" |
---|
[1456] | 22 | #include "core/Debug.h" |
---|
[1393] | 23 | |
---|
| 24 | namespace orxonox |
---|
| 25 | { |
---|
| 26 | using namespace Ogre; |
---|
| 27 | |
---|
| 28 | Navigation::Navigation(OverlayContainer* container){ |
---|
| 29 | container_ = container; |
---|
| 30 | focus_ = NULL; |
---|
| 31 | init(); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | Navigation::Navigation(OverlayContainer* container, RadarObject* focus){ |
---|
| 35 | container_ = container; |
---|
| 36 | focus_ = focus; |
---|
| 37 | init(); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | void Navigation::init(){ |
---|
[1410] | 41 | om = &OverlayManager::getSingleton(); |
---|
| 42 | navCam_ = NULL; |
---|
[1394] | 43 | // create nav text |
---|
| 44 | navText_ = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "navText")); |
---|
| 45 | navText_->show(); |
---|
[1399] | 46 | navText_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
| 47 | navText_->setDimensions(0.001, 0.001); |
---|
[1394] | 48 | navText_->setPosition(0.02, 0.02); |
---|
| 49 | navText_->setFontName("Console"); |
---|
[1399] | 50 | navText_->setCharHeight(20); |
---|
[1396] | 51 | navText_->setCaption(""); |
---|
[1394] | 52 | container_->addChild(navText_); |
---|
| 53 | |
---|
| 54 | |
---|
[1393] | 55 | // create nav marker ... |
---|
| 56 | navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker")); |
---|
| 57 | navMarker_->setMetricsMode(GMM_PIXELS); |
---|
| 58 | navMarker_->hide(); |
---|
[1399] | 59 | navText_->hide(); |
---|
[1393] | 60 | container_->addChild(navMarker_); |
---|
[1410] | 61 | } |
---|
[1393] | 62 | |
---|
[1410] | 63 | void Navigation::update(){ |
---|
[1393] | 64 | if(focus_ == NULL) return; |
---|
[1400] | 65 | navCamPos_ = SpaceShip::getLocalShip()->getPosition(); |
---|
| 66 | currentDir_ = SpaceShip::getLocalShip()->getDir(); |
---|
[1410] | 67 | currentOrth_ = SpaceShip::getLocalShip()->getOrth(); |
---|
[1400] | 68 | |
---|
[1393] | 69 | windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
| 70 | windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
[1399] | 71 | updateMarker(); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | void Navigation::updateMarker(){ |
---|
| 75 | // set text |
---|
[1457] | 76 | int dist = (int) getDist2Focus()/100; |
---|
[1399] | 77 | navText_->setCaption(Ogre::StringConverter::toString(dist)); |
---|
| 78 | |
---|
[1400] | 79 | if(navCam_ == NULL) navCam_ = SpaceShip::getLocalShip()->getCamera()->cam_; |
---|
[1450] | 80 | Vector3 pos = focus_->getPosition(); |
---|
[1399] | 81 | // transform to screen coordinates |
---|
[1400] | 82 | pos = navCam_->getProjectionMatrix()*navCam_->getViewMatrix()*pos; |
---|
[1399] | 83 | float xPosRel = 0.5*pos.x+0.5; |
---|
| 84 | float yPosRel = 1-(0.5*pos.y+0.5); |
---|
[1458] | 85 | int xPos = (int) (xPosRel*windowW_); |
---|
| 86 | int yPos = (int) (yPosRel*windowH_); |
---|
[1411] | 87 | int xFromCenter = xPos-windowW_/2; |
---|
| 88 | int yFromCenter = yPos-windowH_/2; |
---|
[1399] | 89 | // is object in view? |
---|
[1411] | 90 | float radius = RadarOverlayElement::calcRadius(navCamPos_, currentDir_, currentOrth_, focus_); |
---|
[1450] | 91 | bool isRight = (currentDir_.crossProduct(currentOrth_)).dotProduct(focus_->getPosition() - navCamPos_)>0; |
---|
| 92 | bool isAbove = currentOrth_.dotProduct(focus_->getPosition() - navCamPos_)>0; |
---|
[1399] | 93 | bool outOfView = (xPosRel<0 || xPosRel>1 || yPosRel<0 || yPosRel>1); |
---|
| 94 | // if object is behind us, it is out of view anyway: |
---|
[1411] | 95 | if(!outOfView && radius>3.14/2) outOfView = true; |
---|
[1399] | 96 | |
---|
| 97 | if(outOfView){ |
---|
[1411] | 98 | // object is not in view |
---|
[1399] | 99 | navMarker_->setMaterialName("Orxonox/NavArrows"); |
---|
| 100 | navMarker_->setDimensions(16,16); |
---|
[1411] | 101 | float phiUpperCorner = atan((float)(windowW_)/(float)(windowH_)); |
---|
| 102 | // from the angle we find out on which edge to draw the marker |
---|
[1399] | 103 | // and which of the four arrows to take |
---|
[1411] | 104 | float phiNav = atan((float) xFromCenter / (float) yFromCenter); |
---|
| 105 | |
---|
| 106 | if(isAbove && isRight){ |
---|
| 107 | // top right quadrant |
---|
| 108 | if(-phiNav<phiUpperCorner){ |
---|
[1444] | 109 | //COUT(3) << "arrow up\n"; |
---|
[1411] | 110 | navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0); |
---|
[1399] | 111 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
---|
| 112 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 113 | navText_->setTop(navMarker_->getHeight()); |
---|
| 114 | } |
---|
[1411] | 115 | else { |
---|
[1444] | 116 | //COUT(3) << "arrow right\n"; |
---|
[1411] | 117 | navMarker_->setPosition(windowW_-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); |
---|
| 118 | navMarker_->setUV(0.5, 0.5, 1.0, 1.0); |
---|
| 119 | navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth()); |
---|
| 120 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | if(!isAbove && isRight){ |
---|
| 124 | // bottom right quadrant |
---|
| 125 | if(phiNav<phiUpperCorner) { |
---|
[1444] | 126 | //COUT(3) << "arrow down\n"; |
---|
[1411] | 127 | navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16); |
---|
[1399] | 128 | navMarker_->setUV(0.0, 0.5, 0.5, 1.0); |
---|
| 129 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 130 | navText_->setTop(navMarker_->getTop()-navMarker_->getHeight()); |
---|
| 131 | } |
---|
| 132 | else { |
---|
[1444] | 133 | //COUT(3) << "arrow right\n"; |
---|
[1411] | 134 | navMarker_->setPosition(windowW_-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); |
---|
[1399] | 135 | navMarker_->setUV(0.5, 0.5, 1.0, 1.0); |
---|
| 136 | navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth()); |
---|
| 137 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 138 | } |
---|
[1393] | 139 | } |
---|
[1411] | 140 | if(isAbove && !isRight){ |
---|
| 141 | // top left quadrant |
---|
| 142 | if(phiNav<phiUpperCorner){ |
---|
[1444] | 143 | //COUT(3) << "arrow up\n"; |
---|
[1400] | 144 | navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0); |
---|
[1399] | 145 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
---|
| 146 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 147 | navText_->setTop(navMarker_->getHeight()); |
---|
| 148 | } |
---|
[1411] | 149 | else { |
---|
[1444] | 150 | //COUT(3) << "arrow left\n"; |
---|
[1411] | 151 | navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); |
---|
| 152 | navMarker_->setUV(0.0, 0.0, 0.5, 0.5); |
---|
| 153 | navText_->setLeft(navMarker_->getWidth()); |
---|
| 154 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | if(!isAbove && !isRight){ |
---|
| 158 | // bottom left quadrant |
---|
| 159 | if(phiNav>-phiUpperCorner) { |
---|
[1444] | 160 | //COUT(3) << "arrow down\n"; |
---|
[1400] | 161 | navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16); |
---|
[1399] | 162 | navMarker_->setUV(0.0, 0.5, 0.5, 1.0); |
---|
| 163 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 164 | navText_->setTop(navMarker_->getTop()-navMarker_->getHeight()); |
---|
| 165 | } |
---|
| 166 | else { |
---|
[1444] | 167 | //COUT(3) << "arrow left\n"; |
---|
[1400] | 168 | navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); |
---|
[1399] | 169 | navMarker_->setUV(0.0, 0.0, 0.5, 0.5); |
---|
| 170 | navText_->setLeft(navMarker_->getWidth()); |
---|
| 171 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 172 | } |
---|
[1393] | 173 | } |
---|
| 174 | } |
---|
| 175 | else{ |
---|
[1411] | 176 | // object is in view |
---|
[1399] | 177 | navMarker_->setMaterialName("Orxonox/NavTDC"); |
---|
| 178 | navMarker_->setDimensions(24,24); |
---|
[1459] | 179 | navMarker_->setUV(0.0, 0.0, 1.0, 1.0); |
---|
[1399] | 180 | navMarker_->setPosition(xPos-navMarker_->getWidth()/2, yPos-navMarker_->getHeight()/2); |
---|
| 181 | navText_->setPosition(xPos+navMarker_->getWidth()/2, yPos+navMarker_->getHeight()/2); |
---|
[1393] | 182 | } |
---|
[1410] | 183 | } |
---|
[1394] | 184 | |
---|
[1393] | 185 | void Navigation::cycleFocus(){ |
---|
[1410] | 186 | if(focus_ == NULL){ |
---|
[1456] | 187 | it_ = HUD::getSingleton().roSet.begin(); |
---|
| 188 | focus_ = *it_; |
---|
[1459] | 189 | ++it_; |
---|
[1410] | 190 | } |
---|
[1393] | 191 | else{ |
---|
[1450] | 192 | focus_->resetColour(); |
---|
[1456] | 193 | if(it_ != HUD::getSingleton().roSet.end()){ |
---|
| 194 | focus_ = *it_; |
---|
| 195 | ++it_; |
---|
| 196 | } |
---|
| 197 | else focus_ = NULL; |
---|
[1393] | 198 | } |
---|
| 199 | if(focus_ == NULL){ |
---|
| 200 | navMarker_->hide(); |
---|
[1399] | 201 | navText_->hide(); |
---|
[1393] | 202 | } |
---|
| 203 | else{ |
---|
| 204 | navMarker_->show(); |
---|
[1399] | 205 | navText_->show(); |
---|
[1450] | 206 | focus_->setColour(RadarObject::WHITE); |
---|
[1393] | 207 | } |
---|
[1410] | 208 | } |
---|
[1393] | 209 | |
---|
[1410] | 210 | float Navigation::getDist2Focus(){ |
---|
| 211 | if(focus_ == NULL) return(0.0); |
---|
[1450] | 212 | return((focus_->getPosition()-SpaceShip::getLocalShip()->getPosition()).length()); |
---|
[1410] | 213 | } |
---|
[1393] | 214 | } |
---|