Debian Codegraph @

Aircraft/Generic/WalkView/walkview.nas

raw
1 ###############################################################################
2 ##
3 ## Walk view module for FlightGear.
4 ##
5 ## Inspired by the work of Stewart Andreason.
6 ##
7 ## Copyright (C) 2010, 2016 Anders Gidenstam (anders(at)gidenstam.org)
8 ## This file is licensed under the GPL license v2 or later.
9 ##
10 ###############################################################################
11
12 # Global API. Automatically selects the right walker for the current view.
13
14 # NOTE: Coordinates are always 3 component lists: [x, y, z] in meters.
15 # The coordinate system is the same as the main 3d model one.
16 # X - back, Y - right and Z - up.
17
18 # Set the forward speed of the active walker.
19 # speed - walker speed in m/sec
20 # Returns 1 of there is an active walker and 0 otherwise.
21 var forward = func (speed) {
22 var cv = view.current.getPath();
23 if (contains(walkers, cv)) {
24 walkers[cv].forward(speed);
25 return 1;
26 } else {
27 return 0;
28 }
29 }
30
31 # Set the side step speed of the active walker.
32 # speed - walker speed in m/sec
33 # Returns 1 of there is an active walker and 0 otherwise.
34 var side_step = func (speed) {
35 var cv = view.current.getPath();
36 if (contains(walkers, cv)) {
37 walkers[cv].side_step(speed);
38 return 1;
39 } else {
40 return 0;
41 }
42 }
43
44 # Get the currently active walker.
45 # Returns the active walker object or nil otherwise.
46 var active_walker = func {
47 var cv = view.current.getPath();
48 if (contains(walkers, cv)) {
49 return walkers[cv];
50 } else {
51 return nil;
52 }
53 }
54
55 ###############################################################################
56 # The walker class.
57 # ==============================================================================
58 # Class for a moving view.
59 #
60 # CONSTRUCTOR:
61 # walker.new(<view name>, <constraints>, <managers>);
62 #
63 # view name ... The name of the view : string
64 # constraints ... The movement constraints : constraint hash
65 # Determines where the view can go.
66 # managers ... Optional list of custom managers. A manager is a
67 # a hash that contains an update function of the type
68 # func(walker instance). The update function
69 # of each manager will be called as the last part of
70 # each walker update. Intended for controlling a
71 # a 3d model or similar.
72 #
73 # METHODS:
74 # active() : bool
75 # returns true if this walk view is active.
76 #
77 # forward(speed)
78 # Sets the forward speed of this walk view.
79 # speed ... speed in m/sec : double
80 #
81 # side_step(speed)
82 # Sets the side step speed of this walk view.
83 # speed ... speed in m/sec : double
84 #
85 # set_pos(pos)
86 # pos ... position in meter : [double, double, double]
87 # get_pos() : position ([meter, meter, meter])
88 #
89 # set_eye_height(h)
90 # get_eye_height() : int (meter)
91 #
92 # set_constraints(constraints)
93 # get_constraints() : constraint hash
94 #
95 # EXAMPLE:
96 # var constraint =
97 # walkview.slopingYAlignedPlane.new([19.1, -0.3, -8.85],
98 # [19.5, 0.3, -8.85]);
99 # var walker = walkview.walker.new("Passenger View", constraint);
100 #
101 # See Aircraft/Nordstern, Aircraft/Short_Empire and Aircraft/ZLT-NT
102 # for working examples of walk views.
103 #
104 # NOTES:
105 # Currently there can only be one view manager per view so the
106 # walk view should not have any other view manager.
107 var Walker = {
108 new : func (view_name, constraints = nil, managers = nil) {
109 var obj = { parents : [Walker] };
110 obj.view_name = view_name;
111 obj.view = view.views[view.indexof(view_name)];
112 obj.constraints = constraints;
113 obj.managers = managers;
114 obj.position = [
115 obj.view.getNode("config/z-offset-m").getValue(),
116 obj.view.getNode("config/x-offset-m").getValue(),
117 obj.view.getNode("config/y-offset-m").getValue()
118 ];
119 var h = obj.view.getNode("config/heading-offset-deg");
120 if (h != nil and h.getValue() != nil) {
121 obj.heading = h.getValue();
122 }
123 obj.speed_fwd = 0.0;
124 obj.speed_side = 0.0;
125 obj.isactive = 0;
126 obj.eye_height = 1.60;
127 obj.goal_height = obj.position[2] + obj.eye_height;
128
129 # Register this walker.
130 view.manager.register(view_name, obj);
131 walkers[obj.view.getPath()] = obj;
132
133 #debug.dump(obj);
134 return obj;
135 },
136 active : func {
137 return me.isactive;
138 },
139 forward : func (speed) {
140 me.speed_fwd = speed;
141 },
142 side_step : func (speed) {
143 me.speed_side = speed;
144 },
145 set_pos : func (pos) {
146 me.position[0] = pos[0];
147 me.position[1] = pos[1];
148 me.position[2] = pos[2];
149 },
150 get_pos : func {
151 return [me.position[0], me.position[1], me.position[2]];
152 },
153 set_eye_height : func (h) {
154 me.eye_height = h;
155 },
156 get_eye_height : func {
157 return me.eye_height;
158 },
159 set_constraints : func (constraints) {
160 me.constraints = constraints;
161 },
162 get_constraints : func {
163 return me.constraints;
164 },
165 # View handler implementation.
166 init : func {
167 },
168 start : func {
169 me.isactive = 1;
170 me.last_time = getprop("/sim/time/elapsed-sec") - 0.0001;
171 me.update();
172 me.position[2] = me.goal_height;
173 },
174 stop : func {
175 me.isactive = 0;
176 },
177 # The update function is called by the view manager when the view is active.
178 update : func {
179 var t = getprop("/sim/time/elapsed-sec");
180 var dt = t - me.last_time;
181 if (dt == 0.0) return;
182
183 var cur = props.globals.getNode("/sim/current-view");
184 me.heading = cur.getNode("heading-offset-deg").getValue();
185
186 var new_pos =
187 [me.position[0] -
188 (me.speed_fwd * dt * math.cos(me.heading * TO_RAD) +
189 me.speed_side * dt * math.sin(me.heading * TO_RAD)),
190 me.position[1] -
191 (me.speed_fwd * dt * math.sin(me.heading * TO_RAD) -
192 me.speed_side * dt * math.cos(me.heading * TO_RAD)),
193 me.position[2]];
194
195 var cur_height = me.position[2];
196 if (me.constraints != nil) {
197 new_pos = me.constraints.constrain(new_pos);
198 if (new_pos == NO_POS) {
199 printlog("warn",
200 "WalkView: Constraint for " ~ me.view_name ~
201 " returned NO_POS.");
202 } else {
203 me.position = new_pos;
204 me.goal_height = me.position[2] + me.eye_height;
205 }
206 }
207 # Change the view height smoothly
208 if (math.abs(me.goal_height - cur_height) > 2.0 * dt) {
209 me.position[2] =
210 cur_height +
211 2.0 * dt *
212 ((me.goal_height > cur_height) ? 1 : -1);
213 } else {
214 me.position[2] = me.goal_height;
215 }
216
217 cur.getNode("z-offset-m").setValue(me.position[0]);
218 cur.getNode("x-offset-m").setValue(me.position[1]);
219 cur.getNode("y-offset-m").setValue(me.position[2]);
220
221 if (me.managers != nil) {
222 foreach(var m; me.managers) {
223 m.update(me);
224 }
225 }
226
227 me.last_time = t;
228 return 0.0;
229 },
230 };
231
232 ###############################################################################
233 # Constraint classes. Determines where the view can walk.
234 #
235
236 # Convenience functions.
237
238 # Build a UnionConstraint hierarchy from a list of constraints.
239 # cs - list of constraints : [constraint]
240 var makeUnionConstraint = func (cs) {
241 if (size(cs) < 2) return cs[0];
242
243 var ret = cs[0];
244 for (var i = 1; i < size(cs); i += 1) {
245 ret = UnionConstraint.new(ret, cs[i]);
246 }
247 return ret;
248 }
249
250 # Build a UnionConstraint hierachy that represents a polyline path
251 # with a certain width. Each internal point gets a circular surface.
252 # points - list of points : [position] ([[meter, meter, meter]])
253 # width - width of the path : length (meter)
254 # round_ends - put a circle also on the first and last points : bool
255 var makePolylinePath = func (points, width, round_ends = 0) {
256 if (size(points) < 2) return nil;
257 var ret = LinePlane.new(points[0], points[1], width);
258 if (round_ends) {
259 ret = UnionConstraint.new(ret,
260 CircularXYSurface.new(points[0], width/2));
261 }
262 for (var i = 2; i < size(points); i += 1) {
263 var line = LinePlane.new(points[i-1], points[i], width);
264 if (i + 1 < size(points) or round_ends) {
265 line = UnionConstraint.new
266 (line,
267 CircularXYSurface.new(points[i], width/2));
268 }
269 ret = UnionConstraint.new(line, ret);
270 }
271 return ret;
272 }
273
274 # The union of two constraints.
275 # c1, c2 - the constraints : constraint
276 # NOTE: Assumes that the constraints are convex.
277 var UnionConstraint = {
278 new : func (c1, c2) {
279 var obj = { parents : [UnionConstraint] };
280 obj.c1 = c1;
281 obj.c2 = c2;
282 return obj;
283 },
284 constrain : func (pos) {
285 var p1 = me.c1.constrain(pos);
286 var p2 = me.c2.constrain(pos);
287 if (p1[0] == pos[0] and p1[1] == pos[1]) {
288 return p1;
289 } elsif (p2[0] == pos[0] and p2[1] == pos[1]) {
290 return p2;
291 } else {
292 if (closerXY(pos, p1, p2) <= 0) {
293 return p1;
294 } else {
295 return p2;
296 }
297 }
298 }
299 };
300
301 # Rectangular plane defined by a straight line and a width.
302 # The line is extruded horizontally on each side by width/2 into a
303 # planar surface.
304 # p1, p2 - the line endpoints. : position ([meter, meter, meter])
305 # width - total width of the plane. : length (meter)
306 var LinePlane = {
307 new : func (p1, p2, width) {
308 var obj = { parents : [LinePlane] };
309 obj.p1 = p1;
310 obj.p2 = p2;
311 obj.halfwidth = width/2;
312 obj.length = vec2.length(vec2.sub(p2, p1));
313 obj.e1 = vec2.normalize(vec2.sub(p2, p1));
314 obj.e2 = [obj.e1[1], -obj.e1[0]];
315 obj.k = (p2[2] - p1[2]) / obj.length;
316
317 return obj;
318 },
319 constrain : func (pos) {
320 var p = [pos[0], pos[1], pos[2]];
321 var pXY = vec2.sub(pos, me.p1);
322 var along = vec2.dot(pXY, me.e1);
323 var across = vec2.dot(pXY, me.e2);
324
325 var along2 = max(0, min(along, me.length));
326 var across2 = max(-me.halfwidth, min(across, me.halfwidth));
327 if (along2 != along or across2 != across) {
328 # Compute new XY position.
329 var t = vec2.add(vec2.mul(along2, me.e1), vec2.mul(across2, me.e2));
330 p[0] = me.p1[0] + t[0];
331 p[1] = me.p1[1] + t[1];
332 }
333
334 # Compute Z positition.
335 p[2] = me.p1[2] + me.k * along2;
336 return p;
337 }
338 };
339
340 # Circular surface aligned with the XY plane
341 # center - the center point : position ([meter, meter, meter])
342 # radius - radius in the XY plane : length (meter)
343 var CircularXYSurface = {
344 new : func (center, radius) {
345 var obj = { parents : [CircularXYSurface] };
346 obj.center = center;
347 obj.radius = radius;
348
349 return obj;
350 },
351 constrain : func (pos) {
352 var p = [pos[0], pos[1], me.center[2]];
353 var pXY = vec2.sub(pos, me.center);
354 var lXY = vec2.length(pXY);
355
356 if (lXY > me.radius) {
357 var t = vec2.add(me.center, vec2.mul(me.radius/lXY, pXY));
358 p[0] = t[0];
359 p[1] = t[1];
360 }
361 return p;
362 },
363 };
364
365 # Mostly aligned plane sloping along the X axis.
366 # NOTE: Obsolete. Use linePlane instead.
367 # minp - the X,Y minimum point : position ([meter, meter, meter])
368 # maxp - the X,Y maximum point : position ([meter, meter, meter])
369 var SlopingYAlignedPlane = {
370 new : func (minp, maxp) {
371 return LinePlane.new([minp[0], (minp[1] + maxp[1])/2, minp[2]],
372 [maxp[0], (minp[1] + maxp[1])/2, maxp[2]],
373 (maxp[1] - minp[1]));
374 }
375 };
376
377 # Action constraint
378 # Triggers an action when entering or exiting the constraint.
379 # constraint - the area in question : constraint
380 # on_enter() - function that is called when the walker enters the area.
381 # on_exit(x, y) - function that is called when the walker leaves the area.
382 # x and y are <0, 0 or >0 depending on in which direction(s)
383 # the walker left the constraint.
384 var ActionConstraint = {
385 new : func (constraint, on_enter = nil, on_exit = nil) {
386 var obj = { parents : [ActionConstraint] };
387 obj.constraint = constraint;
388 obj.on_enter = on_enter;
389 obj.on_exit = on_exit;
390 obj.inside = 0;
391 return obj;
392 },
393 constrain : func (pos) {
394 var p = me.constraint.constrain(pos);
395 if (p[0] == pos[0] and p[1] == pos[1]) {
396 if (!me.inside) {
397 me.inside = 1;
398 if (me.on_enter != nil) {
399 me.on_enter();
400 }
401 }
402 } else {
403 if (me.inside) {
404 me.inside -= 1;
405 if (!me.inside and me.on_exit != nil) {
406 me.on_exit(pos[0] - p[0], pos[1] - p[1]);
407 }
408 }
409 }
410 return p;
411 }
412 };
413
414 # Conditional constraint
415 # The area is only available when the predicate function returns true.
416 # constraint - the area in question : constraint
417 # predicate() - boolean function that determines if the area is available.
418 var ConditionalConstraint = {
419 new : func (constraint, predicate = nil) {
420 var obj = { parents : [ConditionalConstraint] };
421 obj.constraint = constraint;
422 obj.predicate = predicate;
423 return obj;
424 },
425 constrain : func (pos) {
426 if (me.predicate == nil or me.predicate()) {
427 return me.constraint.constrain(pos);
428 } else {
429 return NO_POS;
430 }
431 }
432 };
433
434
435 ###############################################################################
436 # Manager classes.
437
438 # JSBSim pointmass manager.
439 # Moves a pointmass representing the crew member together with the view.
440 # CONSTRUCTOR:
441 # JSBSimPointmass.new(<pointmass index>);
442 #
443 # pointmass index ... The index of the pointmass : int
444 # offsets ... [x, y ,z] position in meter of the origin of the
445 # JSBSim structural frame in the 3d model frame.
446 #
447 # NOTE: Only supports aligned frames (yet).
448 #
449 var JSBSimPointmass = {
450 new : func (index, offsets = nil) {
451 var base = props.globals.getNode("fdm/jsbsim/inertia");
452 var prefix = "pointmass-location-";
453 var postfix = "-inches[" ~ index ~"]";
454 var obj = { parents : [JSBSimPointmass] };
455 obj.pos_ft =
456 [
457 base.getNode(prefix ~ "X" ~ postfix),
458 base.getNode(prefix ~ "Y" ~ postfix),
459 base.getNode(prefix ~ "Z" ~ postfix)
460 ];
461 obj.offset = (offsets == nil) ? [0.0, 0.0, 0.0] : offsets;
462 return obj;
463 },
464 update : func (walker) {
465 var pos = walker.get_pos();
466 pos[2] += walker.get_eye_height()/2;
467 forindex (var i; pos) {
468 me.pos_ft[i].setValue((pos[i] - me.offset[i])*M2FT*12);
469 }
470 }
471 };
472
473 ###############################################################################
474 # Module implementation below
475
476 var TO_RAD = math.pi/180;
477 var TO_DEG = 180/math.pi;
478
479 var NO_POS = [-9999.0, -9999.0, -9999.0];
480
481 var walkers = {};
482
483 var closerXY = func (pos, p1, p2) {
484 var l1 = [p1[0] - pos[0], p1[1] - pos[1]];
485 var l2 = [p2[0] - pos[0], p2[1] - pos[1]];
486 return (l1[0]*l1[0] + l1[1]*l1[1]) - (l2[0]*l2[0] + l2[1]*l2[1]);
487 }
488
489 var max = func (a, b) {
490 return b > a ? b : a;
491 }
492 var min = func (a, b) {
493 return a > b ? b : a;
494 }
495
496 # 2D vector math.
497 var vec2 = {
498 add : func (a, b) {
499 return [a[0] + b[0], a[1] + b[1]];
500 },
501 sub : func (a, b) {
502 return [a[0] - b[0], a[1] - b[1]];
503 },
504 mul : func (k, a) {
505 return [k * a[0], k * a[1]];
506 },
507 length : func (a) {
508 return math.sqrt(a[0]*a[0] + a[1]*a[1]);
509 },
510 dot : func (a, b) {
511 return a[0]*b[0] + a[1]*b[1];
512 },
513 normalize : func (a) {
514 var s = 1/vec2.length(a);
515 return [s * a[0], s * a[1]];
516 }
517 }