]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/groundcache.cxx
Add a lower-bound type navaid lookup, and the ability to specify navaid type in the...
[flightgear.git] / src / FDM / groundcache.cxx
index 6c2654083a74e13d8812dfebc8974db71849d0f4..edabbf0de34961925ea0ed6a969bd6354114d8fa 100644 (file)
@@ -226,13 +226,15 @@ 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 );
+    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 the texture name and decide what ground type we have.
@@ -285,7 +287,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 +298,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);
       }
@@ -334,7 +340,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 +356,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) {
+          found_ground = true;
+          sgdAddVec3(isectpoint, cache_center);
+          double this_radius = sgdLengthVec3(isectpoint);
+          if (ground_radius < this_radius)
+            ground_radius = this_radius;
+        }
       }
     }
   }
@@ -373,15 +388,25 @@ 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;
 
   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);
   }
@@ -514,7 +539,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 +579,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 +606,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,10 +616,10 @@ 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, double *loadCapacity,
+                       double *frictionFactor, double *agl)
 {
   bool ret = false;
 
@@ -608,9 +641,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 +657,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 ...
+      // 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;
+          // FIXME: figure out how to get that sign ...
 //           *agl = sqrt(sqdist);
-        *agl = sgdLengthVec3( dpt ) - sgdLengthVec3( contact );
+          *agl = sgdLengthVec3( dpt ) - sgdLengthVec3( contact );
 //           *loadCapacity = DBL_MAX;
 //           *frictionFactor = 1.0;
+        }
       }
     }
   }
@@ -698,14 +739,15 @@ bool FGGroundCache::caught_wire(double t, const double pt[4][3])
   // You have cautght 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 +778,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;
     }
   }