1 // location.cxx -- class for determining model location in the flightgear world.
3 // Written by Jim Wilson, David Megginson, started April 2002.
4 // Based largely on code by Curtis Olson and Norman Vine.
6 // Copyright (C) 2002 Curtis L. Olson - curt@flightgear.org
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <simgear/compiler.h>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/constants.h>
33 #include <simgear/math/point3d.hxx>
34 #include <simgear/math/polar3d.hxx>
35 #include <simgear/math/sg_geodesy.hxx>
36 #include <simgear/math/vector.hxx>
38 #include <Scenery/scenery.hxx>
40 #include "globals.hxx"
42 #include "location.hxx"
46 * make model transformation Matrix - based on optimizations by NHV
48 static void MakeTRANS( sgMat4 dst, const double Theta,
49 const double Phi, const double Psi,
52 SGfloat cosTheta = (SGfloat) cos(Theta);
53 SGfloat sinTheta = (SGfloat) sin(Theta);
54 SGfloat cosPhi = (SGfloat) cos(Phi);
55 SGfloat sinPhi = (SGfloat) sin(Phi);
56 SGfloat sinPsi = (SGfloat) sin(Psi) ;
57 SGfloat cosPsi = (SGfloat) cos(Psi) ;
61 tmp[0][0] = cosPhi * cosTheta;
62 tmp[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
63 tmp[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
65 tmp[1][0] = -sinPhi * cosTheta;
66 tmp[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
67 tmp[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
70 tmp[2][1] = cosTheta * -sinPsi;
71 tmp[2][2] = cosTheta * cosPsi;
76 dst[2][0] = a*tmp[0][0] + b*tmp[0][1] + c*tmp[0][2] ;
77 dst[1][0] = a*tmp[1][0] + b*tmp[1][1] + c*tmp[1][2] ;
78 dst[0][0] = -(a*tmp[2][0] + b*tmp[2][1] + c*tmp[2][2]) ;
84 dst[2][1] = a*tmp[0][0] + b*tmp[0][1] + c*tmp[0][2] ;
85 dst[1][1] = a*tmp[1][0] + b*tmp[1][1] + c*tmp[1][2] ;
86 dst[0][1] = -(a*tmp[2][0] + b*tmp[2][1] + c*tmp[2][2]) ;
91 dst[2][2] = a*tmp[0][0] + c*tmp[0][2] ;
92 dst[1][2] = a*tmp[1][0] + c*tmp[1][2] ;
93 dst[0][2] = -(a*tmp[2][0] + c*tmp[2][2]) ;
104 ////////////////////////////////////////////////////////////////////////
105 // Implementation of FGLocation.
106 ////////////////////////////////////////////////////////////////////////
109 FGLocation::FGLocation( void ):
120 sgdZeroVec3(_absolute_view_pos);
121 sgZeroVec3(_relative_view_pos);
122 sgZeroVec3(_zero_elev_view_pos);
123 sgMakeRotMat4( UP, 0.0, 0.0, 0.0 );
124 sgMakeRotMat4( TRANS, 0.0, 0.0, 0.0 );
129 FGLocation::~FGLocation( void ) {
143 FGLocation::unbind ()
148 FGLocation::setPosition (double lon_deg, double lat_deg, double alt_ft)
157 FGLocation::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
160 _roll_deg = roll_deg;
161 _pitch_deg = pitch_deg;
162 _heading_deg = heading_deg;
166 FGLocation::get_absolute_view_pos ()
170 return _absolute_view_pos;
174 FGLocation::getRelativeViewPos ()
178 return _relative_view_pos;
182 FGLocation::getZeroElevViewPos ()
186 return _zero_elev_view_pos;
190 // recalc() is done every time one of the setters is called (making the
191 // cached data "dirty") on the next "get". It calculates all the outputs
194 FGLocation::recalc ()
197 recalcPosition( _lon_deg, _lat_deg, _alt_ft );
199 // Make the world up rotation matrix for eye positioin...
200 sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
203 // get the world up radial vector from planet center for output
204 sgSetVec3( _world_up, UP[0][0], UP[0][1], UP[0][2] );
206 // Creat local matrix with current geodetic position. Converting
207 // the orientation (pitch/roll/heading) to vectors.
208 MakeTRANS( TRANS, _pitch_deg * SG_DEGREES_TO_RADIANS,
209 _roll_deg * SG_DEGREES_TO_RADIANS,
210 -_heading_deg * SG_DEGREES_TO_RADIANS,
213 // Given a vector pointing straight down (-Z), map into onto the
214 // local plane representing "horizontal". This should give us the
215 // local direction for moving "south".
217 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
219 sgmap_vec_onto_cur_surface_plane(_world_up, _relative_view_pos, minus_z,
221 sgNormalizeVec3(_surface_south);
223 // now calculate the surface east vector
225 sgNegateVec3(world_down, _world_up);
226 sgVectorProductVec3(_surface_east, _surface_south, world_down);
232 FGLocation::recalcPosition (double lon_deg, double lat_deg, double alt_ft) const
234 double sea_level_radius_m;
238 // Convert from geodetic to geocentric
240 sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
241 alt_ft * SG_FEET_TO_METER,
245 // Calculate the cartesian coordinates
246 // of point directly below at sea level.
247 // aka Zero Elevation Position
248 Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
251 Point3D tmp = sgPolarToCart3d(p) - _tile_center;
252 sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
254 // Calculate the absolute view position
255 // in fgfs coordinates.
256 // aka Absolute View Position
257 p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
258 tmp = sgPolarToCart3d(p);
259 sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
261 // Calculate the relative view position
262 // from the scenery center.
263 // aka Relative View Position
265 // FIXME: view position should ONLY be calculated in the viewer...
266 // Anything else should calculate their own positions relative to the
267 // viewer's tile_center.
268 sgdVec3 scenery_center;
269 sgdSetVec3(scenery_center,
270 globals->get_scenery()->get_center().x(),
271 globals->get_scenery()->get_center().y(),
272 globals->get_scenery()->get_center().z());
274 sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
275 sgSetVec3(_relative_view_pos, view_pos);
280 FGLocation::update (int dt)