Canvas MapStructure: Difference between revisions

Jump to navigation Jump to search
m
Line 1,028: Line 1,028:
         }
         }
}
}
</syntaxhighlight>
Next, we need to check tutorials.nas to see how it gets a list of targets for the corresponding tutorial, which can be seen below:
<syntaxhighlight lang="nasal">
set_targets(tutorialN.getNode("targets"));
</syntaxhighlight>
So, basically, all we need to do is calling <code>tutorialN.getNode("targets");</code> to get a list of targets for the corresponding tutorial.
Now, let's look up the definition of the set_targets() function, which is processing all targets, to see what we need to do to extract the latitude/longitude for each target:
<syntaxhighlight lang="nasal">
##
# For each <target><*><longitude-deg|latitude-deg> calculate and update
# /sim/tutorials/targets/*/...
#  heading-deg  ... absolute heading to target  (0 -> North)
#  direction-deg ... relative angle to target    (0 -> ahead, 90 -> to the right)
#  distance-m    ... distance in meters
#  eta-min      ... estimated time of arrival (assuming aircraft flies in
#                    in current speed towards target)
#
var set_targets = func(node) {
        node != nil or return;
        var time = time_elapsedN.getValue();
        var dest = props.globals.getNode("/sim/tutorials/targets", 1);
        var aircraft = geo.aircraft_position();
        var hdg = headingN.getValue() + slipN.getValue();
        foreach (var t; node.getChildren()) {
                var lon = t.getNode("longitude-deg");
                var lat = t.getNode("latitude-deg");
                if (lon == nil or lat == nil)
                        die("target coords undefined");
                var target = geo.Coord.new().set_latlon(lat.getValue(), lon.getValue());
                var dist = aircraft.distance_to(target);
                var course = aircraft.course_to(target);
                var angle = geo.normdeg(course - hdg);
                if (angle >= 180)
          angle -= 360;
                var d = dest.getChild(t.getName(), t.getIndex(), 1);
                d.getNode("heading-deg", 1).setDoubleValue(course);
                d.getNode("direction-deg", 1).setDoubleValue(angle);
                var distN = d.getNode("distance-m", 1);
                var lastdist = distN.getValue();
                distN.setDoubleValue(dist);
                if (lastdist != nil) {
                        var speed = (lastdist - dist) / (time - last_step_time) + 0.00001;  # m/s
                        d.getNode("eta-min", 1).setDoubleValue(dist / (speed * 60));
                }
        }
        last_step_time = time;
}
</syntaxhighlight>
</syntaxhighlight>


Navigation menu