SGDfloat tmp = (y2 - y3);
SGDfloat tmpn = (x2 - x3);
int side1 = SG_SIGN (tmp * (rx - x3) + (y3 - ry) * tmpn);
- int side2 = SG_SIGN (tmp * (x1 - x3) + (y3 - side1*eps - y1) * tmpn);
+ int side2 = SG_SIGN (tmp * (x1 - x3) + (y3 - y1) * tmpn
+ + side1 * eps * fabs(tmpn));
if ( side1 != side2 ) {
// printf("failed side 1 check\n");
return false;
tmp = (y3 - ry);
tmpn = (x3 - rx);
side1 = SG_SIGN (tmp * (x2 - rx) + (ry - y2) * tmpn);
- side2 = SG_SIGN (tmp * (x1 - rx) + (ry - side1*eps - y1) * tmpn);
+ side2 = SG_SIGN (tmp * (x1 - rx) + (ry - y1) * tmpn
+ + side1 * eps * fabs(tmpn));
if ( side1 != side2 ) {
// printf("failed side 2 check\n");
return false;
tmp = (y2 - ry);
tmpn = (x2 - rx);
side1 = SG_SIGN (tmp * (x3 - rx) + (ry - y3) * tmpn);
- side2 = SG_SIGN (tmp * (x1 - rx) + (ry - side1*eps - y1) * tmpn);
+ side2 = SG_SIGN (tmp * (x1 - rx) + (ry - y1) * tmpn
+ + side1 * eps * fabs(tmpn));
if ( side1 != side2 ) {
// printf("failed side 3 check\n");
return false;
}
// 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.
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);
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);
}
// 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);
}
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;
+ }
}
}
}
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);
}
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()
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) {
// 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] );
}
}
}
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;
// 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) {
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;
+ }
}
}
}
// 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;
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;
}
}