File: sfmsimforce.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (1771 lines) | stat: -rw-r--r-- 45,695 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <math.h>
#include "matrixmath.h"
#include "sfm.h"


static SFMBoolean SFMForceTouchDownCheck(
	SFMRealmStruct *realm, SFMModelStruct *model
);
#if 0
static SFMBoolean SFMForceCollisionCheck(
	SFMRealmStruct *realm, SFMModelStruct *model
);
#endif

static int SFMForceApplySlew(
	 SFMRealmStruct *realm, SFMModelStruct *model
);
int SFMForceApplyNatural(
	SFMRealmStruct *realm, SFMModelStruct *model
);
int SFMForceApplyArtificial(
	SFMRealmStruct *realm, SFMModelStruct *model
);
static int SFMForceApplyControlSlew(
	SFMRealmStruct *realm, SFMModelStruct *model
);
int SFMForceApplyControl(
	SFMRealmStruct *realm, SFMModelStruct *model
);


#define POW(x,y)        (((x) > 0.0f) ? pow(x,y) : 0.0f)

#define MAX(a,b)        (((a) > (b)) ? (a) : (b))
#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))

#define RADTODEG(r)     ((r) * 180 / PI)
#define DEGTORAD(d)     ((d) * PI / 180)


/*
 *	Checks if the object has come down and impacted the ground
 *	beyond its tolorance. Returns True if a crash occures and
 *	impact_force_coeff will be set to the coefficient value
 *	that has exceeded its touch_down_crash_resistance.
 *
 *	Inputs assumed valid.
 *
 *      Collision notify function specified on the realm will be called.
 */
static SFMBoolean SFMForceTouchDownCheck(
	SFMRealmStruct *realm, SFMModelStruct *model
)
{
	SFMBoolean status = False;
	SFMFlags		flags = model->flags;
	SFMPositionStruct	*vel = &model->velocity_vector;
	double impact_force_coeff = 0.0;

	if(flags & (SFMFlagPosition | SFMFlagVelocityVector |
	            SFMFlagTouchDownCrashResistance)
	)
	{
	    /* Vertical velocity at moment of touch down exceeding
	     * touch down crash resistance?
	     */
	    if(vel->z < -model->touch_down_crash_resistance)
		status = True;

	    /* Calculate impact of crash as a coeff (regardless if
	     * a crash occured or not.
	     */
	    if(model->touch_down_crash_resistance > 0.0)
	    {
		impact_force_coeff = (vel->z /
		    -model->touch_down_crash_resistance);
		if(impact_force_coeff < 0.0)
		    impact_force_coeff = 0.0;
	    }
	}

	/* Report FDM touch down */
	if(realm->touch_down_cb != NULL)
	    realm->touch_down_cb(
		realm, model,
		realm->touch_down_cb_client_data,
		impact_force_coeff
	    );

	/* Inputs may be invalid now, since callback might have
	 * deleted one of them
	 */

	return(status);
}

#if 0
/*
 *	*** Note this function is currently not used! ***
 *
 *	Checks for collision with another object in the realm's
 *	models list. Returns True if a crash occured.
 *
 *      Inputs assumed valid.
 *
 *	Collision notify function specified on the realm will be called.
 */
static SFMBoolean SFMForceCollisionCheck(
	SFMRealmStruct *realm, SFMModelStruct *model
)
{
	int i;
	SFMBoolean status = False;
	SFMFlags	flags = model->flags;
	int		contact_shape = model->crash_contact_shape;
	double		contact_radius = model->crashable_size_radius;
	double r, z, d;
	SFMPositionStruct	*pos = &model->position;
	SFMModelStruct *model2;


	/* Cannot crash into other? */
	if(!(flags & (SFMFlagPosition | SFMFlagCanCrashIntoOther |
		      SFMFlagCrashContactShape)
	))
	    return(status);

	if(!model->can_crash_into_other)
	    return(status);

	/* If crash contact type is cylendrical, make sure z bounds
	 * are set.
	 */
	if(contact_shape == SFMCrashContactShapeCylendrical)
	{
	    if(!(flags & (SFMFlagCrashableSizeZMin | SFMFlagCrashableSizeZMax)
	    ))
		return(status);
	}


	/* Go through all flight models on realm */
	for(i = 0; i < realm->total_models; i++)
	{
	    model2 = realm->model[i];
	    if(model2 == NULL)
		continue;
		 
	    if(!(model2->flags & (SFMFlagPosition | SFMFlagCanCauseCrash |
				  SFMFlagCrashContactShape |
				  SFMFlagCrashableSizeRadius)
	    ))
		continue;
	    if(!model2->can_cause_crash)
		continue;

	    if(model2 == model)	/* Skip itself */
		continue;

	    /* Calculate distance in meters */  
	    r = SFMHypot2(
		model2->position.x - pos->x,
		model2->position.y - pos->y
	    );
	    /* Definatly not in contact? */
	    if((r - contact_radius - model2->crashable_size_radius) > 0)
		continue;

/* Note, we need to calculate the collision impact coeff */

	    /* Our object contact shape spherical? */
	    if(contact_shape == SFMCrashContactShapeSpherical)
	    {
		/* Check target object contact shape */
		if(model2->crash_contact_shape == SFMCrashContactShapeSpherical)
		{
		    /* Spherical to spherical */

		    z = model2->position.z - pos->z;
		    d = SFMHypot2(r, z);
		    if((d - contact_radius - model2->crashable_size_radius) <= 0)
		    {
			/* Call collision notify */
			status = True;

			if(realm->collision_cb != NULL)
			    realm->collision_cb(
				realm, model, model2,
				realm->collision_cb_client_data,
				1.1
			    );

			/* Callback deleted model? */
			if(SFMModelInRealm(realm, model) < 0)
			    return(status);  
		    }
		}
		else if((model2->crash_contact_shape == SFMCrashContactShapeCylendrical) &&
			(model2->flags & (SFMFlagCrashableSizeZMin | SFMFlagCrashableSizeZMax))
		)
		{
		    /* Spherical to cylendrical */

		    z = model2->position.z;
 /* Not too accurate check on z */
		    if((pos->z >= (z + model2->crashable_size_z_min)) &&
		       (pos->z <= (z + model2->crashable_size_z_max))
		    )
		    {
			/* Call collision notify */
			status = True;

			if(realm->collision_cb != NULL)
			    realm->collision_cb(
				realm, model, model2,
				realm->collision_cb_client_data,
				1.1
			    );

			/* Callback deleted model? */
			if(SFMModelInRealm(realm, model) < 0)
			    return(status);
		    }
		}
	    }
	    /* Our object contact shape cylendrical? */
	    else if(contact_shape == SFMCrashContactShapeCylendrical)
	    {
		/* Check target object contact shape */
		if(model2->crash_contact_shape == SFMCrashContactShapeSpherical)
		{
		    /* Cylendrical to spherical */
		    z = model2->position.z;
 /* Not too accurate check on z */
		    if((z >= (pos->z + model->crashable_size_z_min)) &&
		       (z <= (pos->z + model->crashable_size_z_max))
		    )
		    {
			/* Call collision notify */
			status = True;

			if(realm->collision_cb != NULL)
			    realm->collision_cb(
				realm, model, model2,
				realm->collision_cb_client_data,
				1.1
			    );

			/* Callback deleted model? */
			if(SFMModelInRealm(realm, model) < 0)
			    return(status);
		    }
		}
		else if((model2->crash_contact_shape == SFMCrashContactShapeCylendrical) &&
			(model2->flags & (SFMFlagCrashableSizeZMin | SFMFlagCrashableSizeZMax))
		)
		{
		    /* Cylendrical to cylendrical */
		    double z1_min = pos->z + model->crashable_size_z_min,
			   z1_max = pos->z + model->crashable_size_z_max;
		    double z2_min = model2->position.z + model2->crashable_size_z_min,
			   z2_max = model2->position.z + model2->crashable_size_z_max;

		    if((z1_min <= z2_max) &&
		       (z2_min <= z1_max)
		    )
		    {
			/* Call collision notify */
			status = True;

			if(realm->collision_cb != NULL)
			    realm->collision_cb(
				realm, model, model2,
				realm->collision_cb_client_data,
				1.1
			    );

			/* Callback deleted model? */
			if(SFMModelInRealm(realm, model) < 0)
			    return(status);
		    }
		}
	    }
	}
		
	return(status);
}
#endif

