1 /*****************************************************************************
2  * $LastChangedDate: 2011-04-09 21:58:06 -0400 (Sat, 09 Apr 2011) $
3  * @file
4  * @author  Jim E. Brooks  http://www.palomino3d.org
5  * @brief   Physics model.
6  *//*
7  * LEGAL:   COPYRIGHT (C) 2007 JIM E. BROOKS
8  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
9  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
10  *****************************************************************************/
11 
12 #ifndef PHYSICS_AIRCRAFT_PHYSICS_HH
13 #define PHYSICS_AIRCRAFT_PHYSICS_HH 1
14 
15 #include "object/module.hh"
16 //#include "object/aircraft.hh"
17 namespace object { class Aircraft; }
18 using namespace object;
19 #include "physics/aircraft_specs.hh"
20 
21 namespace physics {
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 /// @brief AircraftPhysics model.
25 ///
26 /// Thrust/drag/lift:
27 /// The AircraftPhysics class doesn't need to transform thrust/drag/lift (excluding weight).
28 /// These force vectors are relative to a Aircraft and therefore exist in local space.
29 /// Ultimately these are passed to Aircraft::Translate(Vector3) which transforms them.
30 ///
31 /// Weight:
32 /// The direction of the weight vector is fixed/absolute of course.
33 /// Weight acceleration can be applied  using Object::GetPosition()/SetPosition()
34 /// to directly add weight vector in terms of a world vertex.
35 ///
36 /// Compute() methods:
37 /// Many methods that just get a value are named Compute()
38 /// since ideally they would compute a physical parameter.
39 ///
40 /// Aircraft state enums:
41 /// The state enums (flying, landing, etc) are kept in the Aircraft class.
42 /// Both Aircraft and AircraftPhysics can affect the state.
43 ///
44 class AircraftPhysics
45 {
46 COPYABLE(AircraftPhysics)
47 
48 private:
49     CLASS_CONST int THRUST_DIR = -1;  ///< multiplier/sign
50     CLASS_CONST int LIFT_DIR   =  1;  ///< multiplier/sign
51 
52 public:
53                         AircraftPhysics( SafePtr<Aircraft> aircraft, const AircraftSpecs& specs );
54                         ~AircraftPhysics();
55 
56 // Enable, reset:
57 // Physics can be globally disabled.
58 public:
59     void                Enable( bool enable );
60     bool                IfEnabled( void );
61     void                Reset( void );
62 
63 // Object animation:
64 public:
65     void                Tick( const Milliseconds timeElapsed );
66 private:
67     bool                AnimateAircraft( const Milliseconds timeElapsed );
68     bool                AnimateAircraftLanding( const Milliseconds timeElapsed );
69 
70 // Speed, velocity (Speed type is meters/second):
71 public:
72     void                SetSpeed( const Speed speed );
73     Speed               ComputeSpeed( void );
74     Speed               ComputeMaxSpeed( void );
ComputeVelocity(void)75     Velocity            ComputeVelocity( void ) { return mWorld.mVelocity; }
ComputeStallSpeed(void)76     Speed               ComputeStallSpeed( void ) { return physics::conv::KPH2Speed(mSpecs.mStallSpeed); }
ComputeLiftoffSpeed(void)77     Speed               ComputeLiftoffSpeed( void ) { return physics::conv::KPH2Speed(mSpecs.mLiftoffSpeed); }
78 private:
79     bool                IfVelocity( void );
80     bool                IfForwardVelocity( const Matrix& aircraftMatrix );
81     bool                IfDownwardVelocity( void );
82 
83 // Thrust:
84 public:
85     void                SetThrust( const Newton1 thrust );  // magnitude
ComputeMaxThrust(void)86     Newton1             ComputeMaxThrust( void ) { return mSpecs.mMaxThrustMag; }
87 
88 // Drag:
89 public:
EnableBrakes(const bool enable)90     void                EnableBrakes( const bool enable ) { mBrakes = enable; }
IfBrakes(void)91     bool                IfBrakes( void ) { return mBrakes; }
92     bool                IfWheelBrakes( void );
IfAirBrakes(void)93     bool                IfAirBrakes( void ) { return IfBrakes() and not IfWheelBrakes(); }
94 
95 // Fundamental forces:
96 public:
ComputeThrustForce(void)97     Newton              ComputeThrustForce( void ) { return mWorld.mThrustForce; }
ComputeDragForce(void)98     Newton              ComputeDragForce( void )   { return mWorld.mDragForce; }
ComputeLiftForce(void)99     Newton              ComputeLiftForce( void )   { return mWorld.mLiftForce; }
ComputeWeightForce(void)100     Newton              ComputeWeightForce( void ) { return mWorld.mWeightForce; }
101 private:
102     Vector3             AircraftAxisThrust( const Matrix& m );
103     Vector3             AircraftAxisLift( const Matrix& m );
104 
105 // Angle-of-attack, rotation rates:
106 public:
107     Degree              ComputeAngleOfAttack( const bool ifVelocity );
ComputeAngleOfAttack(void)108     Degree              ComputeAngleOfAttack( void )
109                         { return ComputeAngleOfAttack(IfVelocity()); }
110     Degree              ComputeRollRate( void );
111     Degree              ComputePitchRate( void );
112     Degree              ComputeYawRate( void );
113 private:
114     fp                  ComputeRotationFactor( void );
115 
116 // Turning:
117 public:
118     enum eTurning { eNotTurning, eTurningLeft, eTurningRight };
119     eTurning            ComputeTurningDir( void );
120     bool                IfTurning( void );
121 
122 // Stalling:
123 public:
124     bool                IfStall( void );
125     fp                  ComputeStall( void );
126 
127 private:
128     ////////////////////////////////////////////////////////////////////////////
129     /// @brief Defines motion in a coordinate system.
130     ///
131     /// MotionSpace is a vestige, but one that remains useful, from when
132     /// PhysicsAircraft used to do computations in both local and world spaces.
133     ///
134     struct MotionSpace
135     {
136         MotionSpace( void );
137         void Reset( void );
138 
139         Newton          mThrustForce, mDragForce, mLiftForce, mWeightForce;
140         Acceleration    mThrustAccel, mDragAccel, mLiftAccel, mWeightAccel;
141         Acceleration    mNetAccel;
142         Velocity        mVelocity;
143     };
144 
145 /// Reference-counting problem:
146 /// AircraftPhysics is a member of Aircraft.
147 /// A reference-counted class cannot have any reference to itself
148 /// else it would never be destroyed.  Therefore, AircraftPhysics has
149 /// a SafePtr<Aircraft> back to Aircraft.
150 private:
151     bool                mEnable;            ///< enable this instance
152     SafePtr<Aircraft>   mAircraft;          ///< (cannot be shptr!!) the Object this AircraftPhysics instance controls
153     AircraftSpecs       mSpecs;             ///< specifications/characteristics of a type of Aircraft
154     Milliseconds        mPrevTick;
155     MotionSpace         mWorld;             ///< motion in world space
156     Newton1             mThrustMag;         ///< magnitude (current thrust)
157     bool                mBrakes;
158 public:
159     DECLARE_TYPESIG(TYPESIG_AIRCRAFT_PHYSICS)
160 };
161 
162 } // namespace physics
163 
164 #endif // PHYSICS_AIRCRAFT_PHYSICS_HH
165