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 Aircraft base class.
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 OBJECT_AIRCRAFT_HH
13 #define OBJECT_AIRCRAFT_HH 1
14
15 #include "object/craft.hh"
16 #include "physics/module.hh"
17 #include "physics/physics_aircraft.hh"
18 using namespace physics;
19 #include "graph/module.hh"
20 #include "graph/subgraph.hh"
21 using namespace graph;
22
23 namespace object {
24
25 ////////////////////////////////////////////////////////////////////////////////
26 /// @brief Aircraft class.
27 ///
28 /// This class is unusual:
29 /// - Aircraft has both a virtual and class method to get AircraftSpecs.
30 /// - Derivatives have a class method ClassGetSpecs() used for instantiating themselves.
31 ///
32 /// mThrottle is a fp {0.0,...,1.0}.
33 /// mStep is a world coordinate.
34 /// Speed (translation increment) is determined by mThrottle * mStep.
35 ///
36 /// Aircraft state enums:
37 /// The state enums (flying, landing, etc) are kept in the Aircraft class.
38 /// Both Aircraft and AircraftPhysics can affect the state.
39 ///
40 class Aircraft : public Craft
41 {
42 PREVENT_COPYING(Aircraft)
43 typedef Craft Parent;
44 friend class shptr<Aircraft>; friend class shptr0<Aircraft>;
45 // Axises as defined by 3D model files.
46 public: enum { MODEL_AXIS_PITCH = ZZ,
47 MODEL_AXIS_ROLL = XX,
48 MODEL_AXIS_YAW = YY };
49 public: enum eState
50 {
51 eState_PARKED,
52 eState_TAKEOFF,
53 eState_FLYING,
54 eState_LANDING,
55 };
56 // Only derivatives can access Aircraft ctor.
57 protected: Aircraft( shptr<Graph> graph, const WorldVertex& pos, const AircraftSpecs& specs );
58 public: virtual ~Aircraft();
59 public: CLASS_METHOD shptr<Graph> LoadModel( const AircraftSpecs& specs, const bool loadCopy );
60 public: virtual void Reset( void );
GetName(void)61 public: virtual const string GetName( void ) { return string("Aircraft"); }
62
63 // Spatial:
64 public: virtual void SetPosition( const WorldVertex& position );
65 public: virtual void Translate( uint axis, fp inc );
66 public: virtual void Translate( const Vector3& v );
67 public: virtual void Rotate( uint axis, Radian rad );
68
69 // Transformation:
70 public: virtual void SetMatrix( const Matrix& matrix, Dyna::eMatrixChange change );
71
72 // Control:
73 public: virtual void PhysicalRotate( uint axis, fp controlFraction, Milliseconds controlFreq );
RotateControlSurfaces(const uint axis,const fp frac)74 public: virtual void RotateControlSurfaces( const uint axis, const fp frac ) { }
75 public: virtual void SetThrottle( const fp throttle );
GetThrottle(void)76 public: virtual fp GetThrottle( void ) { CHECK_TYPESIG(this,TYPESIG_AIRCRAFT); return mThrottle; }
77 public: virtual void EnableBrakes( const bool enable );
78 public: virtual bool IfBrakes( void );
79 public: virtual bool IfWheelBrakes( void );
80 public: virtual bool IfAirBrakes( void );
81 public: virtual void SetLandingGear( const bool down );
82 public: virtual bool IfLandingGear( void ); // true if extended down
SwingWings(const float fraction)83 public: virtual void SwingWings( const float fraction ) { } // 0.0:forward...1.0:back
SwingBayDoors(const float fraction)84 public: virtual void SwingBayDoors( const float fraction ) { } // 0.0:close...1.0:open
85
86 // AircraftPhysics:
GetPhysics(void)87 public: virtual AircraftPhysics& GetPhysics( void ) { CHECK_TYPESIG(this,TYPESIG_AIRCRAFT);
88 return mPhysics; }
89 public: void EnableCoordinatedTurn( const bool enable );
90 public: bool IfCoordinatedTurnEnabled( void ) const;
91 public: bool IfCoordinatedTurnExecuting( void ) const;
92
93 // Animation, sounds:
94 protected: virtual void Tick( const Milliseconds millisecElapsed );
95 protected: virtual void AnimateStall( void );
96 public: virtual void PlayEngineSound( const fp volume );
97 public: virtual void StopEngineSound( void );
98 public: virtual void SetShadowCaster( const bool enable = true );
99
100 // Control surfaces (graphical):
101 protected: enum eAileron { LEFT_AILERON = 1, RIGHT_AILERON = -1 }; // direction
102 protected: enum eRudder { SINGLE_RUDDER = 0, LEFT_RUDDER = 1, RIGHT_RUDDER = -1 };
103 protected: void RotateAileron( Subgraph& subgraph, const fp controlFraction, const eAileron aileron );
104 protected: void RotateAileron( Subgraph& subgraph, const fp controlFraction, const eAileron aileron, const Vector3& aileronOffset );
105 protected: void RotateElevator( Subgraph& subgraph, const fp controlFraction );
106 protected: void RotateRudder( Subgraph& subgraph, const fp controlFraction, const eRudder = SINGLE_RUDDER );
107
108 // States: collision-detection and contact force.
109 // Dyna has basic collision-detection.
110 // Aircraft determines what kind of collision: safe contact or fatal collision.
111 public: virtual void SetState( const eState state );
112 public: virtual eState GetState( void );
113 public: bool IfFlying( void );
114 public: virtual void SetCollision( const Object::eCollision collision );
115 public: virtual bool IfCollisionRunway( void );
116 protected: virtual void HandleCollisions( const Dyna::Colliders& colliders );
117 private: void MoreCrashDetermination( void );
118 private: void UpdateState( const Milliseconds millisecElapsed ); // subroutine of Tick()
119
120 // Info:
121 public: virtual NodeSort GetNodeSort( void );
122 public: virtual fp GetHeight( void );
123 public: bool IfCanSetLandingGear( const bool down );
124 public: virtual const AircraftSpecs& GetSpecs( void ) = 0;
125 public: virtual bool IfHasPropeller( void ) = 0;
IfHasGuns(void)126 public: virtual bool IfHasGuns( void ) { return true; }
127
128 // Data:
129 private: Matrix mSavedMatrix; ///< restored by Reset()
130 public: AircraftPhysics mPhysics; ///< physics model (public to use AircraftPhysics interface)
131 private: eState mState; ///< general states { parked, landing, etc }
132 private: Milliseconds mStateUpdateTime; ///< timestamp when state was updated
133 private: bool mOnRunway;
134 private: bool mCoordinatedTurnEnabled; ///< if coordinated turn is enabled
135 private: bool mCoordinatedTurnExecuting; ///< if program is performing a coordinated turn
136 private: fp mThrottle; ///< {0.0,..,1.0}
137 protected: bool mLandingGearDown;
138 protected: bool mShadowCaster; ///< will affect Aircraft::GetNodeSort()
139 public: DECLARE_TYPESIG( TYPESIG_AIRCRAFT )
140 };
141
142 } // namespace object
143
144 #endif // OBJECT_AIRCRAFT_HH
145