Show
Ignore:
Timestamp:
05/21/05 14:57:28 (10 years ago)
Author:
robert
Message:

Updates to osgcatch game to allow different types of falling objects

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgcatch/osgcatch.cpp

    r4071 r4280  
    3232 
    3333typedef std::vector<std::string> FileList; 
     34typedef std::map<std::string, osg::ref_ptr<osg::Node> >  ObjectMap; 
     35 
     36static ObjectMap    s_objectMap; 
    3437 
    3538class Character : public osg::Referenced 
     
    3841    Character(); 
    3942     
    40     void setCharacter(const std::string& filename, const std::string& name, const osg::Vec3& orgin, const osg::Vec3& width, float positionRatio); 
     43    void setCharacter(const std::string& filename, const std::string& name, const osg::Vec3& orgin, const osg::Vec3& width, const osg::Vec3& catchPos, float positionRatio); 
    4144     
    4245    void setLives(const std::string& filename, const osg::Vec3& orgin, const osg::Vec3& delta, unsigned int numLives); 
     
    8689 
    8790 
    88 void Character::setCharacter(const std::string& filename, const std::string& name, const osg::Vec3& origin, const osg::Vec3& width, float positionRatio) 
     91void Character::setCharacter(const std::string& filename, const std::string& name, const osg::Vec3& origin, const osg::Vec3& width, const osg::Vec3& catchPos, float positionRatio) 
    8992{ 
    9093    _origin = origin; 
     
    118121        moveTo(positionRatio); 
    119122 
    120         _centerBasket = width*0.2 + height*0.57 + pos; 
    121         _radiusBasket = width.length()*0.34; 
     123        _centerBasket = width*catchPos.x() + height*catchPos.y() + pos; 
     124        _radiusBasket = width.length()*catchPos.z(); 
    122125 
    123126    } 
     
    277280        double                                       _timeToRemove; 
    278281 
     282        static void setUpCatchablesMap(const FileList& fileList); 
    279283 
    280284    public: 
     
    347351        float       _viscosityCoefficient; 
    348352        float       _densityCoefficeint; 
     353         
    349354  
    350355}; 
     
    357362    _timeToRemove = -1.0; // do not remove. 
    358363    setFluidToAir(); 
     364} 
     365 
     366void CatchableObject::setUpCatchablesMap(const FileList& fileList) 
     367{ 
     368    for(FileList::const_iterator itr=fileList.begin(); 
     369        itr!=fileList.end(); 
     370        ++itr) 
     371    { 
     372        const std::string& filename = *itr; 
     373        osg::Image* image = osgDB::readImageFile(filename); 
     374        if (image) 
     375        { 
     376            osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet(); 
     377            stateset->setTextureAttributeAndModes(0,new osg::Texture2D(image),osg::StateAttribute::ON); 
     378            stateset->setMode(GL_BLEND,osg::StateAttribute::ON); 
     379            stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); 
     380             
     381            osg::Vec3 width((float)(image->s())/(float)(image->t()),0.0f,0.0); 
     382            osg::Vec3 height(0.0f,0.0f,1.0f); 
     383            osg::Vec3 pos = (width+height)*-0.5f; 
     384 
     385            osg::Geometry* geometry = osg::createTexturedQuadGeometry(pos,width,height); 
     386            geometry->setStateSet(stateset.get()); 
     387 
     388            osg::Geode* geode = new osg::Geode; 
     389            geode->addDrawable(geometry); 
     390 
     391            s_objectMap[filename] = geode; 
     392        } 
     393    } 
    359394} 
    360395 
     
    368403    _mass = 1000.0*Volume; 
    369404 
    370     osg::Image* image = osgDB::readImageFile(filename); 
    371     if (image) 
    372     { 
    373         osg::Vec3 width(characterSize*((float)image->s())/(float)(image->t()),0.0f,0.0); 
    374         osg::Vec3 height(0.0f,0.0f,characterSize); 
    375         osg::Vec3 pos = (width+height)*-0.5f; 
    376  
    377         osg::Geometry* geometry = osg::createTexturedQuadGeometry(pos,width,height); 
    378         osg::StateSet* stateset = geometry->getOrCreateStateSet(); 
    379         stateset->setTextureAttributeAndModes(0,new osg::Texture2D(image),osg::StateAttribute::ON); 
    380         stateset->setMode(GL_BLEND,osg::StateAttribute::ON); 
    381         stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); 
    382  
    383         osg::Geode* geode = new osg::Geode; 
    384         geode->addDrawable(geometry); 
     405    if (s_objectMap.count(filename)!=0) 
     406    { 
     407        osg::PositionAttitudeTransform* scaleTransform = new osg::PositionAttitudeTransform; 
     408        scaleTransform->setScale(osg::Vec3(characterSize,characterSize,characterSize)); 
     409        scaleTransform->addChild(s_objectMap[filename].get()); 
    385410 
    386411        _object = new osg::PositionAttitudeTransform; 
    387412        _object->setName(name); 
    388         _object->addChild(geode); 
    389413        _object->setPosition(center); 
    390     } 
    391  
     414        _object->addChild(scaleTransform); 
     415    } 
     416    else 
     417    { 
     418        osg::notify(osg::NOTICE)<<"CatchableObject::setObject("<<filename<<") not able to create catchable object."<<std::endl; 
     419    } 
    392420} 
    393421 
     
    480508     
    481509    void createNewCatchable(); 
     510 
     511    enum Players 
     512    { 
     513        PLAYER_GIRL, 
     514        PLAYER_BOY 
     515    }; 
     516 
     517    void addPlayer(Players player) 
     518    { 
     519        osg::Vec3 livesPosition; 
     520        osg::Vec3 catchesPosition; 
     521        if (_numberOfPlayers==0) 
     522        { 
     523            livesPosition = _originBaseLine+osg::Vec3(0.0f,-0.5f,0.0f); 
     524            catchesPosition = _originBaseLine+osg::Vec3(200.0f,-0.5f,0.0f); 
     525        } 
     526        else 
     527        { 
     528            livesPosition = _originBaseLine+osg::Vec3(900.0f,-0.5f,000.0f); 
     529            catchesPosition = _originBaseLine+osg::Vec3(1100.0f,-0.5f,0.0f); 
     530        } 
     531         
     532        switch(player) 
     533        { 
     534            case PLAYER_GIRL: 
     535            { 
     536                std::string player_one = "Catch/girl.png";  
     537                osg::Vec3 catchPos(0.2, 0.57, 0.34); 
     538 
     539                _players[_numberOfPlayers].setCharacter(player_one,"girl", _originBaseLine + osg::Vec3(0.0f,-1.0f,0.0f), _widthBaseLine, catchPos, 0.5f); 
     540                _players[_numberOfPlayers].setLives(player_one,livesPosition, osg::Vec3(0.0f,0.0f,100.0f),3); 
     541                _players[_numberOfPlayers].setCatches("Catch/broach.png",catchesPosition, osg::Vec3(0.0f,0.0f,100.0f),10); 
     542 
     543                ++_numberOfPlayers; 
     544                break; 
     545            } 
     546            case PLAYER_BOY: 
     547            { 
     548                std::string player_two = "Catch/boy.png";  
     549                osg::Vec3 catchPos(0.8, 0.57, 0.34); 
     550 
     551                _players[_numberOfPlayers].setCharacter(player_two,"boy", _originBaseLine + osg::Vec3(0.0f,-2.0f,0.0f), _widthBaseLine, catchPos, 0.5f); 
     552                _players[_numberOfPlayers].setLives(player_two,livesPosition, osg::Vec3(0.0f,0.0f,100.0f),3); 
     553                _players[_numberOfPlayers].setCatches("Catch/broach.png",catchesPosition, osg::Vec3(0.0f,0.0f,100.0f),10); 
     554 
     555                 ++_numberOfPlayers; 
     556               break; 
     557            } 
     558        }                 
     559    } 
     560         
     561         
    482562 
    483563protected: 
     
    499579    osg::ref_ptr<osg::Group> _group; 
    500580     
    501     Character _player1; 
    502     Character _player2; 
     581    unsigned int _numberOfPlayers; 
     582    Character _players[2]; 
    503583 
    504584    typedef std::list< osg::ref_ptr<CatchableObject> > CatchableObjectList; 
    505585    CatchableObjectList _catchableObjects; 
     586     
     587    FileList _backgroundFiles; 
     588    FileList _benignCatachables; 
    506589 
    507590    bool _leftKeyPressed; 
     
    522605    _characterSize = _width.length()*0.2f; 
    523606 
    524     _backgroundImageFile = "Catch/sky1.JPG"; 
     607    _backgroundImageFile = "Catch/farm.JPG"; 
     608     
     609    _numberOfPlayers = 0; 
    525610 
    526611    _leftKeyPressed=false; 
    527612    _rightKeyPressed=false; 
     613 
     614    _backgroundFiles.push_back("Catch/farm.JPG"); 
     615    _backgroundFiles.push_back("Catch/sky1.JPG"); 
     616    _backgroundFiles.push_back("Catch/sky2.JPG"); 
     617    _backgroundFiles.push_back("Catch/sky3.JPG"); 
     618 
     619    _benignCatachables.push_back("Catch/a.png"); 
     620    _benignCatachables.push_back("Catch/b.png"); 
     621    _benignCatachables.push_back("Catch/c.png"); 
     622    _benignCatachables.push_back("Catch/m.png"); 
     623    _benignCatachables.push_back("Catch/n.png"); 
     624    _benignCatachables.push_back("Catch/s.png"); 
     625    _benignCatachables.push_back("Catch/t.png"); 
     626    _benignCatachables.push_back("Catch/u.png"); 
     627    _benignCatachables.push_back("Catch/ball.png"); 
     628     
     629    CatchableObject::setUpCatchablesMap(_benignCatachables); 
     630     
     631     
     632 
    528633} 
    529634 
     
    537642            if (_leftKeyPressed) 
    538643            { 
    539                 _player2.moveLeft(); 
     644                if (_numberOfPlayers>=2) _players[1].moveLeft(); 
    540645            } 
    541646             
    542647            if (_rightKeyPressed) 
    543648            { 
    544                 _player2.moveRight(); 
     649                if (_numberOfPlayers>=2) _players[1].moveRight(); 
    545650            } 
    546651 
     
    558663                bool removeEntry = false; 
    559664 
    560                 if ((*itr)->dangerous()) 
     665                for(unsigned int i=0;i<_numberOfPlayers;++i) 
    561666                { 
    562                     if ((*itr)->anyInside(_player1.getLowerLeft(),_player1.getUpperRight())) 
     667                    if ((*itr)->dangerous()) 
    563668                    { 
    564                         _player1.looseLife(); 
    565                         removeEntry = true; 
     669                        if ((*itr)->anyInside(_players[i].getLowerLeft(),_players[i].getUpperRight())) 
     670                        { 
     671                            _players[i].looseLife(); 
     672                            removeEntry = true; 
     673                        } 
    566674                    } 
    567  
    568                     if ((*itr)->anyInside(_player2.getLowerLeft(),_player2.getUpperRight())) 
     675                    else 
    569676                    { 
    570                         _player2.looseLife(); 
    571                         removeEntry = true; 
     677                        if ((*itr)->centerInside(_players[i].getCurrentCenterOfBasket(),_players[i].getCurrentRadiusOfBasket())) 
     678                        { 
     679                            _players[i].addCatch(); 
     680                            removeEntry = true; 
     681                        } 
    572682                    } 
    573683                } 
    574                 else 
    575                 { 
    576                     if ((*itr)->centerInside(_player1.getCurrentCenterOfBasket(),_player1.getCurrentRadiusOfBasket())) 
    577                     { 
    578                         _player1.addCatch(); 
    579                         removeEntry = true; 
    580                     } 
    581  
    582                     if ((*itr)->centerInside(_player2.getCurrentCenterOfBasket(),_player2.getCurrentRadiusOfBasket())) 
    583                     { 
    584                         _player2.addCatch(); 
    585                         removeEntry = true; 
    586                     } 
    587                 } 
    588  
    589684 
    590685                if (!(*itr)->anyInside(_origin, _origin+_width+_height) ||  
     
    632727                return true; 
    633728            } 
    634             else if (ea.getKey()=='1') 
    635             { 
    636                 _player1.looseLife(); 
    637                 _player1.addCatch(); 
    638                 return true; 
    639             } 
    640             else if (ea.getKey()=='2') 
    641             { 
    642                 _player2.looseLife(); 
    643                 _player2.addCatch(); 
    644                 return true; 
    645             } 
    646729            else if (ea.getKey()==' ') 
    647730            { 
    648                 _player1.reset(); 
    649                 _player2.reset(); 
     731                for(unsigned int i=0;i<_numberOfPlayers;++i) 
     732                { 
     733                    _players[i].reset(); 
     734                } 
    650735                return true; 
    651736            } 
     
    669754            float px = (ea.getXnormalized()+1.0f)*0.5f; 
    670755 
    671             _player1.moveTo(px); 
     756            if (_numberOfPlayers>=1) _players[0].moveTo(px); 
    672757 
    673758            return true; 
     
    698783    _group = new osg::Group; 
    699784 
    700     std::string player_one = "Catch/girl.png";  
    701     std::string player_two = "Catch/boy.png";  
    702  
    703     _player1.setCharacter(player_one,"girl", _originBaseLine + osg::Vec3(0.0f,-1.0f,0.0f), _widthBaseLine, 0.4f); 
    704     _player1.setLives(player_one,_originBaseLine+osg::Vec3(0.0f,-0.5f,0.0f), osg::Vec3(0.0f,0.0f,100.0f),3); 
    705     _player1.setCatches("Catch/broach.png",_originBaseLine+osg::Vec3(200.0f,-0.5f,0.0f), osg::Vec3(0.0f,0.0f,100.0f),10); 
    706     _group->addChild(_player1._character.get()); 
    707     _group->addChild(_player1._livesSwitch.get()); 
    708     _group->addChild(_player1._catchSwitch.get()); 
    709  
    710     _player2.setCharacter(player_two,"boy", _originBaseLine + osg::Vec3(0.0f,-2.0f,0.0f), _widthBaseLine, 0.4f); 
    711     _player2.setLives(player_two,_originBaseLine+osg::Vec3(900.0f,-0.5f,000.0f), osg::Vec3(0.0f,0.0f,100.0f),3); 
    712     _player2.setCatches("Catch/broach.png",_originBaseLine+osg::Vec3(1100.0f,-0.5f,0.0f), osg::Vec3(0.0f,0.0f,100.0f),10); 
    713     _group->addChild(_player2._character.get()); 
    714     _group->addChild(_player2._livesSwitch.get()); 
    715     _group->addChild(_player2._catchSwitch.get()); 
    716      
     785    if (_numberOfPlayers==0) 
     786    { 
     787        addPlayer(PLAYER_GIRL); 
     788    } 
     789 
     790    for(unsigned int i=0;i<_numberOfPlayers;++i) 
     791    { 
     792        _group->addChild(_players[i]._character.get()); 
     793        _group->addChild(_players[i]._livesSwitch.get()); 
     794        _group->addChild(_players[i]._catchSwitch.get()); 
     795    }     
    717796     
    718797    createNewCatchable(); 
     
    743822void GameEventHandler::createNewCatchable() 
    744823{ 
     824    if (_benignCatachables.empty()) return; 
     825 
     826    unsigned int catachableIndex = (unsigned int)((float)_benignCatachables.size()*(float)rand()/(float)RAND_MAX); 
     827    if (catachableIndex>=_benignCatachables.size()) catachableIndex = _benignCatachables.size()-1; 
     828     
     829    const std::string& filename = _benignCatachables[catachableIndex]; 
     830 
    745831    float ratio = ((float)rand() / (float)RAND_MAX); 
    746832    float size = 100.0f*((float)rand() / (float)RAND_MAX); 
     
    752838    osg::Vec3 velocity(-cosf(angle)*speed,0.0f,-sinf(angle)*speed); 
    753839    //std::cout<<"angle = "<<angle<<" velocity="<<velocity<<std::endl; 
    754     catchableObject->setObject("Catch/a.png","boy",position,size,velocity); 
     840    catchableObject->setObject(filename,"boy",position,size,velocity); 
    755841    _catchableObjects.push_back(catchableObject); 
    756842 
     
    793879 
    794880 
     881    while (arguments.read("--boy")) seh->addPlayer(GameEventHandler::PLAYER_BOY); 
     882    while (arguments.read("--girl")) seh->addPlayer(GameEventHandler::PLAYER_GIRL); 
     883     
     884     
     885 
    795886    // get details on keyboard and mouse bindings used by the viewer. 
    796887    viewer.getUsage(*arguments.getApplicationUsage());