]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/groundcache.cxx
- fix two bugs
[flightgear.git] / src / FDM / groundcache.cxx
index 6c2654083a74e13d8812dfebc8974db71849d0f4..d4cfb80a9612a8b4a8de9b9b4b0f959dcea5ba70 100644 (file)
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
 
 #include <float.h>
 
@@ -29,6 +32,8 @@
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/math/sg_geodesy.hxx>
+#include <simgear/scene/material/mat.hxx>
+#include <simgear/scene/material/matlib.hxx>
 
 #include <Main/globals.hxx>
 #include <Scenery/scenery.hxx>
@@ -202,7 +207,7 @@ FGGroundCache::extractGroundProperty( ssgLeaf* l )
 {
   // FIXME: Do more ...
   // Idea: have a get_globals() function which knows about that stuff.
-  // Or most propably read that from a configuration file,
+  // Or most probably read that from a configuration file,
   // from property tree or whatever ...
   
   // Get ground dependent data.
@@ -226,34 +231,19 @@ FGGroundCache::extractGroundProperty( ssgLeaf* l )
     }
 
     // Copy the velocity from the carrier class.
-    ud->carrier->getVelocityWrtEarth( gp.vel );
+    ud->carrier->getVelocityWrtEarth( gp.vel, gp.rot, gp.pivot );
   }
 
   else {
-
     // Initialize velocity field.
-    sgSetVec3( gp.vel, 0.0, 0.0, 0.0 );
-  }
-  
-  // Get the texture name and decide what ground type we have.
-  ssgState *st = l->getState();
-  if (st != NULL && st->isAKindOf(ssgTypeSimpleState())) {
-    ssgSimpleState *ss = (ssgSimpleState*)st;
-    SGPath fullPath( ss->getTextureFilename() ? ss->getTextureFilename(): "" );
-    string file = fullPath.file();
-    SGPath dirPath(fullPath.dir());
-    string category = dirPath.file();
-    
-    if (category == "Runway")
-      gp.type = FGInterface::Solid;
-    else {
-      if (file == "asphault.rgb" || file == "airport.rgb")
-        gp.type = FGInterface::Solid;
-      else if (file == "water.rgb" || file == "water-lake.rgb")
-        gp.type = FGInterface::Water;
-      else if (file == "forest.rgb" || file == "cropwood.rgb")
-        gp.type = FGInterface::Forest;
-    }
+    sgdSetVec3( gp.vel, 0.0, 0.0, 0.0 );
+    sgdSetVec3( gp.rot, 0.0, 0.0, 0.0 );
+    sgdSetVec3( gp.pivot, 0.0, 0.0, 0.0 );
+
+    // get some material information for use in the gear model
+    gp.material = globals->get_matlib()->findMaterial(l);
+    if (gp.material)
+      gp.type = gp.material->get_solid() ? FGInterface::Solid : FGInterface::Water;
   }
   
   return gp;
