]> git.mxchange.org Git - flightgear.git/blob - src/ATC/AIEntity.cxx
- fixed ANSI C++ namespace problems
[flightgear.git] / src / ATC / AIEntity.cxx
1 // FGAIEntity - abstract base class an artificial intelligence entity
2 //
3 // Written by David Luff, started March 2002.
4 //
5 // Copyright (C) 2002  David C. Luff - david.luff@nottingham.ac.uk
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 /*****************************************************************
22 *
23 * WARNING - Curt has some ideas about AI traffic so anything in here
24 * may get rewritten or scrapped.  Contact Curt curt@flightgear.org 
25 * before spending any time or effort on this code!!!
26 *
27 ******************************************************************/
28
29 #include <Main/globals.hxx>
30 #include <Scenery/scenery.hxx>
31 //#include <simgear/constants.h>
32 #include <simgear/math/point3d.hxx>
33 #include <simgear/math/sg_geodesy.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <string>
36
37 #include "AIEntity.hxx"
38
39 extern ssgRoot* scene;  // The global Flightgear scene graph
40
41 FGAIEntity::~FGAIEntity() {
42 }
43
44 // Run the internal calculations
45 void FGAIEntity::Update() {
46 }
47
48 void FGAIEntity::Transform() {
49
50     // Translate moving object w.r.t eye
51     Point3D sc = scenery.get_center();
52     //cout << "sc0 = " << sc.x() << " sc1 = " << sc.y() << " sc2 = " << sc.z() << '\n';
53     //cout << "op0 = " << obj_pos.x() << " op1 = " << obj_pos.y() << " op2 = " << obj_pos.z() << '\n';
54
55     sgCoord shippos;
56     FastWorldCoordinate(&shippos, sc);
57     position->setTransform( &shippos );
58     scene->addKid(position);
59     //cout << "Transform called\n";
60 }
61
62 #if 0
63 // Taken from tileentry.cxx
64 void FGAIEntity::WorldCoordinate(sgCoord *obj_pos, Point3D center) {
65     // setup transforms
66     Point3D geod( lon * SGD_DEGREES_TO_RADIANS,
67                   lat * SGD_DEGREES_TO_RADIANS,
68                   elev );
69         
70     Point3D world_pos = sgGeodToCart( geod );
71     Point3D offset = world_pos - center;
72         
73     sgMat4 POS;
74     sgMakeTransMat4( POS, offset.x(), offset.y(), offset.z() );
75
76     sgVec3 obj_rt, obj_up;
77     sgSetVec3( obj_rt, 0.0, 1.0, 0.0); // Y axis
78     sgSetVec3( obj_up, 0.0, 0.0, 1.0); // Z axis
79
80     sgMat4 ROT_lon, ROT_lat, ROT_hdg;
81     sgMakeRotMat4( ROT_lon, lon, obj_up );
82     sgMakeRotMat4( ROT_lat, 90 - lat, obj_rt );
83     sgMakeRotMat4( ROT_hdg, hdg, obj_up );
84
85     sgMat4 TUX;
86     sgCopyMat4( TUX, ROT_hdg );
87     sgPostMultMat4( TUX, ROT_lat );
88     sgPostMultMat4( TUX, ROT_lon );
89     sgPostMultMat4( TUX, POS );
90
91     sgSetCoord( obj_pos, TUX );
92 }
93 #endif
94
95 // Norman's 'fast hack' for above
96 void FGAIEntity::FastWorldCoordinate(sgCoord *obj_pos, Point3D center) {
97     double lon_rad = lon * SGD_DEGREES_TO_RADIANS;
98     double lat_rad = lat * SGD_DEGREES_TO_RADIANS;
99     double hdg_rad = hdg * SGD_DEGREES_TO_RADIANS;
100
101     // setup transforms
102     Point3D geod( lon_rad, lat_rad, elev );
103         
104     Point3D world_pos = sgGeodToCart( geod );
105     Point3D offset = world_pos - center;
106
107     sgMat4 mat;
108
109     SGfloat sin_lat = (SGfloat)sin( lat_rad );
110     SGfloat cos_lat = (SGfloat)cos( lat_rad );
111     SGfloat cos_lon = (SGfloat)cos( lon_rad );
112     SGfloat sin_lon = (SGfloat)sin( lon_rad );
113     SGfloat sin_hdg = (SGfloat)sin( hdg_rad ) ;
114     SGfloat cos_hdg = (SGfloat)cos( hdg_rad ) ;
115
116     mat[0][0] =  cos_hdg * (SGfloat)sin_lat * (SGfloat)cos_lon - sin_hdg * (SGfloat)sin_lon;
117     mat[0][1] =  cos_hdg * (SGfloat)sin_lat * (SGfloat)sin_lon + sin_hdg * (SGfloat)cos_lon;
118     mat[0][2] = -cos_hdg * (SGfloat)cos_lat;
119     mat[0][3] =  SG_ZERO;
120
121     mat[1][0] = -sin_hdg * (SGfloat)sin_lat * (SGfloat)cos_lon - cos_hdg * (SGfloat)sin_lon;
122     mat[1][1] = -sin_hdg * (SGfloat)sin_lat * (SGfloat)sin_lon + cos_hdg * (SGfloat)cos_lon;
123     mat[1][2] =  sin_hdg * (SGfloat)cos_lat;
124     mat[1][3] =  SG_ZERO;
125
126     mat[2][0] = (SGfloat)cos_lat * (SGfloat)cos_lon;
127     mat[2][1] = (SGfloat)cos_lat * (SGfloat)sin_lon;
128     mat[2][2] = (SGfloat)sin_lat;
129     mat[2][3] =  SG_ZERO;
130
131     mat[3][0] = offset.x();
132     mat[3][1] = offset.y();
133     mat[3][2] = offset.z();
134     mat[3][3] = SG_ONE ;
135
136     sgSetCoord( obj_pos, mat );
137 }