Changeset 1564 for code/trunk/src/orxonox/hud/Navigation.cc
- Timestamp:
- Jun 8, 2008, 5:46:52 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/orxonox/hud/Navigation.cc
r1562 r1564 41 41 #include "HUD.h" 42 42 #include "core/Debug.h" 43 #include "util/Math.h" 43 44 44 45 namespace orxonox … … 58 59 } 59 60 61 Navigation::~Navigation() 62 { 63 OverlayManager::getSingleton().destroyOverlayElement(this->navText_); 64 OverlayManager::getSingleton().destroyOverlayElement(this->navMarker_); 65 OverlayManager::getSingleton().destroyOverlayElement(this->aimMarker_); 66 } 67 60 68 void Navigation::init(){ 61 om = &OverlayManager::getSingleton();62 navCam_ = NULL;63 69 // create nav text 64 navText_ = static_cast<TextAreaOverlayElement*>( om->createOverlayElement("TextArea", "navText"));70 navText_ = static_cast<TextAreaOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("TextArea", "navText")); 65 71 navText_->show(); 66 72 navText_->setMetricsMode(Ogre::GMM_PIXELS); … … 70 76 navText_->setCharHeight(20); 71 77 navText_->setCaption(""); 78 navText_->hide(); 72 79 container_->addChild(navText_); 73 80 74 81 75 82 // create nav marker ... 76 navMarker_ = static_cast<PanelOverlayElement*>(om->createOverlayElement("Panel", "NavMarker")); 83 navMarker_ = static_cast<PanelOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("Panel", "NavMarker")); 84 aimMarker_ = static_cast<PanelOverlayElement*>(OverlayManager::getSingleton().createOverlayElement("Panel", "aimMarker")); 77 85 navMarker_->setMetricsMode(GMM_PIXELS); 86 aimMarker_->setMetricsMode(GMM_PIXELS); 78 87 navMarker_->hide(); 79 navText_->hide();88 aimMarker_->hide(); 80 89 container_->addChild(navMarker_); 90 container_->addChild(aimMarker_); 81 91 } 82 92 83 93 void Navigation::update(){ 84 if(focus_ == NULL) return; 85 navCamPos_ = SpaceShip::getLocalShip()->getPosition(); 86 currentDir_ = SpaceShip::getLocalShip()->getDir(); 87 currentOrth_ = SpaceShip::getLocalShip()->getOrth(); 88 89 windowW_ = GraphicsEngine::getSingleton().getWindowWidth(); 90 windowH_ = GraphicsEngine::getSingleton().getWindowHeight(); 94 if (!focus_) 95 return; 96 91 97 updateMarker(); 92 98 } 93 99 94 100 void Navigation::updateMarker(){ 101 int windowW = GraphicsEngine::getSingleton().getWindowWidth(); 102 int windowH = GraphicsEngine::getSingleton().getWindowHeight(); 103 95 104 // set text 96 105 int dist = (int) getDist2Focus()/100; 97 106 navText_->setCaption(Ogre::StringConverter::toString(dist)); 98 107 99 if(navCam_ == NULL) navCam_ = SpaceShip::getLocalShip()->getCamera()->cam_;100 108 Vector3 pos = focus_->getPosition(); 109 Ogre::Camera* navCam = SpaceShip::getLocalShip()->getCamera()->cam_; 101 110 // transform to screen coordinates 102 pos = navCam_->getProjectionMatrix()*navCam_->getViewMatrix()*pos; 111 pos = navCam->getProjectionMatrix() * navCam->getViewMatrix() * pos; 112 103 113 float xPosRel = 0.5*pos.x+0.5; 104 114 float yPosRel = 1-(0.5*pos.y+0.5); 105 int xPos = (int) (xPosRel*windowW_); 106 int yPos = (int) (yPosRel*windowH_); 107 int xFromCenter = xPos-windowW_/2; 108 int yFromCenter = yPos-windowH_/2; 115 int xPos = (int) (xPosRel*windowW); 116 int yPos = (int) (yPosRel*windowH); 117 int xFromCenter = xPos-windowW/2; 118 int yFromCenter = yPos-windowH/2; 119 109 120 // is object in view? 110 float radius = RadarOverlayElement::calcRadius(navCamPos_, currentDir_, currentOrth_, focus_); 111 bool isRight = (currentDir_.crossProduct(currentOrth_)).dotProduct(focus_->getPosition() - navCamPos_)>0; 112 bool isAbove = currentOrth_.dotProduct(focus_->getPosition() - navCamPos_)>0; 121 Vector3 navCamPos = SpaceShip::getLocalShip()->getPosition(); 122 Vector3 currentDir = SpaceShip::getLocalShip()->getDir(); 123 Vector3 currentOrth = SpaceShip::getLocalShip()->getOrth(); 124 float radius = getAngle(navCamPos, currentDir, focus_->getPosition()); 125 bool isRight = (currentDir.crossProduct(currentOrth)).dotProduct(focus_->getPosition() - navCamPos)>0; 126 bool isAbove = currentOrth.dotProduct(focus_->getPosition() - navCamPos)>0; 113 127 bool outOfView = (xPosRel<0 || xPosRel>1 || yPosRel<0 || yPosRel>1); 114 128 // if object is behind us, it is out of view anyway: 115 if(!outOfView && radius >3.14/2) outOfView = true;129 if(!outOfView && radius > Ogre::Math::PI / 2) outOfView = true; 116 130 117 131 if(outOfView){ … … 119 133 navMarker_->setMaterialName("Orxonox/NavArrows"); 120 134 navMarker_->setDimensions(16,16); 121 float phiUpperCorner = atan((float)(windowW _)/(float)(windowH_));135 float phiUpperCorner = atan((float)(windowW)/(float)(windowH)); 122 136 // from the angle we find out on which edge to draw the marker 123 137 // and which of the four arrows to take … … 128 142 if(-phiNav<phiUpperCorner){ 129 143 //COUT(3) << "arrow up\n"; 130 navMarker_->setPosition(-tan(phiNav)*windowH _/2+windowW_/2, 0);144 navMarker_->setPosition(-tan(phiNav)*windowH/2+windowW/2, 0); 131 145 navMarker_->setUV(0.5, 0.0, 1.0, 0.5); 132 146 navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); … … 135 149 else { 136 150 //COUT(3) << "arrow right\n"; 137 navMarker_->setPosition(windowW _-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);151 navMarker_->setPosition(windowW-16, tan((3.14-2*phiNav)/2)*windowW/2+windowH/2); 138 152 navMarker_->setUV(0.5, 0.5, 1.0, 1.0); 139 153 navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth()); … … 145 159 if(phiNav<phiUpperCorner) { 146 160 //COUT(3) << "arrow down\n"; 147 navMarker_->setPosition(tan(phiNav)*windowH _/2+windowW_/2, windowH_-16);161 navMarker_->setPosition(tan(phiNav)*windowH/2+windowW/2, windowH-16); 148 162 navMarker_->setUV(0.0, 0.5, 0.5, 1.0); 149 163 navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); … … 152 166 else { 153 167 //COUT(3) << "arrow right\n"; 154 navMarker_->setPosition(windowW _-16, tan((3.14-2*phiNav)/2)*windowW_/2+windowH_/2);168 navMarker_->setPosition(windowW-16, tan((3.14-2*phiNav)/2)*windowW/2+windowH/2); 155 169 navMarker_->setUV(0.5, 0.5, 1.0, 1.0); 156 170 navText_->setLeft(navMarker_->getLeft()-navMarker_->getWidth()); … … 162 176 if(phiNav<phiUpperCorner){ 163 177 //COUT(3) << "arrow up\n"; 164 navMarker_->setPosition(-tan(phiNav)*windowH _/2+windowW_/2, 0);178 navMarker_->setPosition(-tan(phiNav)*windowH/2+windowW/2, 0); 165 179 navMarker_->setUV(0.5, 0.0, 1.0, 0.5); 166 180 navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); … … 169 183 else { 170 184 //COUT(3) << "arrow left\n"; 171 navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW _/2+windowH_/2);185 navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW/2+windowH/2); 172 186 navMarker_->setUV(0.0, 0.0, 0.5, 0.5); 173 187 navText_->setLeft(navMarker_->getWidth()); … … 179 193 if(phiNav>-phiUpperCorner) { 180 194 //COUT(3) << "arrow down\n"; 181 navMarker_->setPosition(tan(phiNav)*windowH _/2+windowW_/2, windowH_-16);195 navMarker_->setPosition(tan(phiNav)*windowH/2+windowW/2, windowH-16); 182 196 navMarker_->setUV(0.0, 0.5, 0.5, 1.0); 183 197 navText_->setLeft(navMarker_->getLeft()+navMarker_->getWidth()); … … 186 200 else { 187 201 //COUT(3) << "arrow left\n"; 188 navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW _/2+windowH_/2);202 navMarker_->setPosition(0, -tan((3.14-2*phiNav)/2)*windowW/2+windowH/2); 189 203 navMarker_->setUV(0.0, 0.0, 0.5, 0.5); 190 204 navText_->setLeft(navMarker_->getWidth()); … … 205 219 void Navigation::cycleFocus(){ 206 220 if(focus_ == NULL){ 207 it_ = HUD::getSingleton().roSet.begin(); 208 focus_ = *it_; 209 ++it_; 221 // Get closest object 222 float distance = (unsigned int) -1; 223 Vector3 shipPos = SpaceShip::getLocalShip()->getPosition(); 224 it_ = HUD::getSingleton().getRadarObjects().begin(); 225 226 for (std::list<RadarObject*>::iterator it = HUD::getSingleton().getRadarObjects().begin(); it != HUD::getSingleton().getRadarObjects().end(); ++it) 227 { 228 float newdist = (*it)->getPosition().squaredDistance(shipPos); 229 if (newdist < distance) 230 { 231 distance = newdist; 232 it_ = it; 233 } 234 } 235 236 if (it_ != HUD::getSingleton().getRadarObjects().end()) 237 { 238 focus_ = *it_; 239 240 // move the focused object to the begin of the list, so we will iterate through all other objects when cycling 241 HUD::getSingleton().getRadarObjects().erase(it_); 242 HUD::getSingleton().getRadarObjects().insert(HUD::getSingleton().getRadarObjects().begin(), focus_); 243 it_ = HUD::getSingleton().getRadarObjects().begin(); 244 ++it_; 245 } 210 246 } 211 247 else{ 212 248 focus_->resetMaterial(); 213 if(it_ != HUD::getSingleton(). roSet.end()){249 if(it_ != HUD::getSingleton().getRadarObjects().end()){ 214 250 focus_ = *it_; 215 251 ++it_; … … 217 253 else focus_ = NULL; 218 254 } 255 updateFocus(); 256 } 257 258 void Navigation::updateFocus(){ 219 259 if(focus_ == NULL){ 220 260 navMarker_->hide(); … … 228 268 } 229 269 230 float Navigation::getDist2Focus() {270 float Navigation::getDist2Focus() const { 231 271 if(focus_ == NULL) return(0.0); 232 272 return((focus_->getPosition()-SpaceShip::getLocalShip()->getPosition()).length());
Note: See TracChangeset
for help on using the changeset viewer.