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);
125 FGLocation::~FGLocation( void ) {
139 FGLocation::unbind ()
144 FGLocation::setPosition (double lon_deg, double lat_deg, double alt_ft)
153 FGLocation::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
156 _roll_deg = roll_deg;
157 _pitch_deg = pitch_deg;
158 _heading_deg = heading_deg;
162 FGLocation::get_absolute_view_pos ()
166 return _absolute_view_pos;
170 FGLocation::getRelativeViewPos ()
174 return _relative_view_pos;
178 FGLocation::getZeroElevViewPos ()
182 return _zero_elev_view_pos;
186 // recalc() is done every time one of the setters is called (making the
187 // cached data "dirty") on the next "get". It calculates all the outputs
190 FGLocation::recalc ()
193 recalcPosition( _lon_deg, _lat_deg, _alt_ft );
195 // Make the world up rotation matrix for eye positioin...
196 sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
199 // get the world up radial vector from planet center for output
200 sgSetVec3( _world_up, UP[0][0], UP[0][1], UP[0][2] );
202 // Creat local matrix with current geodetic position. Converting
203 // the orientation (pitch/roll/heading) to vectors.
204 MakeTRANS( TRANS, _pitch_deg * SG_DEGREES_TO_RADIANS,
205 _roll_deg * SG_DEGREES_TO_RADIANS,
206 -_heading_deg * SG_DEGREES_TO_RADIANS,
209 // Given a vector pointing straight down (-Z), map into onto the
210 // local plane representing "horizontal". This should give us the
211 // local direction for moving "south".
213 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
215 sgmap_vec_onto_cur_surface_plane(_world_up, _relative_view_pos, minus_z,
217 sgNormalizeVec3(_surface_south);
219 // now calculate the surface east vector
221 sgNegateVec3(world_down, _world_up);
222 sgVectorProductVec3(_surface_east, _surface_south, world_down);
228 FGLocation::recalcPosition (double lon_deg, double lat_deg, double alt_ft) const
230 double sea_level_radius_m;
234 // Convert from geodetic to geocentric
236 sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
237 alt_ft * SG_FEET_TO_METER,
241 // Calculate the cartesian coordinates
242 // of point directly below at sea level.
243 // aka Zero Elevation Position
244 Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
247 Point3D tmp = sgPolarToCart3d(p) - _tile_center;
248 sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
250 // Calculate the absolute view position
251 // in fgfs coordinates.
252 // aka Absolute View Position
253 p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
254 tmp = sgPolarToCart3d(p);
255 sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
257 // Calculate the relative view position
258 // from the scenery center.
259 // aka Relative View Position
261 // FIXME: view position should ONLY be calculated in the viewer...
262 // Anything else should calculate their own positions relative to the
263 // viewer's tile_center.
264 sgdVec3 scenery_center;
265 sgdSetVec3(scenery_center,
266 globals->get_scenery()->get_center().x(),
267 globals->get_scenery()->get_center().y(),
268 globals->get_scenery()->get_center().z());
270 sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
271 sgSetVec3(_relative_view_pos, view_pos);
276 FGLocation::update (int dt)