/*
 *	Applies slew forces to the FDM from its current control
 *	positions.
 *
 *	This function should be called from SFMForceApplyArtificial().
 */
static int SFMForceApplySlew(
	 SFMRealmStruct *realm, SFMModelStruct *model
)
{
	double time_compensation = realm->time_compensation;
	double time_compression = realm->time_compression;
	SFMFlags		flags = model->flags;
	double          h_con_coeff = CLIP(
				model->heading_control_coeff, -1.0, 1.0
	);
	double          p_con_coeff = CLIP(
				model->pitch_control_coeff, -1.0, 1.0
	);
	double          b_con_coeff = CLIP(
				model->bank_control_coeff, -1.0, 1.0
	);
	SFMDirectionStruct      *dir = &model->direction;
	SFMPositionStruct       *pos = &model->position;
	double			ground_elevation_msl = model->ground_elevation_msl;


	if((flags & SFMFlagPosition) &&
	   (flags & SFMFlagDirection) &&
	   (flags & SFMFlagGroundElevation) &&
	   (flags & SFMFlagHeadingControlCoeff) &&
	   (flags & SFMFlagBankControlCoeff) &&
	   (flags & SFMFlagPitchControlCoeff) &&
	   (flags & SFMFlagThrottleCoeff)
	)
	{
	    double a[3], r[3];
	    double b_con_coeff_p3 = b_con_coeff * b_con_coeff * b_con_coeff;
	    double p_con_coeff_p3 = p_con_coeff * p_con_coeff * p_con_coeff;


	    /* Calculate change in position relative to FDM's current
	     * control positions (not the FDM's current velocity)
	     *
	     * Because for slew, the controller positions dictate the
	     * velocity (instead of velocity dictating velocity)
	     */
	    a[0] = (b_con_coeff_p3 * 10000.0) *
		time_compensation * time_compression;
	    a[1] = (p_con_coeff_p3 * 10000.0) *
		time_compensation * time_compression;
	    a[2] = 0.0;

	    /* Rotate position change so that it is relative to world 
	     * cooridnates.
	     */
	    MatrixRotateHeading3(a, dir->heading, r);

	    /* Move position */
	    pos->x += r[0];
	    pos->y += r[1];
	    pos->z += r[2];

	    /* Keep above ground level */
	    if(pos->z < ground_elevation_msl)
		pos->z = ground_elevation_msl;

	    /* Turn */
	    dir->heading = SFMSanitizeRadians(
		dir->heading + (h_con_coeff * (0.5 * PI) *
		time_compensation * time_compression)
	    );

	    /* Update internally maintained ground to center of object
	     * height.
	     */
	    model->center_to_ground_height = ground_elevation_msl - pos->z;
	}

	return(0);
}


/*
 *	Applies natural forces to the FDM:
 *
 *	* Air/ground drag & attitude leveling
 *
 *	* Recalculate ground to center height
 *
 *	* Callbacks will be called when needed.
 *
 *	Returns non-zero if the FDM was deleted.
 *
 *	Inputs assumed valid.
 */
