2004-11-22 10:10:33 +00:00
|
|
|
// groundcache.cxx -- carries a small subset of the scenegraph near the vehicle
|
|
|
|
//
|
|
|
|
// Written by Mathias Froehlich, started Nov 2004.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2004 Mathias Froehlich - Mathias.Froehlich@web.de
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
#include <plib/sg.h>
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
#include <simgear/sg_inlines.h>
|
2004-11-22 10:10:33 +00:00
|
|
|
#include <simgear/constants.h>
|
|
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
#include <simgear/math/sg_geodesy.hxx>
|
|
|
|
|
|
|
|
#include <Main/globals.hxx>
|
|
|
|
#include <Scenery/scenery.hxx>
|
|
|
|
#include <Scenery/tilemgr.hxx>
|
|
|
|
#include <AIModel/AICarrier.hxx>
|
|
|
|
|
|
|
|
#include "flight.hxx"
|
|
|
|
#include "groundcache.hxx"
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
// Specialized version of sgMultMat4 needed because of mixed matrix
|
|
|
|
// types
|
|
|
|
static inline void fgMultMat4(sgdMat4 dst, sgdMat4 m1, sgMat4 m2) {
|
|
|
|
for ( int j = 0 ; j < 4 ; j++ ) {
|
|
|
|
dst[0][j] = m2[0][0] * m1[0][j] +
|
|
|
|
m2[0][1] * m1[1][j] +
|
|
|
|
m2[0][2] * m1[2][j] +
|
|
|
|
m2[0][3] * m1[3][j] ;
|
|
|
|
|
|
|
|
dst[1][j] = m2[1][0] * m1[0][j] +
|
|
|
|
m2[1][1] * m1[1][j] +
|
|
|
|
m2[1][2] * m1[2][j] +
|
|
|
|
m2[1][3] * m1[3][j] ;
|
|
|
|
|
|
|
|
dst[2][j] = m2[2][0] * m1[0][j] +
|
|
|
|
m2[2][1] * m1[1][j] +
|
|
|
|
m2[2][2] * m1[2][j] +
|
|
|
|
m2[2][3] * m1[3][j] ;
|
|
|
|
|
|
|
|
dst[3][j] = m2[3][0] * m1[0][j] +
|
|
|
|
m2[3][1] * m1[1][j] +
|
|
|
|
m2[3][2] * m1[2][j] +
|
|
|
|
m2[3][3] * m1[3][j] ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool fgdPointInTriangle( sgdVec3 point, sgdVec3 tri[3] )
|
|
|
|
{
|
|
|
|
sgdVec3 dif;
|
|
|
|
|
|
|
|
// Some tolerance in meters we accept a point to be outside of the triangle
|
|
|
|
// and still return that it is inside.
|
|
|
|
SGDfloat eps = 1e-2;
|
|
|
|
SGDfloat min, max;
|
|
|
|
// punt if outside bouding cube
|
|
|
|
SG_MIN_MAX3 ( min, max, tri[0][0], tri[1][0], tri[2][0] );
|
|
|
|
if( (point[0] < min - eps) || (point[0] > max + eps) )
|
|
|
|
return false;
|
|
|
|
dif[0] = max - min;
|
|
|
|
|
|
|
|
SG_MIN_MAX3 ( min, max, tri[0][1], tri[1][1], tri[2][1] );
|
|
|
|
if( (point[1] < min - eps) || (point[1] > max + eps) )
|
|
|
|
return false;
|
|
|
|
dif[1] = max - min;
|
|
|
|
|
|
|
|
SG_MIN_MAX3 ( min, max, tri[0][2], tri[1][2], tri[2][2] );
|
|
|
|
if( (point[2] < min - eps) || (point[2] > max + eps) )
|
|
|
|
return false;
|
|
|
|
dif[2] = max - min;
|
|
|
|
|
|
|
|
// drop the smallest dimension so we only have to work in 2d.
|
|
|
|
SGDfloat min_dim = SG_MIN3 (dif[0], dif[1], dif[2]);
|
|
|
|
SGDfloat x1, y1, x2, y2, x3, y3, rx, ry;
|
|
|
|
if ( fabs(min_dim-dif[0]) <= DBL_EPSILON ) {
|
|
|
|
// x is the smallest dimension
|
|
|
|
x1 = point[1];
|
|
|
|
y1 = point[2];
|
|
|
|
x2 = tri[0][1];
|
|
|
|
y2 = tri[0][2];
|
|
|
|
x3 = tri[1][1];
|
|
|
|
y3 = tri[1][2];
|
|
|
|
rx = tri[2][1];
|
|
|
|
ry = tri[2][2];
|
|
|
|
} else if ( fabs(min_dim-dif[1]) <= DBL_EPSILON ) {
|
|
|
|
// y is the smallest dimension
|
|
|
|
x1 = point[0];
|
|
|
|
y1 = point[2];
|
|
|
|
x2 = tri[0][0];
|
|
|
|
y2 = tri[0][2];
|
|
|
|
x3 = tri[1][0];
|
|
|
|
y3 = tri[1][2];
|
|
|
|
rx = tri[2][0];
|
|
|
|
ry = tri[2][2];
|
|
|
|
} else if ( fabs(min_dim-dif[2]) <= DBL_EPSILON ) {
|
|
|
|
// z is the smallest dimension
|
|
|
|
x1 = point[0];
|
|
|
|
y1 = point[1];
|
|
|
|
x2 = tri[0][0];
|
|
|
|
y2 = tri[0][1];
|
|
|
|
x3 = tri[1][0];
|
|
|
|
y3 = tri[1][1];
|
|
|
|
rx = tri[2][0];
|
|
|
|
ry = tri[2][1];
|
|
|
|
} else {
|
|
|
|
// all dimensions are really small so lets call it close
|
|
|
|
// enough and return a successful match
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if intersection point is on the same side of p1 <-> p2 as p3
|
|
|
|
SGDfloat tmp = (y2 - y3);
|
|
|
|
SGDfloat tmpn = (x2 - x3);
|
|
|
|
int side1 = SG_SIGN (tmp * (rx - x3) + (y3 - ry) * tmpn);
|
2005-06-02 08:51:47 +00:00
|
|
|
int side2 = SG_SIGN (tmp * (x1 - x3) + (y3 - y1) * tmpn
|
|
|
|
+ side1 * eps * fabs(tmpn));
|
2005-05-30 08:48:27 +00:00
|
|
|
if ( side1 != side2 ) {
|
|
|
|
// printf("failed side 1 check\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if intersection point is on correct side of p2 <-> p3 as p1
|
|
|
|
tmp = (y3 - ry);
|
|
|
|
tmpn = (x3 - rx);
|
|
|
|
side1 = SG_SIGN (tmp * (x2 - rx) + (ry - y2) * tmpn);
|
2005-06-02 08:51:47 +00:00
|
|
|
side2 = SG_SIGN (tmp * (x1 - rx) + (ry - y1) * tmpn
|
|
|
|
+ side1 * eps * fabs(tmpn));
|
2005-05-30 08:48:27 +00:00
|
|
|
if ( side1 != side2 ) {
|
|
|
|
// printf("failed side 2 check\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if intersection point is on correct side of p1 <-> p3 as p2
|
|
|
|
tmp = (y2 - ry);
|
|
|
|
tmpn = (x2 - rx);
|
|
|
|
side1 = SG_SIGN (tmp * (x3 - rx) + (ry - y3) * tmpn);
|
2005-06-02 08:51:47 +00:00
|
|
|
side2 = SG_SIGN (tmp * (x1 - rx) + (ry - y1) * tmpn
|
|
|
|
+ side1 * eps * fabs(tmpn));
|
2005-05-30 08:48:27 +00:00
|
|
|
if ( side1 != side2 ) {
|
|
|
|
// printf("failed side 3 check\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test if the line given by the point on the line pt_on_line and the
|
|
|
|
// line direction dir intersects the sphere sp.
|
|
|
|
// Adapted from plib.
|
|
|
|
static inline bool
|
|
|
|
fgdIsectSphereInfLine(const sgdSphere& sp,
|
|
|
|
const sgdVec3 pt_on_line, const sgdVec3 dir)
|
|
|
|
{
|
|
|
|
sgdVec3 r;
|
|
|
|
sgdSubVec3( r, sp.getCenter(), pt_on_line ) ;
|
|
|
|
|
|
|
|
SGDfloat projectedDistance = sgdScalarProductVec3(r, dir);
|
|
|
|
|
|
|
|
SGDfloat dist = sgdScalarProductVec3 ( r, r ) -
|
|
|
|
projectedDistance * projectedDistance;
|
|
|
|
|
|
|
|
SGDfloat radius = sp.getRadius();
|
|
|
|
return dist < radius*radius;
|
|
|
|
}
|
|
|
|
|
2004-12-08 14:48:06 +00:00
|
|
|
FGGroundCache::FGGroundCache()
|
|
|
|
{
|
|
|
|
sgdSetVec3(cache_center, 0.0, 0.0, 0.0);
|
|
|
|
ground_radius = 0.0;
|
|
|
|
cache_ref_time = 0.0;
|
|
|
|
wire_id = 0;
|
|
|
|
sgdSetVec3(reference_wgs84_point, 0.0, 0.0, 0.0);
|
|
|
|
reference_vehicle_radius = 0.0;
|
|
|
|
found_ground = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FGGroundCache::~FGGroundCache()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
FGGroundCache::GroundProperty
|
2004-11-22 10:10:33 +00:00
|
|
|
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,
|
|
|
|
// from property tree or whatever ...
|
|
|
|
|
|
|
|
// Get ground dependent data.
|
2005-05-30 08:48:27 +00:00
|
|
|
GroundProperty gp;
|
|
|
|
gp.wire_id = -1;
|
2004-11-22 10:10:33 +00:00
|
|
|
|
2004-11-26 10:24:48 +00:00
|
|
|
FGAICarrierHardware *ud =
|
|
|
|
dynamic_cast<FGAICarrierHardware*>(l->getUserData());
|
|
|
|
if (ud) {
|
|
|
|
switch (ud->type) {
|
|
|
|
case FGAICarrierHardware::Wire:
|
2005-05-30 08:48:27 +00:00
|
|
|
gp.type = FGInterface::Wire;
|
|
|
|
gp.wire_id = ud->id;
|
2004-11-26 10:24:48 +00:00
|
|
|
break;
|
|
|
|
case FGAICarrierHardware::Catapult:
|
2005-05-30 08:48:27 +00:00
|
|
|
gp.type = FGInterface::Catapult;
|
2004-11-26 10:24:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2005-05-30 08:48:27 +00:00
|
|
|
gp.type = FGInterface::Solid;
|
2004-11-26 10:24:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-11-22 10:10:33 +00:00
|
|
|
|
2004-11-26 10:24:48 +00:00
|
|
|
// Copy the velocity from the carrier class.
|
2005-08-16 09:37:23 +00:00
|
|
|
ud->carrier->getVelocityWrtEarth( gp.vel, gp.rot, gp.pivot );
|
2004-11-26 10:24:48 +00:00
|
|
|
}
|
2004-11-22 10:10:33 +00:00
|
|
|
|
2004-11-26 10:24:48 +00:00
|
|
|
else {
|
2004-11-22 10:10:33 +00:00
|
|
|
|
|
|
|
// Initialize velocity field.
|
2005-08-16 09:37:23 +00:00
|
|
|
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 );
|
2004-11-26 10:24:48 +00:00
|
|
|
}
|
2004-11-22 10:10:33 +00:00
|
|
|
|
|
|
|
// 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")
|
2005-05-30 08:48:27 +00:00
|
|
|
gp.type = FGInterface::Solid;
|
2004-11-22 10:10:33 +00:00
|
|
|
else {
|
|
|
|
if (file == "asphault.rgb" || file == "airport.rgb")
|
2005-05-30 08:48:27 +00:00
|
|
|
gp.type = FGInterface::Solid;
|
2004-11-22 10:10:33 +00:00
|
|
|
else if (file == "water.rgb" || file == "water-lake.rgb")
|
2005-05-30 08:48:27 +00:00
|
|
|
gp.type = FGInterface::Water;
|
2004-11-22 10:10:33 +00:00
|
|
|
else if (file == "forest.rgb" || file == "cropwood.rgb")
|
2005-05-30 08:48:27 +00:00
|
|
|
gp.type = FGInterface::Forest;
|
2004-11-22 10:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-05-30 08:48:27 +00:00
|
|
|
FGGroundCache::putLineLeafIntoCache(const sgdSphere *wsp, const sgdMat4 xform,
|
2004-11-22 10:10:33 +00:00
|
|
|
ssgLeaf *l)
|
|
|
|
{
|
2005-05-30 08:48:27 +00:00
|
|
|
GroundProperty gp = extractGroundProperty(l);
|
|
|
|
|
2004-11-22 10:10:33 +00:00
|
|
|
// Lines must have special meanings.
|
|
|
|
// Wires and catapults are done with lines.
|
|
|
|
int nl = l->getNumLines();
|
|
|
|
for (int i = 0; i < nl; ++i) {
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdSphere sphere;
|
|
|
|
sphere.empty();
|
|
|
|
sgdVec3 ends[2];
|
2004-11-22 10:10:33 +00:00
|
|
|
short v[2];
|
|
|
|
l->getLine(i, v, v+1 );
|
2005-05-30 08:48:27 +00:00
|
|
|
for (int k=0; k<2; ++k) {
|
|
|
|
sgdSetVec3(ends[k], l->getVertex(v[k]));
|
|
|
|
sgdXformPnt3(ends[k], xform);
|
|
|
|
sphere.extend(ends[k]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wsp->intersects( &sphere )) {
|
|
|
|
if (gp.type == FGInterface::Wire) {
|
|
|
|
Wire wire;
|
|
|
|
sgdCopyVec3(wire.ends[0], ends[0]);
|
|
|
|
sgdCopyVec3(wire.ends[1], ends[1]);
|
2005-08-16 09:37:23 +00:00
|
|
|
sgdCopyVec3(wire.velocity, gp.vel);
|
|
|
|
sgdCopyVec3(wire.rotation, gp.rot);
|
|
|
|
sgdSubVec3(wire.rotation_pivot, gp.pivot, cache_center);
|
2005-05-30 08:48:27 +00:00
|
|
|
wire.wire_id = gp.wire_id;
|
|
|
|
|
|
|
|
wires.push_back(wire);
|
|
|
|
}
|
|
|
|
if (gp.type == FGInterface::Catapult) {
|
|
|
|
Catapult cat;
|
|
|
|
sgdCopyVec3(cat.start, ends[0]);
|
|
|
|
sgdCopyVec3(cat.end, ends[1]);
|
2005-08-16 09:37:23 +00:00
|
|
|
sgdCopyVec3(cat.velocity, gp.vel);
|
|
|
|
sgdCopyVec3(cat.rotation, gp.rot);
|
|
|
|
sgdSubVec3(cat.rotation_pivot, gp.pivot, cache_center);
|
2005-05-30 08:48:27 +00:00
|
|
|
|
|
|
|
catapults.push_back(cat);
|
|
|
|
}
|
2004-11-22 10:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-05-30 08:48:27 +00:00
|
|
|
FGGroundCache::putSurfaceLeafIntoCache(const sgdSphere *sp,
|
|
|
|
const sgdMat4 xform, bool sphIsec,
|
|
|
|
sgdVec3 down, ssgLeaf *l)
|
2004-11-22 10:10:33 +00:00
|
|
|
{
|
2005-05-30 08:48:27 +00:00
|
|
|
GroundProperty gp = extractGroundProperty(l);
|
|
|
|
|
2004-11-22 10:10:33 +00:00
|
|
|
int nt = l->getNumTriangles();
|
|
|
|
for (int i = 0; i < nt; ++i) {
|
2005-05-30 08:48:27 +00:00
|
|
|
Triangle t;
|
|
|
|
t.sphere.empty();
|
2004-11-22 10:10:33 +00:00
|
|
|
short v[3];
|
2005-05-30 08:48:27 +00:00
|
|
|
l->getTriangle(i, &v[0], &v[1], &v[2]);
|
|
|
|
for (int k = 0; k < 3; ++k) {
|
|
|
|
sgdSetVec3(t.vertices[k], l->getVertex(v[k]));
|
|
|
|
sgdXformPnt3(t.vertices[k], xform);
|
|
|
|
t.sphere.extend(t.vertices[k]);
|
|
|
|
}
|
|
|
|
|
|
|
|
sgdMakePlane(t.plane, t.vertices[0], t.vertices[1], t.vertices[2]);
|
|
|
|
SGDfloat dot = sgdScalarProductVec3(down, t.plane);
|
|
|
|
if (dot > 0) {
|
|
|
|
if (!l->getCullFace()) {
|
|
|
|
// Surface points downwards, ignore for altitude computations.
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
sgdScaleVec4( t.plane, -1 );
|
|
|
|
}
|
2004-11-22 10:10:33 +00:00
|
|
|
|
|
|
|
// Check if the sphere around the vehicle intersects the sphere
|
|
|
|
// around that triangle. If so, put that triangle into the cache.
|
2005-05-30 08:48:27 +00:00
|
|
|
if (sphIsec && sp->intersects(&t.sphere)) {
|
2005-08-16 09:37:23 +00:00
|
|
|
sgdCopyVec3(t.velocity, gp.vel);
|
|
|
|
sgdCopyVec3(t.rotation, gp.rot);
|
|
|
|
sgdSubVec3(t.rotation_pivot, gp.pivot, cache_center);
|
2005-05-30 08:48:27 +00:00
|
|
|
t.type = gp.type;
|
|
|
|
triangles.push_back(t);
|
2004-11-22 10:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// In case the cache is empty, we still provide agl computations.
|
|
|
|
// But then we use the old way of having a fixed elevation value for
|
|
|
|
// the whole lifetime of this cache.
|
2005-05-30 08:48:27 +00:00
|
|
|
if ( fgdIsectSphereInfLine(t.sphere, sp->getCenter(), down) ) {
|
|
|
|
sgdVec3 tmp;
|
|
|
|
sgdSetVec3(tmp, sp->center[0], sp->center[1], sp->center[2]);
|
|
|
|
sgdVec3 isectpoint;
|
|
|
|
if ( sgdIsectInfLinePlane( isectpoint, tmp, down, t.plane ) &&
|
|
|
|
fgdPointInTriangle( isectpoint, t.vertices ) ) {
|
2005-11-04 14:49:15 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2004-11-22 10:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
inline void
|
|
|
|
FGGroundCache::velocityTransformTriangle(double dt,
|
|
|
|
FGGroundCache::Triangle& dst,
|
|
|
|
const FGGroundCache::Triangle& src)
|
2004-11-22 10:10:33 +00:00
|
|
|
{
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdCopyVec3(dst.vertices[0], src.vertices[0]);
|
|
|
|
sgdCopyVec3(dst.vertices[1], src.vertices[1]);
|
|
|
|
sgdCopyVec3(dst.vertices[2], src.vertices[2]);
|
2004-11-22 10:10:33 +00:00
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdCopyVec4(dst.plane, src.plane);
|
|
|
|
|
|
|
|
sgdCopyVec3(dst.sphere.center, src.sphere.center);
|
|
|
|
dst.sphere.radius = src.sphere.radius;
|
2004-11-22 10:10:33 +00:00
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdCopyVec3(dst.velocity, src.velocity);
|
2005-08-16 09:37:23 +00:00
|
|
|
sgdCopyVec3(dst.rotation, src.rotation);
|
|
|
|
sgdCopyVec3(dst.rotation_pivot, src.rotation_pivot);
|
2004-11-22 10:10:33 +00:00
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
dst.type = src.type;
|
|
|
|
|
|
|
|
if (dt*sgdLengthSquaredVec3(src.velocity) != 0) {
|
2005-08-16 09:37:23 +00:00
|
|
|
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);
|
|
|
|
}
|
2005-05-30 08:48:27 +00:00
|
|
|
|
2005-08-16 09:37:23 +00:00
|
|
|
// 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);
|
2005-05-30 08:48:27 +00:00
|
|
|
|
|
|
|
sgdAddScaledVec3(dst.sphere.center, src.velocity, dt);
|
|
|
|
}
|
|
|
|
}
|
2004-11-22 10:10:33 +00:00
|
|
|
|
|
|
|
void
|
2005-05-30 08:48:27 +00:00
|
|
|
FGGroundCache::cache_fill(ssgBranch *branch, sgdMat4 xform,
|
|
|
|
sgdSphere* sp, sgdVec3 down, sgdSphere* wsp)
|
2004-11-22 10:10:33 +00:00
|
|
|
{
|
|
|
|
// Travel through all kids.
|
|
|
|
ssgEntity *e;
|
|
|
|
for ( e = branch->getKid(0); e != NULL ; e = branch->getNextKid() ) {
|
2005-05-30 08:48:27 +00:00
|
|
|
if ( !(e->getTraversalMask() & SSGTRAV_HOT) )
|
2004-11-22 10:10:33 +00:00
|
|
|
continue;
|
|
|
|
if ( e->getBSphere()->isEmpty() )
|
|
|
|
continue;
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
// We need to check further if either the sphere around the branch
|
2004-11-22 10:10:33 +00:00
|
|
|
// intersects the sphere around the aircraft or the line downwards from
|
|
|
|
// the aircraft intersects the branchs sphere.
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdSphere esphere;
|
|
|
|
sgdSetVec3(esphere.center, e->getBSphere()->center);
|
|
|
|
esphere.radius = e->getBSphere()->radius;
|
2004-11-22 10:10:33 +00:00
|
|
|
esphere.orthoXform(xform);
|
|
|
|
bool wspIsec = wsp->intersects(&esphere);
|
2005-05-30 08:48:27 +00:00
|
|
|
bool downIsec = fgdIsectSphereInfLine(esphere, sp->getCenter(), down);
|
2004-11-22 10:10:33 +00:00
|
|
|
if (!wspIsec && !downIsec)
|
|
|
|
continue;
|
2005-05-30 08:48:27 +00:00
|
|
|
|
2004-11-22 10:10:33 +00:00
|
|
|
// For branches collect up the transforms to reach that branch and
|
|
|
|
// call cache_fill recursively.
|
|
|
|
if ( e->isAKindOf( ssgTypeBranch() ) ) {
|
|
|
|
ssgBranch *b = (ssgBranch *)e;
|
|
|
|
if ( b->isAKindOf( ssgTypeTransform() ) ) {
|
|
|
|
// Collect up the transfors required to reach that part of
|
|
|
|
// the branch.
|
|
|
|
sgMat4 xform2;
|
|
|
|
sgMakeIdentMat4( xform2 );
|
|
|
|
ssgTransform *t = (ssgTransform*)b;
|
|
|
|
t->getTransform( xform2 );
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdMat4 xform3;
|
|
|
|
fgMultMat4(xform3, xform, xform2);
|
|
|
|
cache_fill( b, xform3, sp, down, wsp );
|
2004-11-22 10:10:33 +00:00
|
|
|
} else
|
|
|
|
cache_fill( b, xform, sp, down, wsp );
|
|
|
|
}
|
|
|
|
|
|
|
|
// For leafs, check each triangle for intersection.
|
|
|
|
// 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
|
|
|
|
// 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
|
|
|
|
// by following the line downwards originating at the aircraft.
|
|
|
|
bool spIsec = sp->intersects(&esphere);
|
|
|
|
putSurfaceLeafIntoCache(sp, xform, spIsec, down, (ssgLeaf *)e);
|
|
|
|
|
|
|
|
// If we are here, we need to put all special hardware here into
|
|
|
|
// the cache.
|
|
|
|
if (wspIsec)
|
|
|
|
putLineLeafIntoCache(wsp, xform, (ssgLeaf *)e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FGGroundCache::prepare_ground_cache(double ref_time, const double pt[3],
|
2005-05-30 08:48:27 +00:00
|
|
|
double rad)
|
2004-11-22 10:10:33 +00:00
|
|
|
{
|
|
|
|
// Empty cache.
|
|
|
|
ground_radius = 0.0;
|
|
|
|
found_ground = false;
|
2005-05-30 08:48:27 +00:00
|
|
|
triangles.resize(0);
|
|
|
|
catapults.resize(0);
|
|
|
|
wires.resize(0);
|
2004-11-22 10:10:33 +00:00
|
|
|
|
|
|
|
// Store the parameters we used to build up that cache.
|
|
|
|
sgdCopyVec3(reference_wgs84_point, pt);
|
|
|
|
reference_vehicle_radius = rad;
|
|
|
|
// Store the time reference used to compute movements of moving triangles.
|
|
|
|
cache_ref_time = ref_time;
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
// 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.
|
|
|
|
if (40*40 < old_cntr.distance3Dsquared(cntr))
|
|
|
|
globals->get_scenery()->set_center(cntr);
|
|
|
|
else
|
|
|
|
cntr = old_cntr;
|
|
|
|
|
2004-11-22 10:10:33 +00:00
|
|
|
// The center of the cache.
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdSetVec3(cache_center, cntr[0], cntr[1], cntr[2]);
|
2004-11-22 10:10:33 +00:00
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdVec3 ptoff;
|
|
|
|
sgdSubVec3(ptoff, pt, cache_center);
|
2004-11-22 10:10:33 +00:00
|
|
|
// Prepare sphere around the aircraft.
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdSphere acSphere;
|
2004-11-22 10:10:33 +00:00
|
|
|
acSphere.setRadius(rad);
|
2005-05-30 08:48:27 +00:00
|
|
|
acSphere.setCenter(ptoff);
|
2004-11-22 10:10:33 +00:00
|
|
|
|
|
|
|
// Prepare bigger sphere around the aircraft.
|
|
|
|
// This one is required for reliably finding wires we have caught but
|
|
|
|
// have already left the hopefully smaller sphere for the ground reactions.
|
|
|
|
const double max_wire_dist = 300.0;
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdSphere wireSphere;
|
2005-04-19 13:21:43 +00:00
|
|
|
wireSphere.setRadius(max_wire_dist < rad ? rad : max_wire_dist);
|
2005-05-30 08:48:27 +00:00
|
|
|
wireSphere.setCenter(ptoff);
|
2004-11-22 10:10:33 +00:00
|
|
|
|
|
|
|
// Down vector. Is used for croase agl computations when we are far enough
|
|
|
|
// from ground that we have an empty cache.
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdVec3 down;
|
|
|
|
sgdSetVec3(down, -pt[0], -pt[1], -pt[2]);
|
|
|
|
sgdNormalizeVec3(down);
|
2005-04-19 13:21:43 +00:00
|
|
|
|
2004-11-22 10:10:33 +00:00
|
|
|
// We collaps 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 ...)
|
|
|
|
// we will later store a speed in the GroundType class. We can then apply
|
|
|
|
// some translations to that nodes according to the time which has passed
|
|
|
|
// compared to that snapshot.
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdMat4 xform;
|
|
|
|
sgdMakeIdentMat4( xform );
|
2005-04-19 13:21:43 +00:00
|
|
|
|
2004-11-22 10:10:33 +00:00
|
|
|
|
Mathias:
I have done a patch to eliminate the jitter of 3D-objects near the viewpoint
(for example 3D cockpit objects).
The problem is the roundoff accuracy of the float values used in the
scenegraph together with the transforms of the eyepoint relative to the
scenery center.
The solution will be to move the scenery center near the view point.
This way floats relative accuracy is enough to show a stable picture.
To get that right I have introduced a transform node for the scenegraph which
is responsible for that shift and uses double values as long as possible.
The scenery subsystem now has a list of all those transforms required to place
objects in the world and will tell all those transforms that the scenery
center has changed when the set_scenery_center() of the scenery subsystem is
called.
The problem was not solvable by SGModelPlacement and SGLocation, since not all
objects, especially the scenery, are placed using these classes.
The first approach was to have the scenery center exactly at the eyepoint.
This works well for the cockpit.
But then the ground jitters a bit below the aircraft. With our default views
you can't see that, but that F-18 has a camera view below the left engine
intake with the nose gear and the ground in its field of view, here I could
see that.
Having the scenery center constant will still have this roundoff problems, but
like it is now too, the roundoff error here is exactly the same in each
frame, so you will not notice any jitter.
The real solution is now to keep the scenery center constant as long as it is
in a ball of 30m radius around the view point. If the scenery center is
outside this ball, just put it at the view point.
As a sideeffect of now beeing able to switch the scenery center in the whole
scenegraph with one function call, I was able to remove a one half of a
problem when switching views, where the scenery center was far off for one or
two frames past switching from one view to the next. Also included is a fix
to the other half of this problem, where the view position was not yet copied
into a view when it is switched (at least under glut). This was responsible
for the 'Error: ...' messages of the cloud subsystem when views were
switched.
2005-04-29 14:38:24 +00:00
|
|
|
// Walk the scene graph and extract solid ground triangles and carrier data.
|
2004-11-22 10:10:33 +00:00
|
|
|
ssgBranch *terrain = globals->get_scenery()->get_scene_graph();
|
|
|
|
cache_fill(terrain, xform, &acSphere, down, &wireSphere);
|
|
|
|
|
|
|
|
// some stats
|
2005-10-22 11:26:58 +00:00
|
|
|
SG_LOG(SG_FLIGHT,SG_DEBUG, "prepare_ground_cache(): ac radius = " << rad
|
2005-05-30 08:48:27 +00:00
|
|
|
<< ", # triangles = " << triangles.size()
|
|
|
|
<< ", # wires = " << wires.size()
|
|
|
|
<< ", # catapults = " << catapults.size()
|
2004-11-22 10:10:33 +00:00
|
|
|
<< ", ground_radius = " << ground_radius );
|
|
|
|
|
|
|
|
// If the ground radius is still below 5e6 meters, then we do not yet have
|
|
|
|
// any scenery.
|
|
|
|
found_ground = found_ground && 5e6 < ground_radius;
|
|
|
|
if (!found_ground)
|
|
|
|
SG_LOG(SG_FLIGHT, SG_WARN, "prepare_ground_cache(): trying to build cache "
|
|
|
|
"without any scenery below the aircraft" );
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
if (cntr != old_cntr)
|
|
|
|
globals->get_scenery()->set_center(old_cntr);
|
Mathias:
I have done a patch to eliminate the jitter of 3D-objects near the viewpoint
(for example 3D cockpit objects).
The problem is the roundoff accuracy of the float values used in the
scenegraph together with the transforms of the eyepoint relative to the
scenery center.
The solution will be to move the scenery center near the view point.
This way floats relative accuracy is enough to show a stable picture.
To get that right I have introduced a transform node for the scenegraph which
is responsible for that shift and uses double values as long as possible.
The scenery subsystem now has a list of all those transforms required to place
objects in the world and will tell all those transforms that the scenery
center has changed when the set_scenery_center() of the scenery subsystem is
called.
The problem was not solvable by SGModelPlacement and SGLocation, since not all
objects, especially the scenery, are placed using these classes.
The first approach was to have the scenery center exactly at the eyepoint.
This works well for the cockpit.
But then the ground jitters a bit below the aircraft. With our default views
you can't see that, but that F-18 has a camera view below the left engine
intake with the nose gear and the ground in its field of view, here I could
see that.
Having the scenery center constant will still have this roundoff problems, but
like it is now too, the roundoff error here is exactly the same in each
frame, so you will not notice any jitter.
The real solution is now to keep the scenery center constant as long as it is
in a ball of 30m radius around the view point. If the scenery center is
outside this ball, just put it at the view point.
As a sideeffect of now beeing able to switch the scenery center in the whole
scenegraph with one function call, I was able to remove a one half of a
problem when switching views, where the scenery center was far off for one or
two frames past switching from one view to the next. Also included is a fix
to the other half of this problem, where the view position was not yet copied
into a view when it is switched (at least under glut). This was responsible
for the 'Error: ...' messages of the cloud subsystem when views were
switched.
2005-04-29 14:38:24 +00:00
|
|
|
|
2004-11-22 10:10:33 +00:00
|
|
|
return found_ground;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FGGroundCache::is_valid(double *ref_time, double pt[3], double *rad)
|
|
|
|
{
|
|
|
|
sgdCopyVec3(pt, reference_wgs84_point);
|
|
|
|
*rad = reference_vehicle_radius;
|
|
|
|
*ref_time = cache_ref_time;
|
|
|
|
return found_ground;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
FGGroundCache::get_cat(double t, const double dpt[3],
|
2005-05-30 08:48:27 +00:00
|
|
|
double end[2][3], double vel[2][3])
|
2004-11-22 10:10:33 +00:00
|
|
|
{
|
|
|
|
// start with a distance of 1e10 meters...
|
|
|
|
double dist = 1e10;
|
|
|
|
|
|
|
|
// Time difference to the reference time.
|
|
|
|
t -= cache_ref_time;
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
size_t sz = catapults.size();
|
|
|
|
for (size_t i = 0; i < sz; ++i) {
|
2005-08-16 09:37:23 +00:00
|
|
|
sgdVec3 pivotoff, rvel[2];
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdLineSegment3 ls;
|
|
|
|
sgdCopyVec3(ls.a, catapults[i].start);
|
|
|
|
sgdCopyVec3(ls.b, catapults[i].end);
|
2004-11-22 10:10:33 +00:00
|
|
|
|
2005-08-16 09:37:23 +00:00
|
|
|
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);
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdAddVec3(ls.a, cache_center);
|
|
|
|
sgdAddVec3(ls.b, cache_center);
|
|
|
|
|
2005-08-16 09:37:23 +00:00
|
|
|
sgdAddScaledVec3(ls.a, rvel[0], t);
|
|
|
|
sgdAddScaledVec3(ls.b, rvel[1], t);
|
2005-05-30 08:48:27 +00:00
|
|
|
|
|
|
|
double this_dist = sgdDistSquaredToLineSegmentVec3( ls, dpt );
|
|
|
|
if (this_dist < dist) {
|
|
|
|
SG_LOG(SG_FLIGHT,SG_INFO, "Found catapult "
|
|
|
|
<< this_dist << " meters away");
|
|
|
|
dist = this_dist;
|
2004-11-22 10:10:33 +00:00
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
// The carrier code takes care of that ordering.
|
|
|
|
sgdCopyVec3( end[0], ls.a );
|
|
|
|
sgdCopyVec3( end[1], ls.b );
|
2005-08-16 09:37:23 +00:00
|
|
|
sgdCopyVec3( vel[0], rvel[0] );
|
|
|
|
sgdCopyVec3( vel[1], rvel[1] );
|
2004-11-22 10:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At the end take the root, we only computed squared distances ...
|
|
|
|
return sqrt(dist);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
Mathias Fröhlich:
I have introduced the posibility to start directly on the carrier.
With that patch you will have a --carrrier=id argument where id can either be
the pennant number configured in the nimitz scenario or the carriers name
also configured in the carriers scenario.
Additionaly you can use --parkpos=id to select different positions on the
carrier. They are also configured in the scenario file.
That includes the switch of the whole FGInterface class to make use of the
groundcache.
That means that an aircraft no longer uses the current elevation value from
the scenery class. It rather has its own local cache of the aircrafts
environment which is setup in the common_init method of FGInterface and
updated either manually by calling
FGInterface::get_groundlevel_m(lat, lon, alt_m);
or implicitly by calling the above method in the
FGInterface::_updateGeo*Position(lat, lon, alt);
methods.
A call get_groundlevel_m rebuilds the groundcache if the request is outside
the range of the cache.
Note that for the real usage of the groundcache including the correct
information about the movement of objects and the velocity information, you
still need to set up the groundcache in the usual way like YASim and JSBSim
currently does.
If you use the native interface, you will get only static objects correctly.
But for FDM's only using one single ground level for a whole step this is IMO
sufficient.
The AIManager gets a way to return the location of a object which is placed
wrt an AI Object. At the moment it only honours AICarriers for that.
That method is a static one, which loads the scenario file for that reason and
throws it away afterwards. This looked like the aprioriate way, because the
AIManager is initialized much later in flightgears bootstrap, and I did not
find an easy way to reorder that for my needs. Since this additional load is
very small and does only happen if such a relative location is required, I
think that this is ok.
Note that moving on the carrier will only work correctly for JSBSim and YASim,
but you should now be able to start and move on every not itself moving
object with any FDM.
2005-07-03 09:39:14 +00:00
|
|
|
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)
|
2004-11-22 10:10:33 +00:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
*type = FGInterface::Unknown;
|
|
|
|
// *agl = 0.0;
|
|
|
|
*loadCapacity = DBL_MAX;
|
|
|
|
*frictionFactor = 1.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 );
|
|
|
|
|
|
|
|
// Time difference to th reference time.
|
|
|
|
t -= cache_ref_time;
|
|
|
|
|
|
|
|
// The double valued point we start to search for intersection.
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdVec3 pt;
|
|
|
|
sgdSubVec3( pt, dpt, cache_center );
|
2004-11-22 10:10:33 +00:00
|
|
|
|
|
|
|
// The search direction
|
2005-05-30 08:48:27 +00:00
|
|
|
sgdVec3 dir;
|
|
|
|
sgdSetVec3( dir, -dpt[0], -dpt[1], -dpt[2] );
|
Mathias Fröhlich:
I have introduced the posibility to start directly on the carrier.
With that patch you will have a --carrrier=id argument where id can either be
the pennant number configured in the nimitz scenario or the carriers name
also configured in the carriers scenario.
Additionaly you can use --parkpos=id to select different positions on the
carrier. They are also configured in the scenario file.
That includes the switch of the whole FGInterface class to make use of the
groundcache.
That means that an aircraft no longer uses the current elevation value from
the scenery class. It rather has its own local cache of the aircrafts
environment which is setup in the common_init method of FGInterface and
updated either manually by calling
FGInterface::get_groundlevel_m(lat, lon, alt_m);
or implicitly by calling the above method in the
FGInterface::_updateGeo*Position(lat, lon, alt);
methods.
A call get_groundlevel_m rebuilds the groundcache if the request is outside
the range of the cache.
Note that for the real usage of the groundcache including the correct
information about the movement of objects and the velocity information, you
still need to set up the groundcache in the usual way like YASim and JSBSim
currently does.
If you use the native interface, you will get only static objects correctly.
But for FDM's only using one single ground level for a whole step this is IMO
sufficient.
The AIManager gets a way to return the location of a object which is placed
wrt an AI Object. At the moment it only honours AICarriers for that.
That method is a static one, which loads the scenario file for that reason and
throws it away afterwards. This looked like the aprioriate way, because the
AIManager is initialized much later in flightgears bootstrap, and I did not
find an easy way to reorder that for my needs. Since this additional load is
very small and does only happen if such a relative location is required, I
think that this is ok.
Note that moving on the carrier will only work correctly for JSBSim and YASim,
but you should now be able to start and move on every not itself moving
object with any FDM.
2005-07-03 09:39:14 +00:00
|
|
|
sgdNormaliseVec3( dir );
|
2004-11-22 10:10:33 +00:00
|
|
|
|
|
|
|
// Initialize to something sensible
|
Mathias Fröhlich:
I have introduced the posibility to start directly on the carrier.
With that patch you will have a --carrrier=id argument where id can either be
the pennant number configured in the nimitz scenario or the carriers name
also configured in the carriers scenario.
Additionaly you can use --parkpos=id to select different positions on the
carrier. They are also configured in the scenario file.
That includes the switch of the whole FGInterface class to make use of the
groundcache.
That means that an aircraft no longer uses the current elevation value from
the scenery class. It rather has its own local cache of the aircrafts
environment which is setup in the common_init method of FGInterface and
updated either manually by calling
FGInterface::get_groundlevel_m(lat, lon, alt_m);
or implicitly by calling the above method in the
FGInterface::_updateGeo*Position(lat, lon, alt);
methods.
A call get_groundlevel_m rebuilds the groundcache if the request is outside
the range of the cache.
Note that for the real usage of the groundcache including the correct
information about the movement of objects and the velocity information, you
still need to set up the groundcache in the usual way like YASim and JSBSim
currently does.
If you use the native interface, you will get only static objects correctly.
But for FDM's only using one single ground level for a whole step this is IMO
sufficient.
The AIManager gets a way to return the location of a object which is placed
wrt an AI Object. At the moment it only honours AICarriers for that.
That method is a static one, which loads the scenario file for that reason and
throws it away afterwards. This looked like the aprioriate way, because the
AIManager is initialized much later in flightgears bootstrap, and I did not
find an easy way to reorder that for my needs. Since this additional load is
very small and does only happen if such a relative location is required, I
think that this is ok.
Note that moving on the carrier will only work correctly for JSBSim and YASim,
but you should now be able to start and move on every not itself moving
object with any FDM.
2005-07-03 09:39:14 +00:00
|
|
|
double current_radius = 0.0;
|
2004-11-22 10:10:33 +00:00
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
size_t sz = triangles.size();
|
|
|
|
for (size_t i = 0; i < sz; ++i) {
|
|
|
|
Triangle triangle;
|
|
|
|
velocityTransformTriangle(t, triangle, triangles[i]);
|
|
|
|
if (!fgdIsectSphereInfLine(triangle.sphere, pt, dir))
|
2004-11-22 10:10:33 +00:00
|
|
|
continue;
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
// Check for intersection.
|
|
|
|
sgdVec3 isecpoint;
|
|
|
|
if ( sgdIsectInfLinePlane( isecpoint, pt, dir, triangle.plane ) &&
|
|
|
|
sgdPointInTriangle( isecpoint, triangle.vertices ) ) {
|
2005-11-04 14:49:15 +00:00
|
|
|
// 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) {
|
Mathias Fröhlich:
I have introduced the posibility to start directly on the carrier.
With that patch you will have a --carrrier=id argument where id can either be
the pennant number configured in the nimitz scenario or the carriers name
also configured in the carriers scenario.
Additionaly you can use --parkpos=id to select different positions on the
carrier. They are also configured in the scenario file.
That includes the switch of the whole FGInterface class to make use of the
groundcache.
That means that an aircraft no longer uses the current elevation value from
the scenery class. It rather has its own local cache of the aircrafts
environment which is setup in the common_init method of FGInterface and
updated either manually by calling
FGInterface::get_groundlevel_m(lat, lon, alt_m);
or implicitly by calling the above method in the
FGInterface::_updateGeo*Position(lat, lon, alt);
methods.
A call get_groundlevel_m rebuilds the groundcache if the request is outside
the range of the cache.
Note that for the real usage of the groundcache including the correct
information about the movement of objects and the velocity information, you
still need to set up the groundcache in the usual way like YASim and JSBSim
currently does.
If you use the native interface, you will get only static objects correctly.
But for FDM's only using one single ground level for a whole step this is IMO
sufficient.
The AIManager gets a way to return the location of a object which is placed
wrt an AI Object. At the moment it only honours AICarriers for that.
That method is a static one, which loads the scenario file for that reason and
throws it away afterwards. This looked like the aprioriate way, because the
AIManager is initialized much later in flightgears bootstrap, and I did not
find an easy way to reorder that for my needs. Since this additional load is
very small and does only happen if such a relative location is required, I
think that this is ok.
Note that moving on the carrier will only work correctly for JSBSim and YASim,
but you should now be able to start and move on every not itself moving
object with any FDM.
2005-07-03 09:39:14 +00:00
|
|
|
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.
|
2005-08-16 09:37:23 +00:00
|
|
|
sgdVec3 pivotoff;
|
|
|
|
sgdSubVec3(pivotoff, pt, triangle.rotation_pivot);
|
|
|
|
sgdVectorProductVec3(vel, triangle.rotation, pivotoff);
|
|
|
|
sgdAddVec3(vel, triangle.velocity);
|
Mathias Fröhlich:
I have introduced the posibility to start directly on the carrier.
With that patch you will have a --carrrier=id argument where id can either be
the pennant number configured in the nimitz scenario or the carriers name
also configured in the carriers scenario.
Additionaly you can use --parkpos=id to select different positions on the
carrier. They are also configured in the scenario file.
That includes the switch of the whole FGInterface class to make use of the
groundcache.
That means that an aircraft no longer uses the current elevation value from
the scenery class. It rather has its own local cache of the aircrafts
environment which is setup in the common_init method of FGInterface and
updated either manually by calling
FGInterface::get_groundlevel_m(lat, lon, alt_m);
or implicitly by calling the above method in the
FGInterface::_updateGeo*Position(lat, lon, alt);
methods.
A call get_groundlevel_m rebuilds the groundcache if the request is outside
the range of the cache.
Note that for the real usage of the groundcache including the correct
information about the movement of objects and the velocity information, you
still need to set up the groundcache in the usual way like YASim and JSBSim
currently does.
If you use the native interface, you will get only static objects correctly.
But for FDM's only using one single ground level for a whole step this is IMO
sufficient.
The AIManager gets a way to return the location of a object which is placed
wrt an AI Object. At the moment it only honours AICarriers for that.
That method is a static one, which loads the scenario file for that reason and
throws it away afterwards. This looked like the aprioriate way, because the
AIManager is initialized much later in flightgears bootstrap, and I did not
find an easy way to reorder that for my needs. Since this additional load is
very small and does only happen if such a relative location is required, I
think that this is ok.
Note that moving on the carrier will only work correctly for JSBSim and YASim,
but you should now be able to start and move on every not itself moving
object with any FDM.
2005-07-03 09:39:14 +00:00
|
|
|
// Save the ground type.
|
|
|
|
*type = triangle.type;
|
|
|
|
// FIXME: figure out how to get that sign ...
|
2004-11-22 10:10:33 +00:00
|
|
|
// *agl = sqrt(sqdist);
|
Mathias Fröhlich:
I have introduced the posibility to start directly on the carrier.
With that patch you will have a --carrrier=id argument where id can either be
the pennant number configured in the nimitz scenario or the carriers name
also configured in the carriers scenario.
Additionaly you can use --parkpos=id to select different positions on the
carrier. They are also configured in the scenario file.
That includes the switch of the whole FGInterface class to make use of the
groundcache.
That means that an aircraft no longer uses the current elevation value from
the scenery class. It rather has its own local cache of the aircrafts
environment which is setup in the common_init method of FGInterface and
updated either manually by calling
FGInterface::get_groundlevel_m(lat, lon, alt_m);
or implicitly by calling the above method in the
FGInterface::_updateGeo*Position(lat, lon, alt);
methods.
A call get_groundlevel_m rebuilds the groundcache if the request is outside
the range of the cache.
Note that for the real usage of the groundcache including the correct
information about the movement of objects and the velocity information, you
still need to set up the groundcache in the usual way like YASim and JSBSim
currently does.
If you use the native interface, you will get only static objects correctly.
But for FDM's only using one single ground level for a whole step this is IMO
sufficient.
The AIManager gets a way to return the location of a object which is placed
wrt an AI Object. At the moment it only honours AICarriers for that.
That method is a static one, which loads the scenario file for that reason and
throws it away afterwards. This looked like the aprioriate way, because the
AIManager is initialized much later in flightgears bootstrap, and I did not
find an easy way to reorder that for my needs. Since this additional load is
very small and does only happen if such a relative location is required, I
think that this is ok.
Note that moving on the carrier will only work correctly for JSBSim and YASim,
but you should now be able to start and move on every not itself moving
object with any FDM.
2005-07-03 09:39:14 +00:00
|
|
|
*agl = sgdLengthVec3( dpt ) - sgdLengthVec3( contact );
|
2004-11-22 10:10:33 +00:00
|
|
|
// *loadCapacity = DBL_MAX;
|
|
|
|
// *frictionFactor = 1.0;
|
Mathias Fröhlich:
I have introduced the posibility to start directly on the carrier.
With that patch you will have a --carrrier=id argument where id can either be
the pennant number configured in the nimitz scenario or the carriers name
also configured in the carriers scenario.
Additionaly you can use --parkpos=id to select different positions on the
carrier. They are also configured in the scenario file.
That includes the switch of the whole FGInterface class to make use of the
groundcache.
That means that an aircraft no longer uses the current elevation value from
the scenery class. It rather has its own local cache of the aircrafts
environment which is setup in the common_init method of FGInterface and
updated either manually by calling
FGInterface::get_groundlevel_m(lat, lon, alt_m);
or implicitly by calling the above method in the
FGInterface::_updateGeo*Position(lat, lon, alt);
methods.
A call get_groundlevel_m rebuilds the groundcache if the request is outside
the range of the cache.
Note that for the real usage of the groundcache including the correct
information about the movement of objects and the velocity information, you
still need to set up the groundcache in the usual way like YASim and JSBSim
currently does.
If you use the native interface, you will get only static objects correctly.
But for FDM's only using one single ground level for a whole step this is IMO
sufficient.
The AIManager gets a way to return the location of a object which is placed
wrt an AI Object. At the moment it only honours AICarriers for that.
That method is a static one, which loads the scenario file for that reason and
throws it away afterwards. This looked like the aprioriate way, because the
AIManager is initialized much later in flightgears bootstrap, and I did not
find an easy way to reorder that for my needs. Since this additional load is
very small and does only happen if such a relative location is required, I
think that this is ok.
Note that moving on the carrier will only work correctly for JSBSim and YASim,
but you should now be able to start and move on every not itself moving
object with any FDM.
2005-07-03 09:39:14 +00:00
|
|
|
}
|
2004-11-22 10:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Whenever we did not have a ground triangle for the requested point,
|
|
|
|
// take the ground level we found during the current cache build.
|
|
|
|
// This is as good as what we had before for agl.
|
|
|
|
double r = sgdLengthVec3( dpt );
|
|
|
|
sgdCopyVec3( contact, dpt );
|
|
|
|
sgdScaleVec3( contact, ground_radius/r );
|
|
|
|
sgdCopyVec3( normal, dpt );
|
|
|
|
sgdNormaliseVec3( normal );
|
|
|
|
sgdSetVec3( vel, 0.0, 0.0, 0.0 );
|
|
|
|
|
|
|
|
// The altitude is the distance of the requested point from the
|
|
|
|
// contact point.
|
|
|
|
*agl = sgdLengthVec3( dpt ) - sgdLengthVec3( contact );
|
|
|
|
*type = FGInterface::Unknown;
|
|
|
|
*loadCapacity = DBL_MAX;
|
|
|
|
*frictionFactor = 1.0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
bool FGGroundCache::caught_wire(double t, const double pt[4][3])
|
2004-11-22 10:10:33 +00:00
|
|
|
{
|
2005-05-30 08:48:27 +00:00
|
|
|
size_t sz = wires.size();
|
|
|
|
if (sz == 0)
|
|
|
|
return false;
|
2004-11-22 10:10:33 +00:00
|
|
|
|
|
|
|
// Time difference to the reference time.
|
|
|
|
t -= cache_ref_time;
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
// Build the two triangles spanning the area where the hook has moved
|
|
|
|
// during the past step.
|
|
|
|
sgdVec4 plane[2];
|
|
|
|
sgdVec3 tri[2][3];
|
|
|
|
sgdMakePlane( plane[0], pt[0], pt[1], pt[2] );
|
|
|
|
sgdCopyVec3( tri[0][0], pt[0] );
|
|
|
|
sgdCopyVec3( tri[0][1], pt[1] );
|
|
|
|
sgdCopyVec3( tri[0][2], pt[2] );
|
|
|
|
sgdMakePlane( plane[1], pt[0], pt[2], pt[3] );
|
|
|
|
sgdCopyVec3( tri[1][0], pt[0] );
|
|
|
|
sgdCopyVec3( tri[1][1], pt[2] );
|
|
|
|
sgdCopyVec3( tri[1][2], pt[3] );
|
|
|
|
|
|
|
|
// Intersect the wire lines with each of these triangles.
|
|
|
|
// You have cautght a wire if they intersect.
|
|
|
|
for (size_t i = 0; i < sz; ++i) {
|
|
|
|
sgdVec3 le[2];
|
2005-08-16 09:37:23 +00:00
|
|
|
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);
|
|
|
|
}
|
2004-11-22 10:10:33 +00:00
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
for (int k=0; k<2; ++k) {
|
|
|
|
sgdVec3 isecpoint;
|
|
|
|
double isecval = sgdIsectLinesegPlane(isecpoint, le[0], le[1], plane[k]);
|
|
|
|
if ( 0.0 <= isecval && isecval <= 1.0 &&
|
|
|
|
sgdPointInTriangle( isecpoint, tri[k] ) ) {
|
|
|
|
SG_LOG(SG_FLIGHT,SG_INFO, "Caught wire");
|
|
|
|
// Store the wire id.
|
|
|
|
wire_id = wires[i].wire_id;
|
|
|
|
return true;
|
2004-11-22 10:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
return false;
|
2004-11-22 10:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FGGroundCache::get_wire_ends(double t, double end[2][3], double vel[2][3])
|
|
|
|
{
|
|
|
|
// Fast return if we do not have an active wire.
|
|
|
|
if (wire_id < 0)
|
|
|
|
return false;
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
// Time difference to the reference time.
|
2004-11-22 10:10:33 +00:00
|
|
|
t -= cache_ref_time;
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
// Search for the wire with the matching wire id.
|
|
|
|
size_t sz = wires.size();
|
|
|
|
for (size_t i = 0; i < sz; ++i) {
|
|
|
|
if (wires[i].wire_id == wire_id) {
|
2005-08-16 09:37:23 +00:00
|
|
|
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);
|
|
|
|
}
|
2005-05-30 08:48:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-11-22 10:10:33 +00:00
|
|
|
}
|
|
|
|
|
2005-05-30 08:48:27 +00:00
|
|
|
return false;
|
2004-11-22 10:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGGroundCache::release_wire(void)
|
|
|
|
{
|
|
|
|
wire_id = -1;
|
|
|
|
}
|