@@ -285,7 +275,9 @@ FGGroundCache::putLineLeafIntoCache(const sgdSphere *wsp, const sgdMat4 xform,
         Wire wire;
         sgdCopyVec3(wire.ends[0], ends[0]);
         sgdCopyVec3(wire.ends[1], ends[1]);
-        sgdSetVec3(wire.velocity, gp.vel);
+        sgdCopyVec3(wire.velocity, gp.vel);
+        sgdCopyVec3(wire.rotation, gp.rot);
+        sgdSubVec3(wire.rotation_pivot, gp.pivot, cache_center);
         wire.wire_id = gp.wire_id;
 
         wires.push_back(wire);
@@ -294,7 +286,9 @@ FGGroundCache::putLineLeafIntoCache(const sgdSphere *wsp, const sgdMat4 xform,
         Catapult cat;
         sgdCopyVec3(cat.start, ends[0]);
         sgdCopyVec3(cat.end, ends[1]);
-        sgdSetVec3(cat.velocity, gp.vel);
+        sgdCopyVec3(cat.velocity, gp.vel);
+        sgdCopyVec3(cat.rotation, gp.rot);
+        sgdSubVec3(cat.rotation_pivot, gp.pivot, cache_center);
 
         catapults.push_back(cat);
       }
@@ -313,6 +307,7 @@ FGGroundCache::putSurfaceLeafIntoCache(const sgdSphere *sp,
   for (int i = 0; i < nt; ++i) {
     Triangle t;
     t.sphere.empty();
+    t.material = gp.material;
     short v[3];
     l->getTriangle(i, &v[0], &v[1], &v[2]);
     for (int k = 0; k < 3; ++k) {
@@ -334,7 +329,9 @@ FGGroundCache::putSurfaceLeafIntoCache(const sgdSphere *sp,
     // Check if the sphere around the vehicle intersects the sphere
     // around that triangle. If so, put that triangle into the cache.
     if (sphIsec && sp->intersects(&t.sphere)) {
-      sgdSetVec3(t.velocity, gp.vel);
+      sgdCopyVec3(t.velocity, gp.vel);
+      sgdCopyVec3(t.rotation, gp.rot);
+      sgdSubVec3(t.rotation_pivot, gp.pivot, cache_center);
       t.type = gp.type;
       triangles.push_back(t);
     }
@@ -348,11 +345,18 @@ FGGroundCache::putSurfaceLeafIntoCache(const sgdSphere *sp,
       sgdVec3 isectpoint;
       if ( sgdIsectInfLinePlane( isectpoint, tmp, down, t.plane ) &&
            fgdPointInTriangle( isectpoint, t.vertices ) ) {
-        found_ground = true;
-        sgdAddVec3(isectpoint, cache_center);
-        double this_radius = sgdLengthVec3(isectpoint);
-        if (ground_radius < this_radius)
-          ground_radius = this_radius;
+        // Compute the offset to the ground cache midpoint
+        sgdVec3 off;
+        sgdSubVec3(off, isectpoint, tmp);
+        // Only accept the altitude if the intersection point is below the
+        // ground cache midpoint
+        if (0 < sgdScalarProductVec3( off, down )) {
+          found_ground = true;
+          sgdAddVec3(isectpoint, cache_center);
+          double this_radius = sgdLengthVec3(isectpoint);
+          if (ground_radius < this_radius)
+            ground_radius = this_radius;
+        }
       }
     }
   }
@@ -373,15 +377,26 @@ FGGroundCache::velocityTransformTriangle(double dt,
   dst.sphere.radius = src.sphere.radius;
 
   sgdCopyVec3(dst.velocity, src.velocity);
+  sgdCopyVec3(dst.rotation, src.rotation);
+  sgdCopyVec3(dst.rotation_pivot, src.rotation_pivot);
 
   dst.type = src.type;
+  dst.material = src.material;
 
   if (dt*sgdLengthSquaredVec3(src.velocity) != 0) {
-    sgdAddScaledVec3(dst.vertices[0], src.velocity, dt);
-    sgdAddScaledVec3(dst.vertices[1], src.velocity, dt);
-    sgdAddScaledVec3(dst.vertices[2], src.velocity, dt);
+    sgdVec3 pivotoff, vel;
+    for (int i = 0; i < 3; ++i) {
+      sgdSubVec3(pivotoff, src.vertices[i], src.rotation_pivot);
+      sgdVectorProductVec3(vel, src.rotation, pivotoff);
+      sgdAddVec3(vel, src.velocity);
+      sgdAddScaledVec3(dst.vertices[i], vel, dt);
+    }
     
-    dst.plane[3] += dt*sgdScalarProductVec3(dst.plane, src.velocity);
+    // Transform the plane equation
+    sgdSubVec3(pivotoff, dst.plane, src.rotation_pivot);
+    sgdVectorProductVec3(vel, src.rotation, pivotoff);
+    sgdAddVec3(vel, src.velocity);
+    dst.plane[3] += dt*sgdScalarProductVec3(dst.plane, vel);
 
     sgdAddScaledVec3(dst.sphere.center, src.velocity, dt);
   }
@@ -416,7 +431,7 @@ FGGroundCache::cache_fill(ssgBranch *branch, sgdMat4 xform,
     if ( e->isAKindOf( ssgTypeBranch() ) ) {
       ssgBranch *b = (ssgBranch *)e;
       if ( b->isAKindOf( ssgTypeTransform() ) ) {
-        // Collect up the transfors required to reach that part of
+        // Collect up the transforms required to reach that part of
         // the branch.
         sgMat4 xform2;
         sgMakeIdentMat4( xform2 );
@@ -433,9 +448,9 @@ FGGroundCache::cache_fill(ssgBranch *branch, sgdMat4 xform,
     // This will minimize the number of vertices/triangles in the cache.
     else if (e->isAKindOf(ssgTypeLeaf())) {
       // Since we reach that leaf if we have an intersection with the
-      // most propably bigger wire/catapult cache sphere, we need to check
+      // most probably bigger wire/catapult cache sphere, we need to check
       // that here, if the smaller cache for the surface has a chance for hits.
-      // Also, if the spheres do not intersect compute a croase agl value
+      // Also, if the spheres do not intersect compute a coarse agl value
       // by following the line downwards originating at the aircraft.
       bool spIsec = sp->intersects(&esphere);
       putSurfaceLeafIntoCache(sp, xform, spIsec, down, (ssgLeaf *)e);
@@ -468,7 +483,7 @@ FGGroundCache::prepare_ground_cache(double ref_time, const double pt[3],
   // Decide where we put the scenery center.
   Point3D old_cntr = globals->get_scenery()->get_center();
   Point3D cntr(pt[0], pt[1], pt[2]);
-  // Only move the cache center if it is unaccaptable far away.
+  // Only move the cache center if it is unacceptable far away.
   if (40*40 < old_cntr.distance3Dsquared(cntr))
     globals->get_scenery()->set_center(cntr);
   else
@@ -498,7 +513,7 @@ FGGroundCache::prepare_ground_cache(double ref_time, const double pt[3],
   sgdSetVec3(down, -pt[0], -pt[1], -pt[2]);
   sgdNormalizeVec3(down);
 
-  // We collaps all transforms we need to reach a particular leaf.
+  // We collapse all transforms we need to reach a particular leaf.
   // The leafs itself will be then transformed later.
   // So our cache is just flat.
   // For leafs which are moving (carriers surface, etc ...)
@@ -514,7 +529,7 @@ FGGroundCache::prepare_ground_cache(double ref_time, const double pt[3],
   cache_fill(terrain, xform, &acSphere, down, &wireSphere);
 
   // some stats
-  SG_LOG(SG_FLIGHT,SG_INFO, "prepare_ground_cache(): ac radius = " << rad
+  SG_LOG(SG_FLIGHT,SG_DEBUG, "prepare_ground_cache(): ac radius = " << rad
          << ", # triangles = " << triangles.size()
          << ", # wires = " << wires.size()
          << ", # catapults = " << catapults.size()
@@ -554,15 +569,23 @@ FGGroundCache::get_cat(double t, const double dpt[3],
 
   size_t sz = catapults.size();
   for (size_t i = 0; i < sz; ++i) {
+    sgdVec3 pivotoff, rvel[2];
     sgdLineSegment3 ls;
     sgdCopyVec3(ls.a, catapults[i].start);
     sgdCopyVec3(ls.b, catapults[i].end);
 
+    sgdSubVec3(pivotoff, ls.a, catapults[i].rotation_pivot);
+    sgdVectorProductVec3(rvel[0], catapults[i].rotation, pivotoff);
+    sgdAddVec3(rvel[0], catapults[i].velocity);
+    sgdSubVec3(pivotoff, ls.b, catapults[i].rotation_pivot);
+    sgdVectorProductVec3(rvel[1], catapults[i].rotation, pivotoff);
+    sgdAddVec3(rvel[1], catapults[i].velocity);
+
     sgdAddVec3(ls.a, cache_center);
     sgdAddVec3(ls.b, cache_center);
 
-    sgdAddScaledVec3(ls.a, catapults[i].velocity, t);
-    sgdAddScaledVec3(ls.b, catapults[i].velocity, t);
+    sgdAddScaledVec3(ls.a, rvel[0], t);
+    sgdAddScaledVec3(ls.b, rvel[1], t);
     
     double this_dist = sgdDistSquaredToLineSegmentVec3( ls, dpt );
     if (this_dist < dist) {
@@ -573,8 +596,8 @@ FGGroundCache::get_cat(double t, const double dpt[3],
       // The carrier code takes care of that ordering.
       sgdCopyVec3( end[0], ls.a );
       sgdCopyVec3( end[1], ls.b );
-      sgdCopyVec3( vel[0], catapults[i].velocity );
-      sgdCopyVec3( vel[1], catapults[i].velocity );
+      sgdCopyVec3( vel[0], rvel[0] );
+      sgdCopyVec3( vel[1], rvel[1] );
     }
   }
 
@@ -583,17 +606,16 @@ FGGroundCache::get_cat(double t, const double dpt[3],
 }
 
 bool
-FGGroundCache::get_agl(double t, const double dpt[3],
-                         double contact[3], double normal[3], double vel[3],
-                         int *type, double *loadCapacity,
-                         double *frictionFactor, double *agl)
+FGGroundCache::get_agl(double t, const double dpt[3], double max_altoff,
+                       double contact[3], double normal[3], double vel[3],
+                       int *type, const SGMaterial** material, double *agl)
 {
   bool ret = false;
 
   *type = FGInterface::Unknown;
 //   *agl = 0.0;
-  *loadCapacity = DBL_MAX;
-  *frictionFactor = 1.0;
+  if (material)
+    *material = 0;
   sgdSetVec3( vel, 0.0, 0.0, 0.0 );
   sgdSetVec3( contact, 0.0, 0.0, 0.0 );
   sgdSetVec3( normal, 0.0, 0.0, 0.0 );
@@ -608,9 +630,10 @@ FGGroundCache::get_agl(double t, const double dpt[3],
   // The search direction
   sgdVec3 dir;
   sgdSetVec3( dir, -dpt[0], -dpt[1], -dpt[2] );
+  sgdNormaliseVec3( dir );
 
   // Initialize to something sensible
-  double sqdist = DBL_MAX;
+  double current_radius = 0.0;
 
   size_t sz = triangles.size();
   for (size_t i = 0; i < sz; ++i) {
@@ -623,28 +646,35 @@ FGGroundCache::get_agl(double t, const double dpt[3],
     sgdVec3 isecpoint;
     if ( sgdIsectInfLinePlane( isecpoint, pt, dir, triangle.plane ) &&
          sgdPointInTriangle( isecpoint, triangle.vertices ) ) {
-
-      // Check for the closest intersection point.
-      // FIXME: is this the right one?
-      SGDfloat newSqdist = sgdDistanceSquaredVec3( isecpoint, pt );
-      if ( newSqdist < sqdist ) {
-        sqdist = newSqdist;
-        ret = true;
-        // Save the new potential intersection point.
-        sgdCopyVec3( contact, isecpoint );
-        sgdAddVec3( contact, cache_center );
-        // The first three values in the vector are the plane normal.
-        sgdCopyVec3( normal, triangle.plane );
-        // The velocity wrt earth.
-        /// FIXME: only true for non rotating objects!!!!
-        sgdCopyVec3( vel, triangle.velocity );
-        // Save the ground type.
-        *type = triangle.type;
-        // FIXME: figure out how to get that sign ...
-//           *agl = sqrt(sqdist);
-        *agl = sgdLengthVec3( dpt ) - sgdLengthVec3( contact );
-//           *loadCapacity = DBL_MAX;
-//           *frictionFactor = 1.0;
+      // Compute the vector from pt to the intersection point ...
+      sgdVec3 off;
+      sgdSubVec3(off, isecpoint, pt);
+      // ... and check if it is too high or not
+      if (-max_altoff < sgdScalarProductVec3( off, dir )) {
+        // Transform to the wgs system
+        sgdAddVec3( isecpoint, cache_center );
+        // compute the radius, good enough approximation to take the geocentric radius
+        SGDfloat radius = sgdLengthSquaredVec3(isecpoint);
+        if (current_radius < radius) {
+          current_radius = radius;
+          ret = true;
+          // Save the new potential intersection point.
+          sgdCopyVec3( contact, isecpoint );
+          // The first three values in the vector are the plane normal.
+          sgdCopyVec3( normal, triangle.plane );
+          // The velocity wrt earth.
+          sgdVec3 pivotoff;
+          sgdSubVec3(pivotoff, pt, triangle.rotation_pivot);
+          sgdVectorProductVec3(vel, triangle.rotation, pivotoff);
+          sgdAddVec3(vel, triangle.velocity);
+          // Save the ground type.
+          *type = triangle.type;
+          sgdVec3 dstToContact;
+          sgdSubVec3(dstToContact, contact, dpt);
+          *agl = sgdScalarProductVec3(dir, dstToContact);
+          if (material)
+            *material = triangle.material;
+        }
       }
     }
   }
@@ -664,10 +694,10 @@ FGGroundCache::get_agl(double t, const double dpt[3],
   
   // The altitude is the distance of the requested point from the
   // contact point.
-  *agl = sgdLengthVec3( dpt ) - sgdLengthVec3( contact );
+  sgdVec3 dstToContact;
+  sgdSubVec3(dstToContact, contact, dpt);
+  *agl = sgdScalarProductVec3(dir, dstToContact);
   *type = FGInterface::Unknown;
-  *loadCapacity = DBL_MAX;
-  *frictionFactor = 1.0;
 
   return ret;
 }
@@ -695,17 +725,18 @@ bool FGGroundCache::caught_wire(double t, const double pt[4][3])
   sgdCopyVec3( tri[1][2], pt[3] );
 
   // Intersect the wire lines with each of these triangles.
-  // You have cautght a wire if they intersect.
+  // You have caught a wire if they intersect.
   for (size_t i = 0; i < sz; ++i) {
     sgdVec3 le[2];
-    sgdCopyVec3(le[0], wires[i].ends[0]);
-    sgdCopyVec3(le[1], wires[i].ends[1]);
-
-    sgdAddVec3(le[0], cache_center);
-    sgdAddVec3(le[1], cache_center);
-
-    sgdAddScaledVec3(le[0], wires[i].velocity, t);
-    sgdAddScaledVec3(le[1], wires[i].velocity, t);
+    for (int k = 0; k < 2; ++k) {
+      sgdVec3 pivotoff, vel;
+      sgdCopyVec3(le[k], wires[i].ends[k]);
+      sgdSubVec3(pivotoff, le[k], wires[i].rotation_pivot);
+      sgdVectorProductVec3(vel, wires[i].rotation, pivotoff);
+      sgdAddVec3(vel, wires[i].velocity);
+      sgdAddScaledVec3(le[k], vel, t);
+      sgdAddVec3(le[k], cache_center);
+    }
     
     for (int k=0; k<2; ++k) {
       sgdVec3 isecpoint;
@@ -736,17 +767,15 @@ bool FGGroundCache::get_wire_ends(double t, double end[2][3], double vel[2][3])
   size_t sz = wires.size();
   for (size_t i = 0; i < sz; ++i) {
     if (wires[i].wire_id == wire_id) {
-      sgdCopyVec3(end[0], wires[i].ends[0]);
-      sgdCopyVec3(end[1], wires[i].ends[1]);
-      
-      sgdAddVec3(end[0], cache_center);
-      sgdAddVec3(end[1], cache_center);
-      
-      sgdAddScaledVec3(end[0], wires[i].velocity, t);
-      sgdAddScaledVec3(end[1], wires[i].velocity, t);
-      
-      sgdCopyVec3(vel[0], wires[i].velocity);
-      sgdCopyVec3(vel[1], wires[i].velocity);
+      for (size_t k = 0; k < 2; ++k) {
+        sgdVec3 pivotoff;
+        sgdCopyVec3(end[k], wires[i].ends[k]);
+        sgdSubVec3(pivotoff, end[k], wires[i].rotation_pivot);
+        sgdVectorProductVec3(vel[k], wires[i].rotation, pivotoff);
+        sgdAddVec3(vel[k], wires[i].velocity);
+        sgdAddScaledVec3(end[k], vel[k], t);
+        sgdAddVec3(end[k], cache_center);
+      }
       return true;
     }
   }