Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 20, 2008, 12:05:12 AM (16 years ago)
Author:
rgrieder
Message:
  • Radar now working like before
  • but more of a svn save..

There is class called Radar which takes care of the focus and all objects that can be displayed on a Radar.
The actual visual implementation is in HUDRadar.
To make a object radar viewable, simply implement RadarViewable and set the WorldEntitiy pointer in the constructor (assertation will fail otherwise!).
You can also set a camouflage value between 0 and 1 that tells how good a radar can see an object.
The HUDRadar (or another one) on the other side has a sensitivity between 0 and 1. So only if the sensitivity is higher than the camouflage value, the object gets displayed.

Location:
code/branches/hud/src/orxonox/overlays/hud
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/hud/src/orxonox/overlays/hud/HUDNavigation.cc

    r1604 r1613  
    4242#include "objects/CameraHandler.h"
    4343#include "overlays/OverlayGroup.h"
    44 #include "RadarObject.h"
    45 #include "HUDRadar.h"
     44#include "Radar.h"
    4645
    4746namespace orxonox
     
    4948    CreateFactory(HUDNavigation);
    5049
    51     SetConsoleCommand(HUDNavigation, cycleNavigationFocus, true).setAccessLevel(AccessLevel::User);
    52     SetConsoleCommand(HUDNavigation, releaseNavigationFocus, true).setAccessLevel(AccessLevel::User);
    53 
    54     HUDNavigation* HUDNavigation::instance_s = 0;
     50    //HUDNavigation* HUDNavigation::instance_s = 0;
    5551
    5652    using namespace Ogre;
     
    6157      , aimMarker_(0)
    6258      , navText_(0)
    63       , focus_(0)
    6459    {
    6560        RegisterObject(HUDNavigation);
    6661       
    67         assert(instance_s == 0); // singleton class
    68         HUDNavigation::instance_s = this;
     62        /*assert(instance_s == 0); // singleton class
     63        HUDNavigation::instance_s = this;*/
    6964    }
    7065
     
    8378        }
    8479
    85         HUDNavigation::instance_s = 0;
     80        //HUDNavigation::instance_s = 0;
    8681    }
    8782
     
    117112
    118113            overlay_->add2D(container_);
    119             overlay_->hide();
    120114        }
    121115
     
    181175    void HUDNavigation::tick(float dt)
    182176    {
    183         if (!focus_)
     177        if (!Radar::getInstance().getFocus())
     178        {
     179            this->overlay_->hide();
    184180            return;
     181        }
     182        else
     183        {
     184            this->overlay_->show();
     185        }
    185186
    186187        // set text
    187         int dist = (int) getDist2Focus()/100.0f;
     188        int dist = (int) getDist2Focus();
    188189        navText_->setCaption(convertToString(dist));
    189190        float textLength = convertToString(dist).size() * navText_->getCharHeight() * 0.3;
     
    192193        Matrix4 transformationMatrix = navCam->getProjectionMatrix() * navCam->getViewMatrix();
    193194        // transform to screen coordinates
    194         Vector3 pos = transformationMatrix * focus_->getPosition();
     195        Vector3 pos = transformationMatrix * Radar::getInstance().getFocus()->getWorldPosition();
    195196
    196197        bool outOfView;
     
    266267
    267268            Vector3 aimpos = transformationMatrix * getPredictedPosition(SpaceShip::getLocalShip()->getPosition(),
    268                     Projectile::getSpeed(), focus_->getPosition(), focus_->getOrientedVelocity());
     269                    Projectile::getSpeed(), Radar::getInstance().getFocus()->getWorldPosition(), Radar::getInstance().getFocus()->getOrientedVelocity());
    269270
    270271            if (wasOutOfView_)
     
    288289    }
    289290
    290     void HUDNavigation::cycleFocus()
    291     {
    292         if (!focus_)
    293         {
    294             // Get closest object
    295             float distance = (unsigned int) -1;
    296             Vector3 shipPos = SpaceShip::getLocalShip()->getPosition();
    297             it_ = HUDRadar::getInstance().getRadarObjects().begin();
    298 
    299             for (std::list<RadarObject*>::iterator it = HUDRadar::getInstance().getRadarObjects().begin(); it != HUDRadar::getInstance().getRadarObjects().end(); ++it)
    300             {
    301                 float newdist = (*it)->getPosition().squaredDistance(shipPos);
    302                 if (newdist < distance)
    303                 {
    304                     distance = newdist;
    305                     it_ = it;
    306                 }
    307             }
    308 
    309             if (it_ != HUDRadar::getInstance().getRadarObjects().end())
    310             {
    311                 focus_ = *it_;
    312 
    313                 // move the focused object to the begin of the list, so we will iterate through all other objects when cycling
    314                 HUDRadar::getInstance().getRadarObjects().erase(it_);
    315                 HUDRadar::getInstance().getRadarObjects().insert(HUDRadar::getInstance().getRadarObjects().begin(), focus_);
    316                 it_ = HUDRadar::getInstance().getRadarObjects().begin();
    317             }
    318         }
    319         else if (it_ != HUDRadar::getInstance().getRadarObjects().end())
    320         {
    321             focus_->resetMaterial();
    322             ++it_;
    323             if (it_ != HUDRadar::getInstance().getRadarObjects().end())
    324                 focus_ = *it_;
    325             else
    326                 focus_ = 0;
    327         }
    328         else
    329         {
    330             focus_ = 0;
    331         }
    332         updateFocus();
    333     }
    334 
    335     void HUDNavigation::updateFocus()
    336     {
    337         if (focus_)
    338         {
    339             overlay_->show();
    340             focus_->setColour(ColourValue::White);
    341         }
    342         else
    343         {
    344             overlay_->hide();
    345         }
    346     }
    347 
    348     void HUDNavigation::releaseFocus()
    349     {
    350         this->focus_ = 0;
    351         this->updateFocus();
    352     }
    353 
    354291    float HUDNavigation::getDist2Focus() const
    355292    {
    356         if (focus_)
    357             return (focus_->getPosition() - SpaceShip::getLocalShip()->getPosition()).length();
     293        if (Radar::getInstance().getFocus())
     294            return (Radar::getInstance().getFocus()->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length();
    358295        else
    359296            return 0;
     
    377314    }
    378315
    379     /*static*/ HUDNavigation& HUDNavigation::getInstance()
     316    /*static*/ /*HUDNavigation& HUDNavigation::getInstance()
    380317    {
    381318        assert(instance_s);
    382319        return *instance_s;
    383     }
    384 
    385     /*static*/ void HUDNavigation::cycleNavigationFocus()
    386     {
    387         // avoid using getInstance because of the assert().
    388         // User might call this fuction even if HUDNavigation doesn't exist.
    389         if (instance_s)
    390             instance_s->cycleFocus();
    391     }
    392 
    393     /*static*/ void HUDNavigation::releaseNavigationFocus()
    394     {
    395         // avoid using getInstance because of the assert().
    396         // User might call this fuction even if HUDNavigation doesn't exist.
    397         if (instance_s)
    398             instance_s->releaseFocus();
    399     }
     320    }*/
    400321}
  • code/branches/hud/src/orxonox/overlays/hud/HUDNavigation.h

    r1604 r1613  
    5050        void tick(float dt);
    5151
    52         void cycleFocus();
     52        //void cycleFocus();
    5353        float getDist2Focus() const;
    5454
    55         inline RadarObject* getFocus() const
     55        /*inline RadarObject* getFocus() const
    5656            { return this->focus_; }
    57         void releaseFocus();
     57        void releaseFocus();*/
    5858
    59         static void cycleNavigationFocus();
     59        /*static void cycleNavigationFocus();
    6060        static void releaseNavigationFocus();
    61         static HUDNavigation& getInstance();
     61        static HUDNavigation& getInstance();*/
    6262
    6363      protected:
     
    8787        float aimMarkerSize_;                       //!< One paramter size of the aim marker
    8888        Ogre::TextAreaOverlayElement* navText_;     //!< Text overlay to display the target distance
    89         std::list<RadarObject*>::iterator it_;
    90         RadarObject* focus_;                        // next pointer of linked list
    9189        bool wasOutOfView_;                         //!< Performance booster variable: setMaterial is not cheap
    9290
    93         static HUDNavigation* instance_s;
     91        //static HUDNavigation* instance_s;
    9492  };
    9593}
  • code/branches/hud/src/orxonox/overlays/hud/HUDRadar.cc

    r1609 r1613  
    3737#include "objects/SpaceShip.h"
    3838#include "objects/WorldEntity.h"
     39#include "tools/TextureGenerator.h"
    3940#include "RadarViewable.h"
    4041
     
    4243{
    4344    CreateFactory(HUDRadar);
    44     CreateFactory(RadarColour);
    4545    CreateFactory(RadarShape);
    46 
    47     HUDRadar* HUDRadar::instance_s = 0;
    4846
    4947    using namespace Ogre;
     
    5149    HUDRadar::HUDRadar()
    5250      : background_(0)
     51      , marker_(0)
     52      , sensitivity_(1.0f)
    5353    {
    5454        RegisterObject(HUDRadar);
    55 
    56         assert(instance_s == 0);
    57         instance_s = this;
    5855    }
    5956
     
    6259        if (this->isInitialized())
    6360        {
    64             if (this->background_)
     61            /*if (this->background_)
    6562                OverlayManager::getSingleton().destroyOverlayElement(this->background_);
    6663            while (this->radarDots_.size() > 0)
     
    6865                OverlayManager::getSingleton().destroyOverlayElement(this->radarDots_[this->radarDots_.size() - 1]);
    6966                this->radarDots_.pop_back();
    70             }
    71         }
    72 
    73         instance_s = 0;
     67            }*/
     68        }
    7469    }
    7570
     
    7873        OrxonoxOverlay::XMLPort(xmlElement, mode);
    7974
    80         XMLPortObject(HUDRadar, RadarColour, "shapes", addShape, getShape, xmlElement, mode, false, true);
     75        if (mode == XMLPort::LoadObject)
     76        {
     77            this->sensitivity_ = 1.0f;
     78            this->halfDotSizeDistance_ = 3000.0f;
     79            this->maximumDotSize_ = 0.1;
     80        }
     81
     82        XMLPortParam(HUDRadar, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlElement, mode);
     83        XMLPortParam(HUDRadar, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlElement, mode);
     84        XMLPortParam(HUDRadar, "maximumDotSize", setMaximumDotSize, getMaximumDotSize, xmlElement, mode);
     85
     86        shapeMaterials_[RadarViewable::Dot]      = "RadarSquare.tga";
     87        shapeMaterials_[RadarViewable::Triangle] = "RadarSquare.tga";
     88        shapeMaterials_[RadarViewable::Square]   = "RadarSquare.tga";
    8189
    8290        if (mode == XMLPort::LoadObject)
     
    8694            overlay_->add2D(background_);
    8795
    88             // create an array of all possible materials
    89             unsigned int iMaterial = 0;
    90             for (std::map<unsigned int, RadarShape*>::const_iterator itShape = shapes_.begin(); itShape != shapes_.end(); ++itShape)
    91             {
    92                 Ogre::MaterialPtr originalMaterial = (Ogre::MaterialPtr)(Ogre::MaterialManager::getSingleton().getByName((*itShape).second->getAttribute()));
    93                 for (std::map<unsigned int, RadarColour*>::const_iterator itColour = colours_.begin(); itColour != colours_.end(); ++itColour)
    94                 {
    95                     Ogre::MaterialPtr newMaterial = originalMaterial->clone((*itShape).second->getAttribute() + convertToString(iMaterial++));
    96                     newMaterial->getTechnique(0)->getPass(0)->setAmbient((*itColour).second->getAttribute());
    97                     materialNames_[(*itShape).first + ((*itColour).first << 8)] = newMaterial->getName();
    98                 }
    99             }
    100 
    101             /*WorldEntity* object;
    102             object = new WorldEntity();
    103             object->setPosition(2000.0, 0.0, 0.0);
    104             addRadarObject(object, ColourValue(0.5, 0, 0, 1));
    105             object = new WorldEntity();
    106             object->setPosition(0.0, 2000.0, 0.0);
    107             addRadarObject(object, ColourValue(0.5, 0, 0, 1));
    108             object = new WorldEntity();
    109             object->setPosition(0.0, 0.0, 2000.0);
    110             addRadarObject(object, ColourValue(0.5, 0, 0, 1));
    111             object = new WorldEntity();
    112             object->setPosition(10000.0,16000.0,0.0);
    113             addRadarObject(object);*/
    114         }
    115     }
    116 
    117     void HUDRadar::addColour(RadarColour* colour)
    118     {
    119         this->colours_[colour->getIndex()] = colour;
    120     }
    121 
    122     RadarColour* HUDRadar::getColour(unsigned int index) const
    123     {
    124         if (index < this->colours_.size())
    125         {
    126             std::map<unsigned int, RadarColour*>::const_iterator it = colours_.begin();
    127             for (unsigned int i = 0; i != index; ++it, ++i)
    128                 ;
    129             return (*it).second;
    130         }
    131         else
    132             return 0;
    133     }
    134 
    135     void HUDRadar::addShape(RadarShape* shape)
     96            marker_ = (Ogre::PanelOverlayElement*)Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", getName() + "_Marker");
     97            marker_->setMaterialName("Orxonox/RadarMarker");
     98            overlay_->add2D(marker_);
     99            marker_->hide();
     100        }
     101    }
     102
     103    /*void HUDRadar::addShape(RadarShape* shape)
    136104    {
    137105        this->shapes_[shape->getIndex()] = shape;
     
    149117        else
    150118            return 0;
    151     }
    152 
    153     void HUDRadar::tick(float dt)
     119    }*/
     120
     121    void HUDRadar::displayObject(RadarViewable* object, bool bIsMarked)
     122    {
     123        const WorldEntity* wePointer = object->getWorldEntity();
     124
     125        // Just to be sure that we actually have a WorldEntity
     126        // We could do a dynamic_cast, but that's a lot slower
     127        if (!wePointer)
     128        {
     129            CCOUT(4) << "Cannot display a non-WorldEntitiy on the radar" << std::endl;
     130            return;
     131        }
     132
     133        /*static int counter = 0;
     134        if (++counter < 1120 && counter > 120)
     135        {
     136            // we have to create a new entry
     137            Ogre::PanelOverlayElement* panel = static_cast<Ogre::PanelOverlayElement*>(
     138                Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberStr()));
     139            // get right material
     140            panel->setMaterialName("Orxonox/RadarSquare");
     141            panel->setDimensions(0.03, 0.03);
     142            panel->setPosition((1.0 + (rand() & 0xFF) / 256.0 - 0.001) * 0.5, (1.0 - (rand() & 0xFF) / 256.0 - 0.001) * 0.5);
     143            panel->show();
     144            this->overlay_->add2D(panel);
     145            this->overlay_->show();
     146        }*/
     147
     148        // try to find a panel already created
     149        Ogre::PanelOverlayElement* panel;
     150        std::map<RadarViewable*, Ogre::PanelOverlayElement*>::iterator it = this->radarDots_.find(object);
     151        if (it == this->radarDots_.end())
     152        {
     153            // we have to create a new entry
     154            panel = static_cast<Ogre::PanelOverlayElement*>(
     155                Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + getUniqueNumberStr()));
     156            radarDots_[object] = panel;
     157            // get right material
     158            panel->setMaterialName(TextureGenerator::getMaterialName(
     159                shapeMaterials_[object->getRadarObjectType()], object->getRadarObjectColour()));
     160            this->overlay_->add2D(panel);
     161        }
     162        else
     163            panel = (*it).second;
     164
     165        // set size to fit distance...
     166        float distance = (wePointer->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length();
     167        // calculate the size with 1/distance dependency for simplicity (instead of exp(-distance * lambda)
     168        float size = maximumDotSize_ * halfDotSizeDistance_ / (halfDotSizeDistance_ + distance);
     169        panel->setDimensions(size, size);
     170
     171        // calc position on radar...
     172        Vector2 coord = get2DViewcoordinates(SpaceShip::getLocalShip()->getPosition(), SpaceShip::getLocalShip()->getDir(), SpaceShip::getLocalShip()->getOrth(), wePointer->getWorldPosition());
     173        coord *= Ogre::Math::PI / 3.5; // small adjustment to make it fit the texture
     174        panel->setPosition((1.0 + coord.x - size) * 0.5, (1.0 - coord.y - size) * 0.5);
     175
     176        if (bIsMarked)
     177        {
     178            this->marker_->show();
     179            this->marker_->setDimensions(size * 1.2, size * 1.2);
     180            this->marker_->setPosition((1.0 + coord.x - size * 1.2) * 0.5, (1.0 - coord.y - size * 1.2) * 0.5);
     181        }
     182    }
     183
     184    void HUDRadar::radarTick(float dt)
     185    {
     186    }
     187
     188    /*void HUDRadar::tick(float dt)
    154189    {
    155190        // iterate through all RadarObjects
     
    159194            if ((*it)->isVisibleOnRadar())
    160195            {
    161                 WorldEntity* object = (*it)->getWorldEntity();
    162                 // Just to be sure that we actually have a WorldEntity
    163                 // We could do a dynamic_cast, but that's a lot slower
    164                 assert(object);
    165 
    166                 // set size to fit distance...
    167                 float size = 1.0/((object->getWorldPosition() - SpaceShip::getLocalShip()->getPosition()).length());
    168                 size = clamp(size * 100.0f, 0.02f, 0.12f);
    169                 if (i == radarDots_.size())
    170                 {
    171                     // we have to create a new panel
    172                     radarDots_.push_back(static_cast<Ogre::PanelOverlayElement*>(
    173                         Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "RadarDot" + convertToString(i))));
    174                 }
    175                 radarDots_[i]->setDimensions(size, size);
    176 
    177                 // calc position on radar...
    178                 Vector2 coord = get2DViewcoordinates(SpaceShip::getLocalShip()->getPosition(), SpaceShip::getLocalShip()->getDir(), SpaceShip::getLocalShip()->getOrth(), object->getWorldPosition());
    179                 coord *= Ogre::Math::PI / 3.5; // small adjustment to make it fit the texture
    180                 radarDots_[i]->setPosition((1.0 + coord.x) * 0.5, (1.0 - coord.y) * 0.5);
    181 
    182                 // apply the right material
    183                 RadarPoint description = (*it)->getDescription();
    184                 radarDots_[i]->setMaterialName(materialNames_[description.shape_ + (description.colour_ << 8)]);
    185196            }
    186197        }
    187     }
    188 
    189     void HUDRadar::listObjects()
     198    }*/
     199
     200    /*void HUDRadar::listObjects()
    190201    {
    191202        COUT(3) << "List of RadarObjects:\n";
     
    196207            COUT(3) << i++ << ": " << (*it)->getWorldEntity()->getWorldPosition() << std::endl;
    197208        }
    198     }
    199 
    200     /*static*/ HUDRadar& HUDRadar::getInstance()
    201     {
    202         assert(instance_s);
    203         return *instance_s;
    204     }
     209    }*/
    205210}
  • code/branches/hud/src/orxonox/overlays/hud/HUDRadar.h

    r1609 r1613  
    9292
    9393
    94     class _OrxonoxExport HUDRadar : public OrxonoxOverlay, public Tickable, public RadarListener
     94    class _OrxonoxExport HUDRadar : public OrxonoxOverlay, public RadarListener//, public Tickable
    9595    {
    9696      public:
     
    100100        void XMLPort(Element& xmlElement, XMLPort::Mode mode);
    101101
    102         void addShape(RadarShape* shape);
    103         RadarShape* getShape(unsigned int index) const;
     102        /*void addShape(RadarShape* shape);
     103        RadarShape* getShape(unsigned int index) const;*/
    104104
    105         void tick(float dt);
     105        float getRadarSensitivity() const { return this->sensitivity_; }
     106        void setRadarSensitivity(float sensitivity) { this->sensitivity_ = sensitivity; }
    106107
    107         void listObjects();
     108        float getHalfDotSizeDistance() const { return this->halfDotSizeDistance_; }
     109        void setHalfDotSizeDistance(float distance) { this->halfDotSizeDistance_ = distance; }
     110
     111        float getMaximumDotSize() const { return this->maximumDotSize_; }
     112        void setMaximumDotSize(float size) { this->maximumDotSize_ = size; }
     113
     114        //void tick(float dt);
     115
     116        //void listObjects();
    108117
    109118      private:
    110         void addColour(RadarColour* colour);
    111         RadarColour* getColour(unsigned int index) const;
     119        void displayObject(RadarViewable* viewable, bool bIsMarked);
     120        void hideMarker() { this->marker_->hide(); }
     121        float getRadarSensitivity() { return 1.0f; }
     122        void radarTick(float dt);
    112123
    113         std::map<unsigned int, std::string> materialNames_;
    114         std::map<unsigned int, RadarColour*> colours_;
    115         std::map<unsigned int, RadarShape*> shapes_;
     124        std::map<RadarViewable::Shape, std::string> shapeMaterials_;
    116125
    117126        Ogre::PanelOverlayElement* background_;
    118127        std::map<RadarViewable*, Ogre::PanelOverlayElement*> radarDots_;
     128        Ogre::PanelOverlayElement* marker_;
     129
     130        float halfDotSizeDistance_;
     131        float maximumDotSize_;
     132
     133        float sensitivity_;
    119134    };
    120135}
Note: See TracChangeset for help on using the changeset viewer.