From 268b79d03f1b28d93617d299aeead9756b42cd86 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sun, 3 Aug 2008 20:35:45 +0000 Subject: [PATCH] Improved the terrain intersection code so that it produces more stable terrain movements. --- src/osgGA/TerrainManipulator.cpp | 38 +++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/osgGA/TerrainManipulator.cpp b/src/osgGA/TerrainManipulator.cpp index 504fbd133..5566305ec 100644 --- a/src/osgGA/TerrainManipulator.cpp +++ b/src/osgGA/TerrainManipulator.cpp @@ -475,22 +475,34 @@ bool TerrainManipulator::calcMovement() CoordinateFrame coordinateFrame = getCoordinateFrame(_center); // need to reintersect with the terrain - double distance = _node->getBound().radius()*0.1f; - osg::Vec3d start_segment = _center + getUpVector(coordinateFrame) * distance; - osg::Vec3d end_segment = start_segment - getUpVector(coordinateFrame) * (2.0f*distance); - - osg::notify(INFO)<<"start="<getBound().radius()*0.25f; + + osg::Vec3d ip1; + osg::Vec3d ip2; + bool hit_ip1 = intersect(_center, _center + getUpVector(coordinateFrame) * distance, ip1); + bool hit_ip2 = intersect(_center, _center - getUpVector(coordinateFrame) * distance, ip2); + if (hit_ip1) { - notify(INFO) << "Hit terrain ok"<< std::endl; - - _center = ip; - + if (hit_ip2) + { + _center = (_center-ip1).length2() < (_center-ip2).length2() ? + ip1 : + ip2; + + hitFound = true; + } + else + { + _center = ip1; + hitFound = true; + } + } + else if (hit_ip2) + { + _center = ip2; hitFound = true; } - + if (!hitFound) { // ??