]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIShip.cxx
fix a segmentation fault situation that is exposed at least on IRIX (but not Linux).
[flightgear.git] / src / AIModel / AIShip.cxx
index 650d835832158c84c0a4875d0e9dcb7cd16afc9d..32950ba65eb1445be5f6cc28035785aa044bd9fd 100644 (file)
 #include "AIShip.hxx"
 
 
-FGAIShip::FGAIShip() {
+FGAIShip::FGAIShip(FGAIManager* mgr) {
+   manager = mgr;
+   _type_str = "ship";
+   _otype = otShip;
+
    hdg_lock = false;
    rudder = 0.0;
 }
@@ -42,6 +46,9 @@ bool FGAIShip::init() {
 
 void FGAIShip::bind() {
     FGAIBase::bind();
+
+    props->tie("surface-positions/rudder-pos-norm",
+                SGRawValuePointer<double>(&rudder));
 }
 
 void FGAIShip::unbind() {
@@ -50,28 +57,24 @@ void FGAIShip::unbind() {
 
 void FGAIShip::update(double dt) {
 
+   FGAIBase::update(dt);
    Run(dt);
    Transform();
-   FGAIBase::update(dt);
 }
 
 
 
 void FGAIShip::Run(double dt) {
 
+   if (fp) ProcessFlightPlan(dt);
+
    double turn_radius_ft;
    double turn_circum_ft;
    double speed_north_deg_sec;
    double speed_east_deg_sec;
-   double ft_per_deg_lon;
-   double ft_per_deg_lat;
    double dist_covered_ft;
    double alpha;
 
-   // get size of a degree at this latitude
-   ft_per_deg_lat = 366468.96 - 3717.12 * cos(pos.lat() / 57.2958 );
-   ft_per_deg_lon = 365228.16 * cos(pos.lat() / 57.2958);
-
    // adjust speed
    double speed_diff = tgt_speed - speed;
    if (fabs(speed_diff) > 0.1) {
@@ -80,8 +83,10 @@ void FGAIShip::Run(double dt) {
    } 
    
    // convert speed to degrees per second
-   speed_north_deg_sec = cos( hdg / 57.29577951 ) * speed * 1.686 / ft_per_deg_lat;
-   speed_east_deg_sec  = sin( hdg / 57.29577951 ) * speed * 1.686 / ft_per_deg_lon;
+   speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
+                          * speed * 1.686 / ft_per_deg_lat;
+   speed_east_deg_sec  = sin( hdg / SG_RADIANS_TO_DEGREES )
+                          * speed * 1.686 / ft_per_deg_lon;
 
    // set new position
    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
@@ -89,8 +94,9 @@ void FGAIShip::Run(double dt) {
 
    // adjust heading based on current rudder angle
    if (rudder != 0.0) {
-     turn_radius_ft = 0.088362 * speed * speed / tan( fabs(rudder) / 57.2958 );
-     turn_circum_ft = 6.2831853 * turn_radius_ft;
+     turn_radius_ft = 0.088362 * speed * speed
+                       / tan( fabs(rudder) / SG_RADIANS_TO_DEGREES );
+     turn_circum_ft = SGD_2PI * turn_radius_ft;
      dist_covered_ft = speed * 1.686 * dt; 
      alpha = dist_covered_ft / turn_circum_ft * 360.0;
      hdg += alpha * sign( rudder );
@@ -157,3 +163,12 @@ double FGAIShip::sign(double x) {
   if ( x < 0.0 ) { return -1.0; }
   else { return 1.0; }
 }
+
+void FGAIShip::setFlightPlan(FGAIFlightPlan* f) {
+  fp = f;
+}
+
+void FGAIShip::ProcessFlightPlan(double dt) {
+  // not implemented yet
+}
+