int SFMForceApplyNatural(
	SFMRealmStruct *realm, SFMModelStruct *model
)
{
	double drag_net = 0.0;
	double tc_min = MIN(realm->time_compensation, 1.0);
	double time_compensation = realm->time_compensation;
	double time_compression = realm->time_compression;
	SFMFlags		flags = model->flags;
	SFMDirectionStruct	*dir = &model->direction;
	SFMPositionStruct	*pos = &model->position,
				*vel = &model->velocity_vector;
	SFMBoolean		gear_state = model->gear_state;
	int			gear_type = model->gear_type;
	double			ground_elevation_msl = model->ground_elevation_msl;


	/* Flight model type must be defined */
	if(!(flags & SFMFlagFlightModelType))
	    return(0);

	/* If in slew mode, then skip applying natural forces */
	if(model->type == SFMFlightModelSlew)
	    return(0);


	/* Update internally maintained ground to center of object height */
	if(flags & (SFMFlagPosition | SFMFlagGroundElevation))
	{
	    model->center_to_ground_height = ground_elevation_msl - pos->z;
	}
	else
	{
	    model->center_to_ground_height = 0.0;
	}


	/* Handle air friction caused drag by flight model type */
	switch(model->type)
	{
	  case SFMFlightModelAirplane:
	    if(flags & (SFMFlagPosition | SFMFlagDirection |
			SFMFlagVelocityVector | SFMFlagSpeed |
			SFMFlagSpeedMax | SFMFlagDragMin |
			SFMFlagLandedState | SFMFlagEnginePower)
	    )
	    {
		/* Airplane flight model */
		double prev_speed = model->speed;
		double speed_change_coeff;

		double pitch_drop_coeff, pitch_raise_coeff;
		/* Pitch change rates in radians per cycle */
		double pitch_drop_rate = (0.25 * PI);
		double pitch_raise_rate = (0.075 * PI);


		/* Calculate pitch drop coeff, independant of stall coeff
		 * even though pitch_drop_coeff is based on the stall
		 * coeff. pitch_drop_coeff is calculated differently.
		 * it reaches 0.0 at overspeed_expected instead of the
		 * speed_stall.
		 */
		if(model->speed > model->speed_stall)
		{
		    double sdc = 0.10;	/* Drop coeff at stall thres */
		    double sm = model->overspeed_expected - model->speed_stall;
		    double sc = model->speed - model->speed_stall;

		    /* Calculate pitch drop */
		    if(sm > 0.0)
			pitch_drop_coeff = (1.0 - MIN(sc / sm, 1.0)) *
			    sdc;
		    else
			pitch_drop_coeff = sdc;

		    /* Calculate pitch raise, 0.0 at stall, 1.0 at
		     * overspeed_expected.
		     */
		    if(sm > 0.0)
			pitch_raise_coeff = MIN(sc / sm, 1.0) * 
			    cos(dir->bank);
		    else
			pitch_raise_coeff = 0.0;
		}
		else
		{
		    double sdc = 0.10;	/* Drop coeff at stall thres */
		    double sm = model->speed_stall;
		    double sc = model->speed;

		    /* Calculate pitch drop */
		    if(sm > 0.0)
			pitch_drop_coeff = ((1.0 - MIN(sc / sm, 1.0)) *
			    (1.0 - sdc)) + sdc;
		    else
			pitch_drop_coeff = 1.0;

		    /* Never pitch raise while below stall speeds */
		    pitch_raise_coeff = 0.0;
		}


		/* Calculate net drag (air frame drag) based on the engine
		 * power and the current speed relative to its maximum
		 * speed.  As current speed approaches the maximum speed,
		 * the net drag (airframe drag) approaches the engine power
		 * (which should counteract all engine appied speed).
		 * Note that the net drag must always remain at or above
		 * the minimum drag.
		 */
		if(model->speed_max > 0.0f)
		    drag_net = MAX(
			model->engine_power *
			POW(model->speed / model->speed_max, 1.7f),
			model->drag_min
		    ) * tc_min;

		/* Add air brakes to net drag if deployed */
		if(flags & (SFMFlagAirBrakesState | SFMFlagAirBrakesRate))
		{
		    if(model->air_brakes_state)
			drag_net += model->air_brakes_rate * tc_min;
		}


		/* Reduce speed due to net drag (airframe drag) */
		model->speed -= drag_net;

		/* Increase speed if pitched down due to gravity or
		 * decrease speed if pitched up due to gravity. In either
		 * gravity has its greatest affect with pitched directly
		 * up or down but gravity has minimal affect when pitched
		 * level.
		 */
		model->speed += sin(dir->pitch) * realm->gravity * tc_min;

		/* Keep speed zero or positive */
		if(model->speed < 0.0)
		    model->speed = 0.0;


		/* Calculate the amount of speed changed from the above
		 * drag and gravity calculations, this will be applied
		 * to the velocity vector.
		 */
		if(prev_speed > 0.0)
		    speed_change_coeff = (model->speed / prev_speed);
		else
		    speed_change_coeff = 1.0;

		/* Add speed change to velocity vector */
		vel->x *= speed_change_coeff;	/* Always 0.0 */
		vel->y *= speed_change_coeff;
		vel->z *= speed_change_coeff;


		/* Apply pitch drop? */
		if((pitch_drop_coeff > 0.0) &&
		   (flags & SFMFlagAttitudeLevelingRate)
		)
		{
		    double prev_angle = dir->pitch;

		    /* Check if landed */
		    if(model->landed_state)
		    {
			/* On the ground pitch leveling */
			if(dir->pitch > (1.0 * PI))
			{
			    /* Pitch is currently up, so pitch down */
			    dir->pitch = SFMSanitizeRadians(
				dir->pitch + (pitch_drop_rate * time_compensation *
				time_compression * pitch_drop_coeff)
			    );
			    if((dir->pitch < prev_angle) &&
			       (dir->pitch > (0.0 * PI))
			    )
				dir->pitch = (0.0 * PI);
			}
			else
			{
			    /* Landed on ground and pitch is negative,
			     * so pitch back to ground level.
			     */
			    dir->pitch = (0.0 * PI);
			}
		    }
		    else
		    {
			/* In flight pitch drop */
			if(dir->pitch > (1.0 * PI))
			{
			    /* Pitch is currently up, so pitch down */
			    dir->pitch = SFMSanitizeRadians(
				dir->pitch + (pitch_drop_rate * time_compensation *
				time_compression * pitch_drop_coeff)
			    );
			    if((dir->pitch < prev_angle) &&
			       (dir->pitch > (0.5 * PI))
			    )
				dir->pitch = (0.5 * PI);
			}
			else
			{
			    /* Pitch is currently down, keep pitching down */
			    dir->pitch = SFMSanitizeRadians(
				dir->pitch + (pitch_drop_rate * time_compensation *
				time_compression * pitch_drop_coeff)
			    );
			    if(dir->pitch > (0.5 * PI))
				dir->pitch = (0.5 * PI);
			}
		    }
		}
		/* Apply pitch raise? Note that this will negate some
		 * of the pitch drop applied above. Also pitch raise
		 * can be negative.
		 */
		if((pitch_raise_coeff != 0.0) &&
		   (flags & SFMFlagAttitudeLevelingRate)
		)
		{
		    dir->pitch = SFMSanitizeRadians(
			dir->pitch - (pitch_raise_rate *
			time_compensation * time_compression *
			pitch_raise_coeff)
		    );
		    /* Pitch flipping check */
		    if((dir->pitch > (0.5 * PI)) &&
		       (dir->pitch < (1.5 * PI))
		    )
		    {
			dir->heading = SFMSanitizeRadians(dir->heading + PI);
			dir->bank = SFMSanitizeRadians(dir->bank + PI);
			if(dir->pitch > (1.0 * PI))
			    dir->pitch = SFMSanitizeRadians(
				(2.0 * PI) - dir->pitch + PI
			    );
			else
			    dir->pitch = SFMSanitizeRadians(
				PI - dir->pitch
			    );
		    }
		}
	    }
	    break;

	  /* ***************************************************** */
	  case SFMFlightModelHelicopter:
	    if(flags & (SFMFlagPosition | SFMFlagDirection |
			SFMFlagVelocityVector | SFMFlagSpeed |
			SFMFlagSpeedMax | SFMFlagDragMin |
			SFMFlagLandedState | SFMFlagEnginePower)
	    )
	    {
		/* Helicopter flight model */
		double prev_angle;


		/* Calculate net drag */
		if(model->speed_max > 0.0f)
		    drag_net = MAX(
			model->engine_power *
			POW(model->speed / model->speed_max, 1.7f),
			model->drag_min
		    ) * tc_min;

		/* Add air brakes to net drag if deployed */
		if(flags & (SFMFlagAirBrakesState | SFMFlagAirBrakesRate))
		{
		    if(model->air_brakes_state)
			drag_net += model->air_brakes_rate * tc_min;
		}


		/* Apply sideways drag */
		if(vel->x < 0.0)
		{
		    vel->x += drag_net;
		    if(vel->x > 0.0)
		        vel->x = 0.0;
		}
		else
		{
		    vel->x -= drag_net;
		    if(vel->x < 0.0)
			vel->x = 0.0;
		}

		/* Apply forwards/backwards drag */
		if(vel->y < 0.0)
		{
		    vel->y += drag_net;
		    if(vel->y > 0.0)
			vel->y = 0.0;
		}   
		else
		{
		    vel->y -= drag_net;
		    if(vel->y < 0.0)
			vel->y = 0.0;
		}

		/* Set new current speed based on drag applied IJ plane
		 * velocities
		 */
		model->speed = SFMHypot2(vel->x, vel->y);

		/* Helicopter flight model landed? */
		if((model->landed_state) &&
		   (flags & SFMFlagAttitudeLevelingRate)
		)
		{
		    /* Landed so apply ground leveling */
		    double	pitch_change_rate = DEGTORAD(3.0),
				pitch_leveling_rate;
		    double	pitch_up_max = (2.0 * PI) - pitch_change_rate,
				pitch_down_max = (0.0 * PI) + pitch_change_rate;

		    prev_angle = dir->pitch;	/* Record pitch */

		    /* Begin applying pitch ground leveling */

		    /* Check if pitch is outside the controller affected
		     * pitch range
		     */
		    if((dir->pitch < pitch_up_max) &&
		       (dir->pitch > pitch_down_max)
		    )
		    {
			/* Apply rough pitch leveling */
			pitch_leveling_rate = DEGTORAD(90.0);

			/* Pitched up? */
			if(dir->pitch > (1.0 * PI))
			{
			    /* Pitched up so need to pitch down */
			    dir->pitch += pitch_leveling_rate *
				time_compensation * time_compression;
			    if(dir->pitch > pitch_up_max)
				dir->pitch = pitch_up_max;
			}
			else
			{
			    /* Pitched down so need to pitch up */
			    dir->pitch -= pitch_leveling_rate * 
				time_compensation * time_compression;
			    if(dir->pitch < pitch_down_max)
				dir->pitch = pitch_down_max;
		        }
		    }
		    /* Apply mild pitch leveling */
		    pitch_leveling_rate = DEGTORAD(2.0);
		    /* Pitched up? */
		    if(dir->pitch > (1.0 * PI))
		    {
			/* Pitched up so need to pitch down */
			dir->pitch += pitch_leveling_rate *
			    time_compensation * time_compression;
			if(dir->pitch  > (2.0 * PI))
			    dir->pitch = (0.0 * PI);
		    }
		    else
		    {
			/* Pitched down so need to pitch up */
			dir->pitch -= pitch_leveling_rate *
			    time_compensation * time_compression;
			if(dir->pitch < (0.0 * PI))  
			    dir->pitch = (0.0 * PI);
		    }
		}
		/* Helicopter flight model in flight pitch attitude
	         * leveling (bank leveling will be applied later).
		 */
		else if(flags & SFMFlagAttitudeLevelingRate)
		{
		    double leveling_coeff;

		    if(dir->pitch > (1.5 * PI))
		    {
			leveling_coeff = MIN(
			    (dir->pitch - (1.5 * PI)) / (0.5 * PI),
			    1.0
			);
			dir->pitch = MIN(
			    dir->pitch + (leveling_coeff *
			    model->attitude_leveling_rate.pitch *
				time_compensation * time_compression),
			    2.0 * PI
			);
		    }
		    else if(dir->pitch < (0.5 * PI))
		    {
			leveling_coeff = MAX(
			    ((0.5 * PI) - dir->pitch) / (0.5 * PI),
			    0.0
			);
			dir->pitch = MAX(
			    dir->pitch - (leveling_coeff *
			    model->attitude_leveling_rate.pitch *
				time_compensation * time_compression),
			    0.0 * PI
			);
		    }
		}
	    }
	    break;
	}

	/* ******************************************************** */
	/* Apply ground drag and levelings regardless of flight
	 * model type.
	 */
	if(flags & (SFMFlagPosition | SFMFlagDirection |
		    SFMFlagVelocityVector | SFMFlagSpeed |
		    SFMFlagSpeedMax | SFMFlagDragMin |
		    SFMFlagLandedState | SFMFlagEnginePower)
	)
	{
	    /* Check if landed */
	    if(model->landed_state)
	    {
		/* Is landed so handle ground friction caused drag plus
		 * pitch and bank ground leveling
		 */
		double prev_angle;
		double x_vel_ground_drag = (5.0 * tc_min);	/* Meters per cycle */


		/* Applying strong ground drag if landing gear is not
		 * down or gear type is not wheels.
		 */
		if((gear_type != SFMGearTypeWheels) ||
		   !gear_state
		)
		{
		    /* Landing gear up or landing gear is not of
		     * type SFMGearTypeWheels.
		     */
		    if(vel->x < 0.0)
		    {
			vel->x += x_vel_ground_drag;
			if(vel->x > 0.0)
			    vel->x = 0.0;
		    }
		    else
		    {
			vel->x -= x_vel_ground_drag;
			if(vel->x < 0.0) 
			    vel->x = 0.0;
		    }

		    /* Use x velocity ground drag for y axis */
		    if(vel->y < 0.0)
		    {
			vel->y += x_vel_ground_drag;
			if(vel->y > 0.0)
			    vel->y = 0.0;
		    }
		    else
		    {   
			vel->y -= x_vel_ground_drag;
			if(vel->y < 0.0)
			    vel->y = 0.0;
		    }

		    /* Do not modify z velocity */

		    /* Update speed */
		    model->speed = SFMHypot2(vel->x, vel->y);
		}
		else
		{
		    /* Landed and on wheels, apply drag to just the
		     * I compoent direction.
		     */

		    if(vel->x < 0.0)
		    {
			vel->x += x_vel_ground_drag;
			if(vel->x > 0.0)
			    vel->x = 0.0;
		    }
		    else
		    {
			vel->x -= x_vel_ground_drag;
			if(vel->x < 0.0)
			    vel->x = 0.0;
		    }

		    /* Do not modify z velocity */

		    /* Update speed */
		    model->speed = SFMHypot2(vel->x, vel->y);
		}

		/* Apply ground bank leveling? */
		if(flags & SFMFlagAttitudeLevelingRate)
		{
		    prev_angle = dir->bank;
		    if(dir->bank > (1.0 * PI))
		    {
		        dir->bank = SFMSanitizeRadians(
			    dir->bank + (1.5 * time_compensation *
			    time_compression)
		        );
		        if(dir->bank < prev_angle)
			    dir->bank = (0.0 * PI);
		        else if(dir->bank < (1.0 * PI))
			    dir->bank = (0.0 * PI);
		    }
		    else
		    {
		        dir->bank = SFMSanitizeRadians(
			    dir->bank - (1.5 * time_compensation *
			    time_compression)
		        );
		        if(dir->bank > prev_angle)
			    dir->bank = (0.0 * PI);
		        else if(dir->bank > (1.0 * PI))
			    dir->bank = (0.0 * PI);
		    }
		}
	    }
	    else	/* Check if landed */
	    {
		/* Not landed so in flight */
		double leveling_coeff;

		/* Apply in flight bank leveling? */
		if(flags & SFMFlagAttitudeLevelingRate)
		{
		    if(dir->bank < (1.0 * PI))
		    {
		        leveling_coeff = MAX(
			    1.0 - (dir->bank / (0.5 * PI)), 0.0
		        );
		        dir->bank = MAX(
			    dir->bank - (leveling_coeff *
			    model->attitude_leveling_rate.bank *
			        time_compensation * time_compression),
			    0.0
		        );
		    }
		    else
		    {
		        leveling_coeff = MAX(
			    1.0 - (((2.0 * PI) - dir->bank) / (0.5 * PI)),
			    0.0
			);
		        dir->bank = dir->bank + (leveling_coeff * 
			    model->attitude_leveling_rate.bank *
			    time_compensation * time_compression
		        );
		        if(dir->bank >= (2.0 * PI))
			    dir->bank = 0.0 * PI;
		    }
		}
	    }	/* In flight */
	}


	return(0);
}

