1 // location.hxx -- class for determining model location in the flightgear world.
3 // Written by Jim Wilson, David Megginson, started April 2002.
5 // Copyright (C) 2002 Jim Wilson, David Megginson
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.
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.
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.
24 #include <simgear/compiler.h>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/constants.h>
32 #include <simgear/math/point3d.hxx>
33 #include <simgear/math/polar3d.hxx>
34 #include <simgear/math/sg_geodesy.hxx>
36 #include <Scenery/scenery.hxx>
38 #include <simgear/math/vector.hxx>
40 #include "globals.hxx"
43 #include "location.hxx"
47 * make model transformation Matrix - based on optimizations by NHV
49 static void MakeTRANS( sgMat4 dst, const double Theta,
50 const double Phi, const double Psi,
53 SGfloat cosTheta = (SGfloat) cos(Theta);
54 SGfloat sinTheta = (SGfloat) sin(Theta);
55 SGfloat cosPhi = (SGfloat) cos(Phi);
56 SGfloat sinPhi = (SGfloat) sin(Phi);
57 SGfloat sinPsi = (SGfloat) sin(Psi) ;
58 SGfloat cosPsi = (SGfloat) cos(Psi) ;
62 tmp[0][0] = cosPhi * cosTheta;
63 tmp[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
64 tmp[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
66 tmp[1][0] = -sinPhi * cosTheta;
67 tmp[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
68 tmp[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
71 tmp[2][1] = cosTheta * -sinPsi;
72 tmp[2][2] = cosTheta * cosPsi;
77 dst[2][0] = a*tmp[0][0] + b*tmp[0][1] + c*tmp[0][2] ;
78 dst[1][0] = a*tmp[1][0] + b*tmp[1][1] + c*tmp[1][2] ;
79 dst[0][0] = -(a*tmp[2][0] + b*tmp[2][1] + c*tmp[2][2]) ;
85 dst[2][1] = a*tmp[0][0] + b*tmp[0][1] + c*tmp[0][2] ;
86 dst[1][1] = a*tmp[1][0] + b*tmp[1][1] + c*tmp[1][2] ;
87 dst[0][1] = -(a*tmp[2][0] + b*tmp[2][1] + c*tmp[2][2]) ;
92 dst[2][2] = a*tmp[0][0] + c*tmp[0][2] ;
93 dst[1][2] = a*tmp[1][0] + c*tmp[1][2] ;
94 dst[0][2] = -(a*tmp[2][0] + c*tmp[2][2]) ;
105 ////////////////////////////////////////////////////////////////////////
106 // Implementation of FGLocation.
107 ////////////////////////////////////////////////////////////////////////
110 FGLocation::FGLocation( void ):
119 sgdZeroVec3(_absolute_view_pos);
124 FGLocation::~FGLocation( void ) {
138 FGLocation::unbind ()
143 FGLocation::setPosition (double lon_deg, double lat_deg, double alt_ft)
152 FGLocation::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
155 _roll_deg = roll_deg;
156 _pitch_deg = pitch_deg;
157 _heading_deg = heading_deg;
161 FGLocation::get_absolute_view_pos ()
165 return _absolute_view_pos;
169 FGLocation::getRelativeViewPos ()
173 return _relative_view_pos;
177 FGLocation::getZeroElevViewPos ()
181 return _zero_elev_view_pos;
185 // recalc() is done every time one of the setters is called (making the
186 // cached data "dirty") on the next "get". It calculates all the outputs
189 FGLocation::recalc ()
192 recalcPosition( _lon_deg, _lat_deg, _alt_ft );
194 // Make the world up rotation matrix for eye positioin...
195 sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
198 // get the world up radial vector from planet center for output
199 sgSetVec3( _world_up, UP[0][0], UP[0][1], UP[0][2] );
201 // Creat local matrix with current geodetic position. Converting
202 // the orientation (pitch/roll/heading) to vectors.
203 MakeTRANS( TRANS, _pitch_deg * SG_DEGREES_TO_RADIANS,
204 _roll_deg * SG_DEGREES_TO_RADIANS,
205 -_heading_deg * SG_DEGREES_TO_RADIANS,
208 // Given a vector pointing straight down (-Z), map into onto the
209 // local plane representing "horizontal". This should give us the
210 // local direction for moving "south".
212 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
214 sgmap_vec_onto_cur_surface_plane(_world_up, _relative_view_pos, minus_z,
216 sgNormalizeVec3(_surface_south);
218 // now calculate the surface east vector
220 sgNegateVec3(world_down, _world_up);
221 sgVectorProductVec3(_surface_east, _surface_south, world_down);
227 FGLocation::recalcPosition (double lon_deg, double lat_deg, double alt_ft) const
229 double sea_level_radius_m;
233 // Convert from geodetic to geocentric
235 sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
236 alt_ft * SG_FEET_TO_METER,
240 // Calculate the cartesian coordinates
241 // of point directly below at sea level.
242 // aka Zero Elevation Position
243 Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
246 Point3D tmp = sgPolarToCart3d(p) - scenery.get_next_center();
247 sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
249 // Calculate the absolute view position
250 // in fgfs coordinates.
251 // aka Absolute View Position
252 p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
253 tmp = sgPolarToCart3d(p);
254 sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
256 // Calculate the relative view position
257 // from the scenery center.
258 // aka Relative View Position
259 sgdVec3 scenery_center;
260 sgdSetVec3(scenery_center,
261 scenery.get_next_center().x(),
262 scenery.get_next_center().y(),
263 scenery.get_next_center().z());
265 sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
266 sgSetVec3(_relative_view_pos, view_pos);
271 FGLocation::update (int dt)