Hi Nick,

I already have 1 update callback that sets the node at the right altitude in 
the scene given the current position: 
Just to go over it again, the problem I'm getting is that the value of my PAT 
is reset to 0 when I call my function setBackhoe(). Is this because my PAT node 
which was declared in main() is getting out of scope in the while loop and the 
function called from the while loop ? 

------------------------------------------------------------------------------------
Since the code is quite a few lines, I'm copying the relevant parts as it might 
get way too long otherwise.
Below is the update callback () I'm using:


Code:
void BackhoeUpdatePositionCallback::operator()(osg::Node* node, 
osg::NodeVisitor* nv)
{
        osg::PositionAttitudeTransform* backhoePosition = 
                dynamic_cast<osg::PositionAttitudeTransform*> (node);
        osg::Vec3 backhoeGroundPosition;
        if(backhoePosition)
        {
                backhoeGroundPosition = backhoePosition->getPosition();
                backhoeGroundPosition = 
getCurrentPosition(backhoeGroundPosition);
                backhoePosition->setPosition(backhoeGroundPosition);
        }
        traverse(node, nv);
}

osg::Vec3 BackhoeUpdatePositionCallback::getCurrentPosition(osg::Vec3 
currentPosition)
{
        double xPosition;
        double yPosition;

        xPosition = currentPosition.x();
        yPosition = currentPosition.y();

        osgUtil::LineSegmentIntersector* backhoeLocationSegment = new 
osgUtil::LineSegmentIntersector(
                osg::Vec3(xPosition, yPosition, 999),
                osg::Vec3(xPosition, yPosition, -999));
        osgUtil::IntersectionVisitor findBackhoeElevationVisitor;
    findBackhoeElevationVisitor.setIntersector(backhoeLocationSegment);
        surface->accept(findBackhoeElevationVisitor);
        osgUtil::LineSegmentIntersector::Intersection backhoeIntersection = 
                                             
backhoeLocationSegment->getFirstIntersection();
        osg::Vec3 backhoeContactPoint = 
backhoeIntersection.getWorldIntersectPoint();
        return backhoeContactPoint;
}



------------------------------------------------------------------------------------

The main() is:


Code:

int main( int argc, char **argv )
{
  //ROOT NODE:
        osg::Group* root = NULL;
        root = new osg::Group();
        
        //Surface object Geode:
  osg::Node *surface = new osg::Geode();
  surface = osgDB::readNodeFile("NorthCampusLarge.earth");  

        if(!surface)
        {
                std::cout << "surface is null" << std::endl;
          _getch();
          return 0;
        }

  //MapNode:
  osgEarth::MapNode* mapNode = MapNode::findMapNode(surface);
  osgEarth::SpatialReference* mapSRS = 
osgEarth::SpatialReference::create("UTM17N");
  
        //object placer for surface model:
  osgEarthUtil::ObjectPlacer* objectPlacer = new 
osgEarthUtil::ObjectPlacer(mapNode, 0, true);

        //Backhoe object:
        BackhoeClass backhoe1("one");
        BackhoeClass backhoe2("two");

...
//ObjectLocator nodes:
        osgEarthUtil::ObjectLocator* objectLocator = new 
osgEarthUtil::ObjectLocator(mapSRS);   
        
        //Get PAT handle to the backhoe object.
        osg::PositionAttitudeTransform* backhoe1PAT = new 
osg::PositionAttitudeTransform();     
        osg::PositionAttitudeTransform* backhoe2PAT = new 
osg::PositionAttitudeTransform();
        osg::PositionAttitudeTransform* cab1PAT = new 
osg::PositionAttitudeTransform();

        //attach 1st backhoe to its PAT
        osg::Group* backhoe1Group = new osg::Group();
        backhoe1Group = backhoe1.getBackhoe();
        backhoe1PAT->addChild(backhoe1Group);
        
backhoe1PAT->setAttitude(osg::Quat(osg::DegreesToRadians(90.0f),osg::Vec3d(0, 
0, 1))); 
...
....
...
//attach callback functions to backhoe PATs:
        backhoe1PAT->setUpdateCallback(new 
BackhoeUpdatePositionCallback(surface));
...
viewer.realize();
    while( !viewer.done() )
    {
      //osg::Vec3d positionVector;
      //the while loop ensures that we use legitimate gps coordinates
      //right from the first update callback.
      while(gpsLat == -999 || gpsLon == -999)
      {
        receiveSignal(gpsInstance, gpsLat, gpsLon);                
      }
                  setBackhoe(gpsLat, gpsLon, mapNode, surface, objectPlacer, 
backhoe1PAT);
            // fire off the cull and draw traversals of the scene.
            viewer.frame();     
}       
    return 0;
}



------------------------------------------------------------------------------------

The function to set the object at its new location is:

Code:
//function called to set backhoe at its new lat-long position
void setBackhoe(double gpsLat, 
                double gpsLon, 
                osgEarth::MapNode* mapNode,
                osg::Node *surface,                
                osgEarthUtil::ObjectPlacer* objectPlacer, 
                osg::PositionAttitudeTransform* backhoe1PAT
                //osg::Vec3d &positionVector
                ) 
{
  osg::Matrixd positionMatrix1; 
        objectPlacer->createPlacerMatrix(gpsLat, gpsLon, 131, positionMatrix1);
        //osg::EllipsoidModel* ellipsoidModel = new osg::EllipsoidModel();
        //ellipsoidModel = mapNode->getEllipsoidModel();
        static double latitude;
        static double longitude;
        static double height;
        static double X1 = positionMatrix1.getTrans().x();
        static double Y1 = positionMatrix1.getTrans().y();
  static double Z1 = positionMatrix1.getTrans().z();
        //get SpatialReferenceSystem SRS for terrain (i.e. map) and lat-long.
        const SpatialReference* terrain_srs = 
mapNode->getMap()->getProfile()->getSRS();
  const SpatialReference* latlong_srs = terrain_srs->getGeographicSRS();
        //convert from projected to lat-long:
        terrain_srs->transform(X1, Y1, latlong_srs, longitude, latitude ); 
        //to go from lat-long to projected:
        latlong_srs->transform(longitude, latitude, terrain_srs, X1, Y1);
  //set backhoe 1 exactly over terrain: letting the callback do that work.
        //set the PAT value of backhoe1:
        backhoe1PAT->setPosition(osg::Vec3d(X1, Y1, 0.0));
        return ;
}



Excuse me for my rather lengthy post. But I guess the earlier one had too 
little code in it to infer anything.

Thanks again,

Sanat.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=30198#30198





_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to