/*
 *	Apply artificial forces; thrust and brakes.
 *
 *	If model is in SFMFlightModelSlew mode then this function will
 *	call the appropriate slew function.
 *
 *	Notify callbacks may be called, return will be non-zero if the
 *	model was deleted and/or no longer valid.
 *
 *	Inputs assumed valid.
 */
int SFMForceApplyArtificial(
	SFMRealmStruct *realm, SFMModelStruct *model
)
{
	double theta, r;
	double di = 0.0, dj = 0.0, dk = 0.0;
	double dic = 0.0, djc = 0.0, dkc = 0.0;
	double thrust_output = 0;
	double net_mass = 0.0;		/* In kg */
	double net_weight;		/* net_mass * SAR_GRAVITY */
	double	ground_elevation_msl = model->ground_elevation_msl,
		center_to_gear_height = 0.0;
	double tc_min = MIN(realm->time_compensation, 1.0);
	double time_compensation = realm->time_compensation;
	double time_compression = realm->time_compression;
	SFMFlags                flags = model->flags;
	SFMDirectionStruct      *dir = &model->direction;
	SFMPositionStruct       *pos = &model->position,
				*vel = &model->velocity_vector,
				*ar = &model->accel_responsiveness;
	SFMBoolean              gear_state = model->gear_state;
	int                     gear_type = model->gear_type;


	/* Flight model type must be defined */
	if(!(flags & SFMFlagFlightModelType))
	    return(0);

	/* Check if in slew mode, if so then apply slew forces */
	if(model->type == SFMFlightModelSlew)
	    return(SFMForceApplySlew(realm, model));

	/* Calculate center to gear height */
	if(flags & (SFMFlagBellyHeight | SFMFlagGearState |
		    SFMFlagGearType | SFMFlagGearHeight)
	)
	{
	    center_to_gear_height = model->belly_height + (
		(gear_state) ? model->gear_height : 0.0
	    );
	}

	/* Calculate net mass of object in kg */
	if(flags & SFMFlagTotalMass)
	    net_mass = model->total_mass;
	if(net_mass < 0.0) 
	    net_mass = 0.0;

	/* Calculate net weight of object (gravity in cycles) */
	net_weight = net_mass * realm->gravity;


	/* Calculate thrust output based on altitude and throttle */
	if(flags & (SFMFlagPosition | SFMFlagServiceCeiling |
		    SFMFlagThrottleCoeff | SFMFlagEnginePower)
	)
	{
	    double height_coeff;

	    if(model->service_ceiling > 0.0)
		height_coeff = (pos->z / model->service_ceiling);
	    else
		height_coeff = 0.0;

	    thrust_output = model->throttle_coeff *
		model->engine_power * MAX(
		    1.0 - height_coeff, 0.0
		);
	    if(thrust_output < 0.0)
		thrust_output = 0.0;

	    /* Add afterburner to thrust? */
	    if(flags & (SFMFlagAfterBurnerState | SFMFlagAfterBurnerPowerCoeff))
	    {
		if(model->after_burner_state)
		{
		    thrust_output += ((model->after_burner_power_coeff *
			model->engine_power) * MAX(
			    1.0 - height_coeff, 0.0
			));
		}
	    }
	}

	/* Check if current speed is exceeding its maximum expected
	 * speed (overspeed).
	 */
	if(flags & (SFMFlagSpeedMax | SFMFlagSpeed))
	{
	    /* Current speed greater than expected overspeed? */
	    if(model->speed > model->overspeed_expected)
	    {
		if(realm->overspeed_cb != NULL)
		    realm->overspeed_cb(
			realm,
			model,
			realm->overspeed_cb_client_data,
			model->speed,
			model->overspeed_expected,
			model->overspeed
		    );

		/* Check if model is still valid */
		if(SFMModelInRealm(realm, model) < 0)
		    return(1);
	    }
	}


	/* Handle air friction caused drag by flight model type */
	switch(model->type)
	{
	  case SFMFlightModelAirplane:
	    /* Airplane flight model */
	    if(flags & (SFMFlagPosition | SFMFlagDirection |
			SFMFlagVelocityVector | SFMFlagSpeed |
			SFMFlagSpeedStall | SFMFlagSpeedMax |
			SFMFlagAccelResponsiveness | SFMFlagLandedState)
	    )
	    {
	        double	speed_coeff,	/* Inverse speed coeff, where 1.0
					 * is at standing still and 0.0
					 * is at maximum speed.
					 */
			stall_coeff;	/* The stall coeff, 1.0 is max
					 * stall and 0.0 is no stall.
					 */
	        double	prev_speed;
		/* Velocity sin and cos of pitch and bank, they come
		 * from attitude but are later adjusted for velocity
		 * direction.
		 */
		double sin_pitch, cos_pitch, sin_bank, cos_bank;
		/* New velocity applied by aircraft attitude and thrust
		 * to be added to current velocity.
		 */
		double vel_thrust_mag;
		SFMPositionStruct vel_thrust;


		/* Record previous speed */
		prev_speed = model->speed;

		/* Calculate speed coeff, where 1.0 is standing still
		 * and 0.0 is at maximum.
		 */
		if(model->speed_max > 0.0)
		    speed_coeff = MAX(
			1.0 - (model->speed / model->speed_max),
			0.0
		    );
		else
		    speed_coeff = 0.0;

		/* Update current stall coefficent. 1.0 is highest
		 * stall and 0.0 is no stall.
		 */
		model->stall_coeff = SFMStallCoeff(
		    model->speed,
		    model->speed_stall,
		    model->speed_max
		);
		stall_coeff = model->stall_coeff;


		/* Calculate new pitch and bank trig values */
		sin_pitch = sin(dir->pitch);
		cos_pitch = cos(dir->pitch);
		sin_bank = sin(dir->bank);
		cos_bank = cos(dir->bank);

		/* Calculate heading change caused by bank, the
		 * heading change becomes minimal when the speed reaches
		 * the maximum and heading change is 0.04 * PI radians
		 * per cycle when the speed is at minimum.
		 */
		if(!model->landed_state)
		    dir->heading = SFMSanitizeRadians(
			dir->heading + (sin_bank * speed_coeff *
			(0.03 * PI) * time_compensation *
		        time_compression)
		    );

		/* Calculate new thrust vector in meters per cycle,
		 * include tc_min. Use acceleration responsiveness'
		 * j compoent.
		 */
		vel_thrust_mag = thrust_output * tc_min / MAX(ar->y, 1.0);
		vel_thrust.x = 0.0 * vel_thrust_mag;
		vel_thrust.y = cos_pitch * vel_thrust_mag;
		vel_thrust.z = -sin_pitch * vel_thrust_mag;

		/* Add thrust velocity to current velocity */
		vel->x = (0.0 * prev_speed) + vel_thrust.x;
		vel->y = (cos_pitch * prev_speed) + vel_thrust.y;
		vel->z = (-sin_pitch * prev_speed) + vel_thrust.z;


		/* Calculate stalling velocity */
		vel->z -= model->speed_stall *
		    MIN(stall_coeff, 0.25) / MAX(ar->z, 1.0);


		/* Re-update speed based on velocity vector compoents
		 * (ignore x velocity vector compoent for airplane models).
		 */
		if(model->landed_state)
		    model->speed = vel->y;
		else
		    model->speed = SFMHypot2(vel->y, vel->z);

/*
printf(" Y:%f Z:%f  new Y:%f Z%f  S:%f  %f\r",
 vel->y, vel->z, vel_thrust.y, vel_thrust.z, obj_helicopter_ptr->speed,
 ar->z
);
fflush(stdout);
 */

		/* Calculate new x y position */
		r = vel->y * time_compensation * time_compression;
		theta = dir->heading;
		pos->x += r * sin(theta);
		pos->y += r * cos(theta);

		/* Calculate new z position (but it will actually be
		 * set farther below).
		 */
		dkc = vel->z * time_compensation * time_compression;
	    }
	    break;

	  /* **************************************************** */
	  case SFMFlightModelHelicopter:
	    /* Helicopter flight model */
	    if(flags & (SFMFlagPosition | SFMFlagDirection |
			SFMFlagVelocityVector | SFMFlagSpeed |
			SFMFlagSpeedStall | SFMFlagSpeedMax |
			SFMFlagAccelResponsiveness | SFMFlagLandedState)
	    )
	    {
		double	sin_pitch = sin(dir->pitch),
			cos_pitch = cos(dir->pitch),
			sin_bank = sin(dir->bank),
			cos_bank = cos(dir->bank);

		/* To calculate movement offset, use the equation:
		 *
		 * (last_vel / compilation) + ((thrust_in_direction) / response)
		 *
		 * Compilation is a value from 1 to infinity, the closer
		 * it is to 1, the stronger the velocity compilation.
		 *
		 * Response is how sensitive the thrust (force) is
		 * applied, lower values increase the responsiveness.
		 */

		/* Heading by pitch and bank caused by thrust direction.
		 * When the helicopter pitches forward and banks, the
		 * heading will then change.
		 */
		if(!model->landed_state)
		    dir->heading = SFMSanitizeRadians(
			dir->heading + (sin_pitch * sin_bank *
			    (0.2 * PI) *
			    time_compensation * time_compression)
		    );


	        /* XY Plane: Pitch and bank applied force */

	        /* Calculate i force compoent (obj relative) */
	        di = (vel->x / 1.00) +
		    (thrust_output * sin_bank * cos_pitch *
		    tc_min / MAX(ar->x, 1.0));
		dic = di * time_compensation * time_compression;
		vel->x = di;

		/* Calculate j force compoent (obj relative) */
		dj = (vel->y / 1.00) +
		    (thrust_output * sin_pitch * cos_bank *
		    tc_min / MAX(ar->y, 1.0));
		djc = dj * time_compensation * time_compression;
		vel->y = dj;

		/* Rotate direction with object's scene heading */
		theta = SFMSanitizeRadians((PI / 2) -
		    atan2(dj, di) + dir->heading
		);
		/* Calculate speed on the x y velocity vector plane */
		model->speed = SFMHypot2(di, dj);

		/* Calculate new position */
		r = model->speed * time_compensation * time_compression;
		pos->x += r * sin(theta);
		pos->y += r * cos(theta);


		/* Vertical velocity */
/*
		dk = (vel->z / 1.60) +
		    (((thrust_output * cos_pitch * cos_bank * tc_min) -
		    (net_weight * tc_min))
		    / MAX(ar->z, 1.0));
 */
/*
		dk = (vel->z / (1.0 + (3.0 * tc_min))) +
		    (((thrust_output * cos_pitch * cos_bank * tc_min) -
		    (net_weight * tc_min))
		    / MAX(ar->z, 1.0));
 */
		if(1)
		{
		    /* Better solution for vertical velocity,
		     * but with one small problem, with good frame
		     * rates, tc_min will be really small which makes
		     * the convergence to dk_new take longer than
		     * it would if tc_min was big (poor frame rates).
		     */
		    double dk_prev, dk_new, dk_dv;

		    dk_prev = vel->z;
		    dk_new = ((thrust_output * cos_pitch * cos_bank) -
			(net_weight)) / MAX(ar->z, 1.0);
		    dk_dv = dk_new - dk_prev;

		    /* Add the previous velocit to the new delta change
		     * in velocity multiplied by the time compensation 
		     * min. Make sure tc_min stays about 0.01 to ensure
		     * that as this function cycles that it will converge
		     * to dk.
		     */
		    dk = dk_prev + (dk_dv * MAX(tc_min / 1.0, 0.01));
		}
		/* No negative velocity if landed */
		if((dk < 0.0) && model->landed_state)
		    dk = 0.0;

		dkc = dk * time_compensation * time_compression;
		vel->z = dk;
	    }
	    break;
	}

	/* ************************************************************ */
	/* Set z position for all flight model types (note that
	 * dkc was calculated above).
	 */
	if(flags & (SFMFlagPosition | SFMFlagDirection |
		    SFMFlagVelocityVector | SFMFlagSpeed |
		    SFMFlagGroundElevation | SFMFlagServiceCeiling |
		    SFMFlagBellyHeight | SFMFlagLandedState)
	)
	{
	    /* Adjust vertical position (take land into account).
	     * Variable dkc should be properly calculated from above.
	     */
	    double ground_to_center_height;
	    double prev_z = pos->z;	/* Record previous z position */

	    /* Now set new Z position */
	    pos->z += dkc;


	    /* Contact with ground? */
	    ground_to_center_height = ground_elevation_msl + center_to_gear_height;
	    if(pos->z <= ground_to_center_height)
	    {
		/* Reset z position (leave z velocity alone) */
/*              vel->z = 0.0; */
		pos->z = ground_to_center_height;

		/* Check if currently marked `landed'? */
		if(model->landed_state)
		{
		    /* Already landed, do nothing */
		}
		else
		{
		    /* Was previously not landed */
		    model->landed_state = True;

		    /* Just touched down? */
		    if(prev_z > ground_to_center_height)
		    {
			/* Calculate touch down impact force and
			 * and call touch down notify function.
			 */
			SFMForceTouchDownCheck(
			    realm, model
			);
		    }
		    else
		    {
			/* Was previously `under the ground' but not
			 * landed. So we do not call SFMForceTouchDownCheck()
			 * because we know that the impact force is 0.0.
			 * So just call touch down callback with a impact
			 * force of 0.0.
			 */
			if(realm->touch_down_cb != NULL)
			    realm->touch_down_cb(
				realm, model,
				realm->touch_down_cb_client_data,
				0.0		/* Impact force coeff */
			    );
		    }

		    /* Check if model is still valid */
		    if(SFMModelInRealm(realm, model) < 0)
			return(1);
		}
	    }
	    else
	    {
		/* Is currently above ground */

		/* Check if above ground plus tolorance (0.05 meters) */
		if((pos->z - 0.05) > ground_to_center_height)
		{
		    /* Very much above ground, so mark as not landed */
		    if(model->landed_state)
		    {
			/* Mark as not landed */
			model->landed_state = False;

			/* Call airborne callback */
			if(realm->airborne_cb != NULL)
			    realm->airborne_cb(
				realm,
				model,
				realm->airborne_cb_client_data
			    );
			/* Check if model is still valid after callback */
			if(SFMModelInRealm(realm, model) < 0)
			    return(1);
		    }
		}

		/* Currently marked landed but velocity going up? */
		if(model->landed_state && (vel->z > 0.0))
		{
		    /* Mark as not landed */
		    model->landed_state = False;

		    /* Call airborne callback */
		    if(realm->airborne_cb != NULL)
			realm->airborne_cb(
			    realm,
			    model,
			    realm->airborne_cb_client_data
			);
		    /* Check if model is still valid after callback */
		    if(SFMModelInRealm(realm, model) < 0)
			return(1);
		}
	    }


	    /* Air brakes are applied in SFMForceApplyNatural() */


	    /* Apply wheel brakes if landed */
	    if(flags & (SFMFlagGearState | SFMFlagGearType |
			SFMFlagGearBrakesState)
	    )
	    {
		if(model->landed_state &&
		   gear_state && (gear_type == SFMGearTypeWheels) &&
		   model->gear_brakes_state
		)
		{
		    double wheel_brakes_power = (5.0 * tc_min);	/* Meters per second */

		    if(vel->y < 0)
		    {
			vel->y += wheel_brakes_power;
			if(vel->y > 0) 
			    vel->y = 0;
		    }
		    else
		    {
			vel->y -= wheel_brakes_power;
			if(vel->y < 0)
			    vel->y = 0;
		    }

		    model->speed = SFMHypot2(vel->x, vel->y);
		}
	    }
	}

	return(0);
}


