1 // Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _DYNAMICBODY_H
5 #define _DYNAMICBODY_H
6 
7 #include "ModelBody.h"
8 #include "matrix4x4.h"
9 #include "vector3.h"
10 
11 class Propulsion;
12 class FixedGuns;
13 class Orbit;
14 
15 class DynamicBody : public ModelBody {
16 private:
17 	friend class Propulsion;
18 	friend class FixedGuns;
19 
20 public:
21 	OBJDEF(DynamicBody, ModelBody, DYNAMICBODY);
22 	DynamicBody();
23 	DynamicBody(const Json &jsonObj, Space *space);
24 	virtual ~DynamicBody();
25 
26 	virtual vector3d GetVelocity() const override;
27 	virtual void SetVelocity(const vector3d &v) override;
28 	virtual void SetFrame(FrameId fId) override;
29 	vector3d GetAngVelocity() const;
30 	void SetAngVelocity(const vector3d &v);
31 	virtual bool OnCollision(Body *o, Uint32 flags, double relVel) override;
32 	vector3d GetAngularMomentum() const;
GetAngularInertia()33 	double GetAngularInertia() const { return m_angInertia; }
34 	void SetMassDistributionFromModel();
SetMoving(bool isMoving)35 	void SetMoving(bool isMoving) { m_isMoving = isMoving; }
IsMoving()36 	bool IsMoving() const { return m_isMoving; }
GetMass()37 	virtual double GetMass() const override { return m_mass; } // XXX don't override this
38 	virtual void TimeStepUpdate(const float timeStep) override;
39 	double CalcAtmosphericDrag(double velSqr, double area, double coeff) const;
40 	void CalcExternalForce();
41 
42 	void SetMass(double);
43 	void AddForce(const vector3d &);
44 	void AddTorque(const vector3d &);
45 	void SetForce(const vector3d &);
46 	void SetTorque(const vector3d &);
GetLastForce()47 	vector3d GetLastForce() const { return m_lastForce; }
GetLastTorque()48 	vector3d GetLastTorque() const { return m_lastTorque; }
49 	// body-relative forces
50 	void AddRelForce(const vector3d &);
51 	void AddRelTorque(const vector3d &);
GetExternalForce()52 	vector3d GetExternalForce() const { return m_externalForce; }
GetAtmosForce()53 	vector3d GetAtmosForce() const { return m_atmosForce; }
GetGravityForce()54 	vector3d GetGravityForce() const { return m_gravityForce; }
55 	virtual void UpdateInterpTransform(double alpha) override;
56 
57 	virtual void PostLoadFixup(Space *space) override;
58 
59 	Orbit ComputeOrbit() const;
60 
61 	/* TODO: This is a big simplification...
62 	 * something better because AI on dynamic is
63 	 * a "loose" thing (also see AIError m_aiMessage
64 	 * in line 83)
65 	*/
66 	enum AIError { // <enum scope='Ship' name=ShipAIError prefix=AIERROR_ public>
67 		AIERROR_NONE = 0,
68 		AIERROR_GRAV_TOO_HIGH,
69 		AIERROR_REFUSED_PERM,
70 		AIERROR_PRESS_TOO_HIGH,
71 		AIERROR_ORBIT_IMPOSSIBLE
72 	};
73 	AIError AIMessage(AIError msg = AIERROR_NONE)
74 	{
75 		AIError tmp = m_aiMessage;
76 		m_aiMessage = msg;
77 		return tmp;
78 	}
79 
80 	enum Feature {
81 		PROPULSION = 0,
82 		FIXED_GUNS = 1,
83 		MAX_FEATURE = 2,
84 	};
85 
Have(Feature f)86 	bool Have(Feature f) const { return m_features[f]; };
SetDecelerating(bool decel)87 	void SetDecelerating(bool decel) { m_decelerating = decel; }
88 	const Propulsion *GetPropulsion() const;
89 	Propulsion *GetPropulsion();
90 	const FixedGuns *GetFixedGuns() const;
91 	FixedGuns *GetFixedGuns();
92 	void AddFeature(Feature f);
93 
94 protected:
95 	virtual void SaveToJson(Json &jsonObj, Space *space) override;
96 
97 	void GetCurrentAtmosphericState(double &pressure, double &density) const;
98 
99 	virtual vector3d CalcAtmosphericForce() const;
100 
101 	static const double DEFAULT_DRAG_COEFF;
102 
103 	double m_dragCoeff;
104 
105 	bool m_decelerating;
106 	AIError m_aiMessage;
107 
108 private:
109 	vector3d m_oldPos;
110 	vector3d m_oldAngDisplacement;
111 
112 	vector3d m_force;
113 	vector3d m_torque;
114 	vector3d m_vel;
115 	vector3d m_angVel;
116 	double m_mass;
117 	double m_massRadius; // set in a mickey-mouse fashion from the collision mesh and used to calculate m_angInertia
118 	double m_angInertia; // always sphere mass distribution
119 	bool m_isMoving;
120 
121 	vector3d m_externalForce;
122 	vector3d m_atmosForce;
123 	vector3d m_gravityForce;
124 	// for time accel reduction fudge
125 	vector3d m_lastForce;
126 	vector3d m_lastTorque;
127 
128 	bool m_features[MAX_FEATURE];
129 
130 	RefCountedPtr<Propulsion> m_propulsion;
131 	RefCountedPtr<FixedGuns> m_fixedGuns;
132 };
133 
134 #endif /* _DYNAMICBODY_H */
135