1
0
Fork 0

Don't duplicate userpos in in AIObjects.

This commit is contained in:
James Turner 2013-03-16 14:47:41 +00:00
parent 4b21dc51ee
commit d70e26d87b
7 changed files with 78 additions and 151 deletions

View file

@ -255,9 +255,9 @@ void FGAIBallistic::bind() {
} }
void FGAIBallistic::update(double dt) { void FGAIBallistic::update(double dt)
{
FGAIBase::update(dt); FGAIBase::update(dt);
_setUserPos();
if (_slave_to_ac){ if (_slave_to_ac){
slaveToAC(dt); slaveToAC(dt);
@ -623,7 +623,7 @@ void FGAIBallistic::slaveToAC(double dt){
pch = manager->get_user_pitch(); pch = manager->get_user_pitch();
rll = manager->get_user_roll(); rll = manager->get_user_roll();
// agl = manager->get_user_agl(); // agl = manager->get_user_agl();
setOffsetPos(userpos, hdg, pch, rll); setOffsetPos(globals->get_aircraft_position(), hdg, pch, rll);
setSpeed(manager->get_user_speed()); setSpeed(manager->get_user_speed());
} }
@ -1011,15 +1011,11 @@ void FGAIBallistic::report_impact(double elevation, const FGAIBase *object)
_impact_report_node->setStringValue(props->getPath()); _impact_report_node->setStringValue(props->getPath());
} }
SGVec3d FGAIBallistic::getCartUserPos() const {
SGVec3d cartUserPos = SGVec3d::fromGeod(userpos);
return cartUserPos;
}
SGVec3d FGAIBallistic::getCartHitchPos() const{ SGVec3d FGAIBallistic::getCartHitchPos() const{
// convert geodetic positions to geocentered // convert geodetic positions to geocentered
SGVec3d cartuserPos = SGVec3d::fromGeod(userpos); SGVec3d cartuserPos = globals->get_aircraft_position_cart();
//SGVec3d cartPos = getCartPos(); //SGVec3d cartPos = getCartPos();
// Transform to the right coordinate frame, configuration is done in // Transform to the right coordinate frame, configuration is done in
@ -1031,7 +1027,7 @@ SGVec3d FGAIBallistic::getCartHitchPos() const{
-_z_offset * SG_FEET_TO_METER); -_z_offset * SG_FEET_TO_METER);
// Transform the user position to the horizontal local coordinate system. // Transform the user position to the horizontal local coordinate system.
SGQuatd hlTrans = SGQuatd::fromLonLat(userpos); SGQuatd hlTrans = SGQuatd::fromLonLat(globals->get_aircraft_position());
// and postrotate the orientation of the user model wrt the horizontal // and postrotate the orientation of the user model wrt the horizontal
// local frame // local frame
@ -1103,7 +1099,7 @@ double FGAIBallistic::getRelBrgHitchToUser() const {
//calculate the relative bearing //calculate the relative bearing
double az1, az2, distance; double az1, az2, distance;
geo_inverse_wgs_84(_offsetpos, userpos, &az1, &az2, &distance); geo_inverse_wgs_84(_offsetpos, globals->get_aircraft_position(), &az1, &az2, &distance);
double rel_brg = az1 - hdg; double rel_brg = az1 - hdg;
@ -1116,14 +1112,14 @@ double FGAIBallistic::getElevHitchToUser() const {
//calculate the distance from the user position //calculate the distance from the user position
SGVec3d carthitchPos = getCartHitchPos(); SGVec3d carthitchPos = getCartHitchPos();
SGVec3d cartuserPos = getCartUserPos(); SGVec3d cartuserPos = globals->get_aircraft_position_cart();
SGVec3d diff = cartuserPos - carthitchPos; SGVec3d diff = cartuserPos - carthitchPos;
double distance = norm(diff); double distance = norm(diff);
double angle = 0; double angle = 0;
double daltM = userpos.getElevationM() - _offsetpos.getElevationM(); double daltM = globals->get_aircraft_position().getElevationM() - _offsetpos.getElevationM();
// now the angle, positive angles are upwards // now the angle, positive angles are upwards
if (fabs(distance) < SGLimits<float>::min()) { if (fabs(distance) < SGLimits<float>::min()) {

View file

@ -230,7 +230,6 @@ private:
//void setOffsetPos(SGGeod pos, double heading, double pitch, double roll); //void setOffsetPos(SGGeod pos, double heading, double pitch, double roll);
//void setOffsetVelocity(double dt, SGGeod pos); //void setOffsetVelocity(double dt, SGGeod pos);
SGVec3d getCartUserPos() const;
SGVec3d getCartOffsetPos(SGGeod pos, double heading, double pitch, double roll) const; SGVec3d getCartOffsetPos(SGGeod pos, double heading, double pitch, double roll) const;
//double getDistanceLoadToHitch() const; //double getDistanceLoadToHitch() const;

View file

@ -100,8 +100,6 @@ FGAIBase::FGAIBase(object_type ot, bool enableHot) :
_roll_offset = 0; _roll_offset = 0;
_yaw_offset = 0; _yaw_offset = 0;
userpos = SGGeod::fromDeg(0, 0);
pos = SGGeod::fromDeg(0, 0); pos = SGGeod::fromDeg(0, 0);
speed = 0; speed = 0;
altitude_ft = 0; altitude_ft = 0;
@ -479,106 +477,68 @@ void FGAIBase::removeSoundFx() {
} }
} }
double FGAIBase::UpdateRadar(FGAIManager* manager) { double FGAIBase::UpdateRadar(FGAIManager* manager)
{
bool control = fgGetBool("/sim/controls/radar", true); bool control = fgGetBool("/sim/controls/radar", true);
if(!control) return 0; if(!control) return 0;
double radar_range_ft2 = fgGetDouble("/instrumentation/radar/range"); double radar_range_m = fgGetDouble("/instrumentation/radar/range");
bool force_on = fgGetBool("/instrumentation/radar/debug-mode", false); bool force_on = fgGetBool("/instrumentation/radar/debug-mode", false);
radar_range_ft2 *= SG_NM_TO_METER * SG_METER_TO_FEET * 1.1; // + 10% radar_range_m *= SG_NM_TO_METER * 1.1; // + 10%
radar_range_ft2 *= radar_range_ft2; radar_range_m *= radar_range_m; // squared
double user_latitude = manager->get_user_latitude(); double d2 = distSqr(SGVec3d::fromGeod(pos), globals->get_aircraft_position_cart());
double user_longitude = manager->get_user_longitude(); double range_ft = sqrt(d2) * SG_METER_TO_FEET;
double lat_range = fabs(pos.getLatitudeDeg() - user_latitude) * ft_per_deg_lat;
double lon_range = fabs(pos.getLongitudeDeg() - user_longitude) * ft_per_deg_lon; if (!force_on && (d2 > radar_range_m)) {
double range_ft2 = lat_range*lat_range + lon_range*lon_range; return range_ft * range_ft;
//
// Test whether the target is within radar range.
//
in_range = (range_ft2 && (range_ft2 <= radar_range_ft2));
if ( in_range || force_on ) {
props->setBoolValue("radar/in-range", true);
// copy values from the AIManager
double user_altitude = manager->get_user_altitude();
double user_heading = manager->get_user_heading();
double user_pitch = manager->get_user_pitch();
//double user_yaw = manager->get_user_yaw();
//double user_speed = manager->get_user_speed();
// calculate range to target in feet and nautical miles
double range_ft = sqrt( range_ft2 );
range = range_ft / 6076.11549;
// calculate bearing to target
if (pos.getLatitudeDeg() >= user_latitude) {
bearing = atan2(lat_range, lon_range) * SG_RADIANS_TO_DEGREES;
if (pos.getLongitudeDeg() >= user_longitude) {
bearing = 90.0 - bearing;
} else {
bearing = 270.0 + bearing;
}
} else {
bearing = atan2(lon_range, lat_range) * SG_RADIANS_TO_DEGREES;
if (pos.getLongitudeDeg() >= user_longitude) {
bearing = 180.0 - bearing;
} else {
bearing = 180.0 + bearing;
}
}
// This is an alternate way to compute bearing and distance which
// agrees with the original scheme within about 0.1 degrees.
//
// Point3D start( user_longitude * SGD_DEGREES_TO_RADIANS,
// user_latitude * SGD_DEGREES_TO_RADIANS, 0 );
// Point3D dest( pos.getLongitudeRad(), pos.getLatitudeRad(), 0 );
// double gc_bearing, gc_range;
// calc_gc_course_dist( start, dest, &gc_bearing, &gc_range );
// gc_range *= SG_METER_TO_NM;
// gc_bearing *= SGD_RADIANS_TO_DEGREES;
// printf("orig b = %.3f %.2f gc b= %.3f, %.2f\n",
// bearing, range, gc_bearing, gc_range);
// calculate look left/right to target, without yaw correction
horiz_offset = bearing - user_heading;
if (horiz_offset > 180.0) horiz_offset -= 360.0;
if (horiz_offset < -180.0) horiz_offset += 360.0;
// calculate elevation to target
elevation = atan2( altitude_ft - user_altitude, range_ft ) * SG_RADIANS_TO_DEGREES;
// calculate look up/down to target
vert_offset = elevation - user_pitch;
/* this calculation needs to be fixed, but it isn't important anyway
// calculate range rate
double recip_bearing = bearing + 180.0;
if (recip_bearing > 360.0) recip_bearing -= 360.0;
double my_horiz_offset = recip_bearing - hdg;
if (my_horiz_offset > 180.0) my_horiz_offset -= 360.0;
if (my_horiz_offset < -180.0) my_horiz_offset += 360.0;
rdot = (-user_speed * cos( horiz_offset * SG_DEGREES_TO_RADIANS ))
+(-speed * 1.686 * cos( my_horiz_offset * SG_DEGREES_TO_RADIANS ));
*/
// now correct look left/right for yaw
// horiz_offset += user_yaw; // FIXME: WHY WOULD WE WANT TO ADD IN SIDE-SLIP HERE?
// calculate values for radar display
y_shift = range * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
x_shift = range * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
rotation = hdg - user_heading;
if (rotation < 0.0) rotation += 360.0;
ht_diff = altitude_ft - user_altitude;
} }
props->setBoolValue("radar/in-range", true);
return range_ft2; // copy values from the AIManager
double user_heading = manager->get_user_heading();
double user_pitch = manager->get_user_pitch();
range = range_ft * SG_FEET_TO_METER * SG_METER_TO_NM;
// calculate bearing to target
bearing = SGGeodesy::courseDeg(globals->get_aircraft_position(), pos);
// calculate look left/right to target, without yaw correction
horiz_offset = bearing - user_heading;
SG_NORMALIZE_RANGE(horiz_offset, -180.0, 180.0);
// calculate elevation to target
ht_diff = altitude_ft - globals->get_aircraft_position().getElevationFt();
elevation = atan2( ht_diff, range_ft ) * SG_RADIANS_TO_DEGREES;
// calculate look up/down to target
vert_offset = elevation - user_pitch;
/* this calculation needs to be fixed, but it isn't important anyway
// calculate range rate
double recip_bearing = bearing + 180.0;
if (recip_bearing > 360.0) recip_bearing -= 360.0;
double my_horiz_offset = recip_bearing - hdg;
if (my_horiz_offset > 180.0) my_horiz_offset -= 360.0;
if (my_horiz_offset < -180.0) my_horiz_offset += 360.0;
rdot = (-user_speed * cos( horiz_offset * SG_DEGREES_TO_RADIANS ))
+(-speed * 1.686 * cos( my_horiz_offset * SG_DEGREES_TO_RADIANS ));
*/
// now correct look left/right for yaw
// horiz_offset += user_yaw; // FIXME: WHY WOULD WE WANT TO ADD IN SIDE-SLIP HERE?
// calculate values for radar display
y_shift = range * cos( horiz_offset * SG_DEGREES_TO_RADIANS);
x_shift = range * sin( horiz_offset * SG_DEGREES_TO_RADIANS);
rotation = hdg - user_heading;
SG_NORMALIZE_RANGE(rotation, 0.0, 360.0);
return range_ft * range_ft;
} }
/* /*
@ -637,12 +597,6 @@ void FGAIBase::_setLatitude ( double latitude ) {
pos.setLatitudeDeg(latitude); pos.setLatitudeDeg(latitude);
} }
void FGAIBase::_setUserPos(){
userpos.setLatitudeDeg(manager->get_user_latitude());
userpos.setLongitudeDeg(manager->get_user_longitude());
userpos.setElevationM(manager->get_user_altitude() * SG_FEET_TO_METER);
}
void FGAIBase::_setSubID( int s ) { void FGAIBase::_setSubID( int s ) {
_subID = s; _subID = s;
} }

View file

@ -140,9 +140,6 @@ public:
std::string _name; std::string _name;
string _parent; string _parent;
SGGeod userpos;
protected: protected:
/** /**
* Tied-properties helper, record nodes which are tied for easy un-tie-ing * Tied-properties helper, record nodes which are tied for easy un-tie-ing
@ -255,7 +252,6 @@ public:
void _setLongitude( double longitude ); void _setLongitude( double longitude );
void _setLatitude ( double latitude ); void _setLatitude ( double latitude );
void _setSubID( int s ); void _setSubID( int s );
void _setUserPos();
double _getAltitudeAGL(SGGeod inpos, double start); double _getAltitudeAGL(SGGeod inpos, double start);

View file

@ -155,17 +155,11 @@ void FGAIStorm::Run(double dt) {
// *************************************************** // ***************************************************
// copy user's position from the AIManager // copy user's position from the AIManager
double user_latitude = manager->get_user_latitude(); double d = dist(SGVec3d::fromGeod(pos), globals->get_aircraft_position_cart());
double user_longitude = manager->get_user_longitude(); double rangeNm = d * SG_METER_TO_NM;
double user_altitude = manager->get_user_altitude(); double user_altitude = globals->get_aircraft_position().getElevationFt();
// calculate range to target in feet and nautical miles if (rangeNm < (diameter * 0.5) &&
double lat_range = fabs(pos.getLatitudeDeg() - user_latitude) * ft_per_deg_lat;
double lon_range = fabs(pos.getLongitudeDeg() - user_longitude) * ft_per_deg_lon;
double range_ft = sqrt(lat_range*lat_range + lon_range*lon_range);
range = range_ft / 6076.11549;
if (range < (diameter * 0.5) &&
user_altitude > (altitude_ft - 1000.0) && user_altitude > (altitude_ft - 1000.0) &&
user_altitude < height) { user_altitude < height) {
turb_mag_node->setDoubleValue(strength_norm); turb_mag_node->setDoubleValue(strength_norm);

View file

@ -201,14 +201,6 @@ double slice_center_lat;
// **************************************
// various variables relative to the user
// **************************************
double user_latitude = manager->get_user_latitude();
double user_longitude = manager->get_user_longitude();
double user_altitude = manager->get_user_altitude(); // MSL
//we need to know the thermal foot AGL altitude //we need to know the thermal foot AGL altitude
@ -227,6 +219,9 @@ if (dt_count >= 10.0 ) {
} }
//user altitude relative to the thermal height, seen AGL from the thermal foot //user altitude relative to the thermal height, seen AGL from the thermal foot
double user_altitude = globals->get_aircraft_position().getElevationFt();
if ( user_altitude < 1.0 ) { user_altitude = 1.0 ;}; // an ugly way to avoid NaNs for users at alt 0 if ( user_altitude < 1.0 ) { user_altitude = 1.0 ;}; // an ugly way to avoid NaNs for users at alt 0
double user_altitude_agl= ( user_altitude - ground_elev_ft ) ; double user_altitude_agl= ( user_altitude - ground_elev_ft ) ;
alt_rel = user_altitude_agl / altitude_agl_ft; alt_rel = user_altitude_agl / altitude_agl_ft;
@ -343,14 +338,9 @@ double dt_slice_lat = dt_slice_lat_FT / ft_per_deg_lat;
slice_center_lon = thermal_foot_lon + dt_slice_lon; slice_center_lon = thermal_foot_lon + dt_slice_lon;
slice_center_lat = thermal_foot_lat + dt_slice_lat; slice_center_lat = thermal_foot_lat + dt_slice_lat;
double dist_center_lon = fabs(slice_center_lon - user_longitude)* ft_per_deg_lon; dist_center = SGGeodesy::distanceNm(SGGeod::fromDeg(slice_center_lon, slice_center_lat),
double dist_center_lat = fabs(slice_center_lat - user_latitude)* ft_per_deg_lat; globals->get_aircraft_position());
double dist_center_FT = sqrt ( dist_center_lon*dist_center_lon + dist_center_lat*dist_center_lat ); // feet
dist_center = dist_center_FT/ 6076.11549; //nautic miles
// Now we can calculate Vup // Now we can calculate Vup
if ( max_strength >=0.0 ) { // this is a thermal if ( max_strength >=0.0 ) { // this is a thermal

View file

@ -243,7 +243,6 @@ void FGAIWingman::formateToAC(double dt){
double p_hdg, p_pch, p_rll, p_agl, p_ht, p_wow = 0; double p_hdg, p_pch, p_rll, p_agl, p_ht, p_wow = 0;
setTgtOffsets(dt, 25); setTgtOffsets(dt, 25);
_setUserPos();
if (_pnode != 0) { if (_pnode != 0) {
setParentPos(); setParentPos();
@ -257,8 +256,8 @@ void FGAIWingman::formateToAC(double dt){
p_hdg = manager->get_user_heading(); p_hdg = manager->get_user_heading();
p_pch = manager->get_user_pitch(); p_pch = manager->get_user_pitch();
p_rll = manager->get_user_roll(); p_rll = manager->get_user_roll();
p_ht = manager->get_user_altitude(); p_ht = globals->get_aircraft_position().getElevationFt();
setOffsetPos(userpos, p_hdg,p_pch, p_rll); setOffsetPos(globals->get_aircraft_position(), p_hdg,p_pch, p_rll);
setSpeed(manager->get_user_speed()); setSpeed(manager->get_user_speed());
} }
@ -341,7 +340,6 @@ void FGAIWingman::Join(double dt) {
double p_hdg, p_pch, p_rll = 0; double p_hdg, p_pch, p_rll = 0;
setTgtOffsets(dt, 25); setTgtOffsets(dt, 25);
_setUserPos();
if (_pnode != 0) { if (_pnode != 0) {
setParentPos(); setParentPos();
@ -355,7 +353,7 @@ void FGAIWingman::Join(double dt) {
p_hdg = manager->get_user_heading(); p_hdg = manager->get_user_heading();
p_pch = manager->get_user_pitch(); p_pch = manager->get_user_pitch();
p_rll = manager->get_user_roll(); p_rll = manager->get_user_roll();
setOffsetPos(userpos, p_hdg, p_pch, p_rll); setOffsetPos(globals->get_aircraft_position(), p_hdg, p_pch, p_rll);
parent_hdg = manager->get_user_heading(); parent_hdg = manager->get_user_heading();
parent_spd = manager->get_user_speed(); parent_spd = manager->get_user_speed();
} }