/*
 *	Applies control positions for slew mode.
 *
 *	This function should be called from SFMForceApplyControl().
 */
static int SFMForceApplyControlSlew(
	SFMRealmStruct *realm, SFMModelStruct *model
)
{
	/* Changing of control positions do not affect slew */
	return(0);
}

/*
 *	Apply flight model control positions to change model's
 *	attitude (direction).
 *
 *	Notify callbacks may be called, return will be non-zero if the
 *	model was deleted and/or no longer valid.
 *
 *	Inputs assumed valid.
 */
int SFMForceApplyControl(
	SFMRealmStruct *realm, SFMModelStruct *model
)
{
	double	time_compensation = realm->time_compensation,
		time_compression = realm->time_compression;

	SFMFlags	flags = model->flags;
	SFMDirectionStruct	*dir = &model->direction;
	SFMPositionStruct	*vel = &model->velocity_vector;
	SFMDirectionStruct	*acr = &model->attitude_change_rate;
	double		h_con_coeff = CLIP(
				model->heading_control_coeff, -1.0, 1.0
	);
	double          p_con_coeff = CLIP(
				model->pitch_control_coeff +
				model->elevator_trim_coeff, -1.0, 1.0
	);
	double          b_con_coeff = CLIP(
				model->bank_control_coeff, -1.0, 1.0
	);

	SFMDirectionStruct prev_dir;


	/* Direction and all control positions must be defined */
	if(!(flags & (SFMFlagFlightModelType | SFMFlagDirection |
		      SFMFlagVelocityVector | SFMFlagSpeed |
		      SFMFlagHeadingControlCoeff | SFMFlagBankControlCoeff |
		      SFMFlagPitchControlCoeff)
	))
	    return(0);


	/* If in slew mode, skip control handling */
	if(model->type == SFMFlightModelSlew)
	    return(SFMForceApplyControlSlew(realm, model));


	/* Record previous attitude */
	memcpy(&prev_dir, dir, sizeof(SFMDirectionStruct));


	/* Handle by flight model type */
	switch(model->type)
	{
	  case SFMFlightModelAirplane:
	    /* Airplane flight model */

	    break;


	}


	/* Landed state defined? */
	if(flags & SFMFlagLandedState)
	{
	    /* Is landed? */
	    if(model->landed_state)
	    {
		/* Landed */
		double theta;


		/* Bank by bank (helicopter only) */
		if(model->type == SFMFlightModelHelicopter)
		    dir->bank = SFMSanitizeRadians(
			dir->bank + (b_con_coeff * acr->bank *
		        time_compensation * time_compression)
		    );

		/* Pitch by pitch */
		dir->pitch = SFMSanitizeRadians(
		    dir->pitch + (cos(dir->bank) * p_con_coeff * acr->pitch *
	            time_compensation * time_compression)
		);
		/* Pitch flipping check */
		if((dir->pitch > (0.5 * PI)) &&
		   (dir->pitch < (1.5 * PI))
		)
		{
		    dir->heading = SFMSanitizeRadians(dir->heading + PI);
		    dir->bank = SFMSanitizeRadians(dir->bank + PI);
		    if(dir->pitch > (1.0 * PI))
			dir->pitch = SFMSanitizeRadians(
			    (2.0 * PI) - dir->pitch + PI 
			);
		    else
			dir->pitch = SFMSanitizeRadians(
			    PI - dir->pitch
			);
		}
		/* Heading by pitch */
		dir->heading = SFMSanitizeRadians(
		    dir->heading + (sin(dir->bank) * p_con_coeff *
		    acr->pitch * -1 * time_compensation * time_compression)
		);

		/* Ground turning */
		if(flags & (SFMFlagGearTurnVelocityOptimul |
			    SFMFlagGearTurnVelocityMax |
			    SFMFlagGearTurnRate)
		)
		{
		    /* Heading by heading (turning) */
		    if((model->gear_turn_velocity_optimul > 0.0) &&
		       (model->gear_turn_velocity_max > 0.0)
		    )
		    {
/* TODO: Leaving model->gturn_radius out of the calculation, we are
 * just using a constant turn rate (in radians per second) for now
 */
			const double turn_rate = (0.2 * PI);
			double turn_coeff;

			/* Calculate turning coefficient, the amount of
			 * turning possible based on speed. The
			 * turning coefficient is optimul (best turning)
			 * when the current velocity is at
			 * gear_turn_velocity_optimul.
			 */
			if(vel->y < 0.0)
			{
			    /* Moving backwards */
			    turn_coeff = (vel->y /
				model->gear_turn_velocity_optimul);
			    if(turn_coeff < -1.0)
				turn_coeff = -MAX(
				    1.0 + (vel->y /
				    model->gear_turn_velocity_max),
				    0.0
				);
			}
			else
			{
			    /* Moving forwards */
			    turn_coeff = (vel->y /
				model->gear_turn_velocity_optimul);
			    if(turn_coeff > 1.0)
				turn_coeff = MAX(
				    1.0 - (vel->y /
				    model->gear_turn_velocity_max),
				    0.0
				);
			}
			/* Apply heading control to turn coefficient to
			 * calculate the actual amount of turning rate
			 * coefficient.
			 */
			turn_coeff = turn_coeff * h_con_coeff * turn_rate;

			/* Heading by ground turning */
			dir->heading = SFMSanitizeRadians(
			    dir->heading + (cos(dir->bank) * turn_coeff *
			    time_compensation * time_compression)
			);

			/* Pitch by ground turning */
			dir->pitch = SFMSanitizeRadians(
			    dir->pitch + (sin(dir->bank) * turn_coeff *
			    time_compensation * time_compression)
			);
			/* Bank by ground turning */
			dir->bank = SFMSanitizeRadians(
			    dir->bank + (sin(dir->pitch) * turn_coeff *
			    -1 * time_compensation * time_compression)
			);
		    }	/* Heading by heading (turning) */
		}	/* Ground landing gear turning defined? */

		/* Adjust velocity direction due to heading change */
		theta = SFMDeltaRadians(
		    prev_dir.heading, dir->heading
		);
		if(theta != 0.0)
		{
		    double a[3], r[3];

		    a[0] = vel->x;
		    a[1] = vel->y;
		    a[2] = vel->z;
		    MatrixRotateHeading3(a, -theta, r);
		    vel->x = r[0];
		    vel->y = r[1];
		    vel->z = r[2];
		}
	    }
	    else
	    {
		/* Not landed */

		/* Bank by bank */
		dir->bank = SFMSanitizeRadians(
		    dir->bank + (b_con_coeff * acr->bank *
		    time_compensation * time_compression)
		);

		/* Pitch by pitch */
		dir->pitch = SFMSanitizeRadians(
		    dir->pitch + (cos(dir->bank) * p_con_coeff * acr->pitch *
		    time_compensation * time_compression)
		);
		/* Pitch flipping check */
		if((dir->pitch > (0.5 * PI)) &&
		   (dir->pitch < (1.5 * PI))
		)
		{
		    dir->heading = SFMSanitizeRadians(dir->heading + PI);
		    dir->bank = SFMSanitizeRadians(dir->bank + PI);
		    if(dir->pitch > (1.0 * PI))
			dir->pitch = SFMSanitizeRadians(
			    (2.0 * PI) - dir->pitch + PI 
			);
		    else
			dir->pitch = SFMSanitizeRadians(
			    PI - dir->pitch
			);
		}

		/* Heading by pitch */
		dir->heading = SFMSanitizeRadians(
		    dir->heading + (sin(dir->bank) * p_con_coeff *
		    acr->pitch * -1 * time_compensation * time_compression)
		);


		/* Check if engines are on */
		if(1)
		{
		    /* Heading by heading (rudders) */
		    dir->heading = SFMSanitizeRadians(
			dir->heading + (cos(dir->bank) * h_con_coeff *
			acr->heading * time_compensation * time_compression)
		    );
		    /* Pitch by heading */
		    dir->pitch = SFMSanitizeRadians(
			dir->pitch + (sin(dir->bank) * h_con_coeff *
			acr->heading * time_compensation * time_compression)
		    );
		    /* Bank by heading */
		    dir->bank = SFMSanitizeRadians(
			dir->bank + (sin(dir->pitch) * h_con_coeff *
			acr->heading * -1 * time_compensation * time_compression)
		    );
		}

	        /* Drag caused by change of heading */
		if(1)
		{
		    double heading_delta = SFMDeltaRadians(
			prev_dir.heading, dir->heading
		    );

		    /* Heading delta must always be positive since only the
		     * net change is important to heading drag regardless
		     * of direction.
		     */
		    if(heading_delta < 0.0)
			heading_delta *= -1.0;

		    vel->y *= MAX(1.0 - (heading_delta / (0.5 * PI)), 0.0);

		    /* Update speed depending on flight model */
		    switch(model->type)
		    {
		      case SFMFlightModelAirplane:
			model->speed = SFMHypot2(vel->z, vel->y);
			break;

		      case SFMFlightModelHelicopter:
			model->speed = SFMHypot2(vel->x, vel->y);
			break;
		    }
		}
#if 0
printf("\r Speed %.2f  Z Vel: %.2f ",
 SFMMPCToMPH(obj_helicopter_ptr->speed),
 SFMMPCToMPH(vel->z)
); fflush(stdout);
#endif
	    }	/* Not landed */
	}

	return(0);
}