[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" |
---|
[1399] | 38 | #include "objects/CameraHandler.h" |
---|
[1410] | 39 | #include "RadarObject.h" |
---|
| 40 | #include "RadarOverlayElement.h" |
---|
[1393] | 41 | #include "HUD.h" |
---|
[1456] | 42 | #include "core/Debug.h" |
---|
[1393] | 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
| 46 | using namespace Ogre; |
---|
| 47 | |
---|
| 48 | Navigation::Navigation(OverlayContainer* container){ |
---|
| 49 | container_ = container; |
---|
| 50 | focus_ = NULL; |
---|
| 51 | init(); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | Navigation::Navigation(OverlayContainer* container, RadarObject* focus){ |
---|
| 55 | container_ = container; |
---|
| 56 | focus_ = focus; |
---|
| 57 | init(); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | void Navigation::init(){ |
---|
[1410] | 61 | om = &OverlayManager::getSingleton(); |
---|
| 62 | navCam_ = NULL; |
---|
[1394] | 63 | // create nav text |
---|
| 64 | navText_ = static_cast<TextAreaOverlayElement*>(om->createOverlayElement("TextArea", "navText")); |
---|
| 65 | navText_->show(); |
---|
[1399] | 66 | navText_->setMetricsMode(Ogre::GMM_PIXELS); |
---|
| 67 | navText_->setDimensions(0.001, 0.001); |
---|
[1394] | 68 | navText_->setPosition(0.02, 0.02); |
---|
| 69 | navText_->setFontName("Console"); |
---|
[1399] | 70 | navText_->setCharHeight(20); |
---|
[1396] | 71 | navText_->setCaption(""); |
---|
[1394] | 72 | container_->addChild(navText_); |
---|
| 73 | |
---|
| 74 | |
---|
[1393] | 75 | // create nav marker ... |
---|
| 76 | navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker")); |
---|
| 77 | navMarker_->setMetricsMode(GMM_PIXELS); |
---|
| 78 | navMarker_->hide(); |
---|
[1399] | 79 | navText_->hide(); |
---|
[1393] | 80 | container_->addChild(navMarker_); |
---|
[1410] | 81 | } |
---|
[1393] | 82 | |
---|
[1410] | 83 | void Navigation::update(){ |
---|
[1393] | 84 | if(focus_ == NULL) return; |
---|
[1400] | 85 | navCamPos_ = SpaceShip::getLocalShip()->getPosition(); |
---|
| 86 | currentDir_ = SpaceShip::getLocalShip()->getDir(); |
---|
[1410] | 87 | currentOrth_ = SpaceShip::getLocalShip()->getOrth(); |
---|
[1400] | 88 | |
---|
[1393] | 89 | windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); |
---|
| 90 | windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); |
---|
[1399] | 91 | updateMarker(); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void Navigation::updateMarker(){ |
---|
| 95 | // set text |
---|
[1457] | 96 | int dist = (int) getDist2Focus()/100; |
---|
[1399] | 97 | navText_->setCaption(Ogre::StringConverter::toString(dist)); |
---|
| 98 | |
---|
[1400] | 99 | if(navCam_ == NULL) navCam_ = SpaceShip::getLocalShip()->getCamera()->cam_; |
---|
[1450] | 100 | Vector3 pos = focus_->getPosition(); |
---|
[1399] | 101 | // transform to screen coordinates |
---|
[1400] | 102 | pos = navCam_->getProjectionMatrix()*navCam_->getViewMatrix()*pos; |
---|
[1399] | 103 | float xPosRel = 0.5*pos.x+0.5; |
---|
| 104 | float yPosRel = 1-(0.5*pos.y+0.5); |
---|
[1458] | 105 | int xPos = (int) (xPosRel*windowW_); |
---|
| 106 | int yPos = (int) (yPosRel*windowH_); |
---|
[1411] | 107 | int xFromCenter = xPos-windowW_/2; |
---|
| 108 | int yFromCenter = yPos-windowH_/2; |
---|
[1399] | 109 | // is object in view? |
---|
[1411] | 110 | float radius = RadarOverlayElement::calcRadius(navCamPos_, currentDir_, currentOrth_, focus_); |
---|
[1450] | 111 | bool isRight = (currentDir_.crossProduct(currentOrth_)).dotProduct(focus_->getPosition() - navCamPos_)>0; |
---|
| 112 | bool isAbove = currentOrth_.dotProduct(focus_->getPosition() - navCamPos_)>0; |
---|
[1399] | 113 | bool outOfView = (xPosRel<0 || xPosRel>1 || yPosRel<0 || yPosRel>1); |
---|
| 114 | // if object is behind us, it is out of view anyway: |
---|
[1411] | 115 | if(!outOfView && radius>3.14/2) outOfView = true; |
---|
[1399] | 116 | |
---|
| 117 | if(outOfView){ |
---|
[1411] | 118 | // object is not in view |
---|
[1399] | 119 | navMarker_->setMaterialName("Orxonox/NavArrows"); |
---|
| 120 | navMarker_->setDimensions(16,16); |
---|
[1411] | 121 | float phiUpperCorner = atan((float)(windowW_)/(float)(windowH_)); |
---|
| 122 | // from the angle we find out on which edge to draw the marker |
---|
[1399] | 123 | // and which of the four arrows to take |
---|
[1411] | 124 | float phiNav = atan((float) xFromCenter / (float) yFromCenter); |
---|
| 125 | |
---|
| 126 | if(isAbove && isRight){ |
---|
| 127 | // top right quadrant |
---|
| 128 | if(-phiNav<phiUpperCorner){ |
---|
[1444] | 129 | //COUT(3) << "arrow up\n"; |
---|
[1411] | 130 | navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0); |
---|
[1399] | 131 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
---|
| 132 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 133 | navText_->setTop(navMarker_->getHeight()); |
---|
| 134 | } |
---|
[1411] | 135 | else { |
---|
[1444] | 136 | //COUT(3) << "arrow right\n"; |
---|
[1411] | 137 | navMarker_->setPosition(windowW_-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); |
---|
| 138 | navMarker_->setUV(0.5, 0.5, 1.0, 1.0); |
---|
| 139 | navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth()); |
---|
| 140 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | if(!isAbove && isRight){ |
---|
| 144 | // bottom right quadrant |
---|
| 145 | if(phiNav<phiUpperCorner) { |
---|
[1444] | 146 | //COUT(3) << "arrow down\n"; |
---|
[1411] | 147 | navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16); |
---|
[1399] | 148 | navMarker_->setUV(0.0, 0.5, 0.5, 1.0); |
---|
| 149 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 150 | navText_->setTop(navMarker_->getTop()-navMarker_->getHeight()); |
---|
| 151 | } |
---|
| 152 | else { |
---|
[1444] | 153 | //COUT(3) << "arrow right\n"; |
---|
[1411] | 154 | navMarker_->setPosition(windowW_-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); |
---|
[1399] | 155 | navMarker_->setUV(0.5, 0.5, 1.0, 1.0); |
---|
| 156 | navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth()); |
---|
| 157 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 158 | } |
---|
[1393] | 159 | } |
---|
[1411] | 160 | if(isAbove && !isRight){ |
---|
| 161 | // top left quadrant |
---|
| 162 | if(phiNav<phiUpperCorner){ |
---|
[1444] | 163 | //COUT(3) << "arrow up\n"; |
---|
[1400] | 164 | navMarker_->setPosition(-tan(phiNav)*windowH_/2+windowW_/2, 0); |
---|
[1399] | 165 | navMarker_->setUV(0.5, 0.0, 1.0, 0.5); |
---|
| 166 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 167 | navText_->setTop(navMarker_->getHeight()); |
---|
| 168 | } |
---|
[1411] | 169 | else { |
---|
[1444] | 170 | //COUT(3) << "arrow left\n"; |
---|
[1411] | 171 | navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); |
---|
| 172 | navMarker_->setUV(0.0, 0.0, 0.5, 0.5); |
---|
| 173 | navText_->setLeft(navMarker_->getWidth()); |
---|
| 174 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | if(!isAbove && !isRight){ |
---|
| 178 | // bottom left quadrant |
---|
| 179 | if(phiNav>-phiUpperCorner) { |
---|
[1444] | 180 | //COUT(3) << "arrow down\n"; |
---|
[1400] | 181 | navMarker_->setPosition(tan(phiNav)*windowH_/2+windowW_/2, windowH_-16); |
---|
[1399] | 182 | navMarker_->setUV(0.0, 0.5, 0.5, 1.0); |
---|
| 183 | navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); |
---|
| 184 | navText_->setTop(navMarker_->getTop()-navMarker_->getHeight()); |
---|
| 185 | } |
---|
| 186 | else { |
---|
[1444] | 187 | //COUT(3) << "arrow left\n"; |
---|
[1400] | 188 | navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2); |
---|
[1399] | 189 | navMarker_->setUV(0.0, 0.0, 0.5, 0.5); |
---|
| 190 | navText_->setLeft(navMarker_->getWidth()); |
---|
| 191 | navText_->setTop(navMarker_->getTop()+navMarker_->getHeight()); |
---|
| 192 | } |
---|
[1393] | 193 | } |
---|
| 194 | } |
---|
| 195 | else{ |
---|
[1411] | 196 | // object is in view |
---|
[1399] | 197 | navMarker_->setMaterialName("Orxonox/NavTDC"); |
---|
| 198 | navMarker_->setDimensions(24,24); |
---|
[1459] | 199 | navMarker_->setUV(0.0, 0.0, 1.0, 1.0); |
---|
[1399] | 200 | navMarker_->setPosition(xPos-navMarker_->getWidth()/2, yPos-navMarker_->getHeight()/2); |
---|
| 201 | navText_->setPosition(xPos+navMarker_->getWidth()/2, yPos+navMarker_->getHeight()/2); |
---|
[1393] | 202 | } |
---|
[1410] | 203 | } |
---|
[1394] | 204 | |
---|
[1393] | 205 | void Navigation::cycleFocus(){ |
---|
[1410] | 206 | if(focus_ == NULL){ |
---|
[1456] | 207 | it_ = HUD::getSingleton().roSet.begin(); |
---|
| 208 | focus_ = *it_; |
---|
[1459] | 209 | ++it_; |
---|
[1410] | 210 | } |
---|
[1393] | 211 | else{ |
---|
[1450] | 212 | focus_->resetColour(); |
---|
[1456] | 213 | if(it_ != HUD::getSingleton().roSet.end()){ |
---|
| 214 | focus_ = *it_; |
---|
| 215 | ++it_; |
---|
| 216 | } |
---|
| 217 | else focus_ = NULL; |
---|
[1393] | 218 | } |
---|
| 219 | if(focus_ == NULL){ |
---|
| 220 | navMarker_->hide(); |
---|
[1399] | 221 | navText_->hide(); |
---|
[1393] | 222 | } |
---|
| 223 | else{ |
---|
| 224 | navMarker_->show(); |
---|
[1399] | 225 | navText_->show(); |
---|
[1450] | 226 | focus_->setColour(RadarObject::WHITE); |
---|
[1393] | 227 | } |
---|
[1410] | 228 | } |
---|
[1393] | 229 | |
---|
[1410] | 230 | float Navigation::getDist2Focus(){ |
---|
| 231 | if(focus_ == NULL) return(0.0); |
---|
[1450] | 232 | return((focus_->getPosition()-SpaceShip::getLocalShip()->getPosition()).length()); |
---|
[1410] | 233 | } |
---|
[1393] | 234 | } |
---|