]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/RigidBody.hpp
b34b5a482a1f26c182c11b31053aa19da7e74878
[flightgear.git] / src / FDM / YASim / RigidBody.hpp
1 #ifndef _RIGIDBODY_HPP
2 #define _RIGIDBODY_HPP
3
4 namespace yasim {
5
6 //
7 // A RigidBody object maintains all "internal" state about an object,
8 // accumulates force and torque information from external sources, and
9 // calculates the resulting accelerations.
10 //
11 //
12 // Units note: obviously, the choice of mass, time and distance units
13 // is up to the user.  If you provide mass in kilograms, forces in
14 // newtons, and torques in newton-meters, you'll get your
15 // accelerations back in m/s^2.  The angular units, however, are
16 // UNIFORMLY radians.  Angular velocities are radians per <time unit>,
17 // the angular acceleration you get back is radians per <time unit>^2,
18 // and the angular momenta supplied to setGyro must be in radians,
19 // too.  Radians, not degrees.  Don't forget.
20 //
21 class RigidBody
22 {
23 public:
24     RigidBody();
25     ~RigidBody();
26
27     // Adds a point mass to the system.  Returns a handle so the gyro
28     // can be later modified via setMass().
29     int addMass(float mass, float* pos);
30
31     // Modifies a previously-added point mass (fuel tank running dry,
32     // gear going up, swing wing swinging, pilot bailing out, etc...)
33     void setMass(int handle, float mass);
34     void setMass(int handle, float mass, float* pos);
35
36     int numMasses();
37     float getMass(int handle);
38     void getMassPosition(int handle, float* out);
39     float getTotalMass();
40
41     // The velocity, in local coordinates, of the specified point on a
42     // body rotating about its c.g. with velocity rot.
43     void pointVelocity(float* pos, float* rot, float* out);
44
45     // Sets the "gyroscope" for the body.  This is the total
46     // "intrinsic" angular momentum of the body; that is, rotations of
47     // sub-objects, NOT rotation of the whole body within the global
48     // frame.  Because angular momentum is additive in this way, we
49     // don't need to specify specific gyro objects; just add all their
50     // momenta together and set it here.
51     void setGyro(float* angularMomentum);
52
53
54     // When masses are moved or changed, this object needs to
55     // regenerate its internal tables.  This step is expensive, so
56     // it's exposed to the client who can amortize the call across
57     // multiple changes.
58     void recalc();
59
60     // Resets the current force/torque parameters to zero.
61     void reset();
62
63
64     // Applies a force at the specified position.
65     void addForce(float* pos, float* force);
66
67     // Applies a force at the center of gravity.
68     void addForce(float* force);
69
70     // Adds a torque with the specified axis and magnitude
71     void addTorque(float* torque);
72
73     // Sets the rotation rate of the body (about its c.g.) within the
74     // surrounding environment.  This is needed to compute torque on
75     // the body due to the centripetal forces involved in the
76     // rotation.  NOTE: the rotation vector, like all other
77     // coordinates used here, is specified IN THE LOCAL COORDINATE
78     // SYSTEM.
79     void setBodySpin(float* rotation);
80
81
82
83     // Returns the center of gravity of the masses, in the body
84     // coordinate system.
85     void getCG(float* cgOut);
86
87     // Returns the acceleration of the body's c.g. relative to the
88     // rest of the world, specified in local coordinates.
89     void getAccel(float* accelOut);
90
91     // Returns the acceleration of a specific location in local
92     // coordinates.  If the body is rotating, this will be different
93     // from the c.g. acceleration due to the centripetal accelerations
94     // of points not on the rotation axis.
95     void getAccel(float* pos, float* accelOut);
96
97     // Returns the instantaneous rate of change of the angular
98     // velocity, as a vector in local coordinates.
99     void getAngularAccel(float* accelOut);
100
101 private:
102     struct Mass { float m; float p[3]; };
103
104     // Internal "rotational structure"
105     Mass* _masses;
106     int   _nMasses;
107     int   _massesAlloced;
108     float _totalMass;
109     float _cg[3];
110     float _gyro[3];
111
112     // Inertia tensor, and its inverse.  Computed from the above.
113     float _tI[9];
114     float _invI[9];
115
116     // Externally determined quantities
117     float _force[3];
118     float _torque[3];
119     float _spin[3];
120 };
121
122 }; // namespace yasim
123 #endif // _RIGIDBODY_HPP