| 1 | #ifndef _BODYENVIRONMENT_HPP |
| 2 | #define _BODYENVIRONMENT_HPP |
| 3 | |
| 4 | #include "RigidBody.hpp" |
| 5 | #include "Math.hpp" |
| 6 | |
| 7 | namespace yasim { |
| 8 | |
| 9 | // The values for position and orientation within the global reference |
| 10 | // frame, along with their first and second time derivatives. The |
| 11 | // orientation is stored as a matrix for simplicity. Note that |
| 12 | // because it is orthonormal, its inverse is simply its transpose. |
| 13 | // You can get local->global transformations by calling Math::tmul33() |
| 14 | // and using the same matrix. |
| 15 | struct State { |
| 16 | double pos[3] {0, 0, 0}; // position |
| 17 | float orient[9] {1,0,0, 0,1,0, 0,0,1}; // global->local xform matrix |
| 18 | float v[3] {0, 0, 0}; // velocity |
| 19 | float rot[3] {0, 0, 0}; // rotational velocity |
| 20 | float acc[3] {0, 0, 0}; // acceleration |
| 21 | float racc[3] {0, 0, 0}; // rotational acceleration |
| 22 | double dt {0}; // time offset |
| 23 | |
| 24 | // Simple initialization |
| 25 | State() { |
| 26 | } |
| 27 | |
| 28 | void posLocalToGlobal(const float* lpos, double *gpos) const { |
| 29 | float tmp[3]; |
| 30 | Math::tmul33(orient, lpos, tmp); |
| 31 | gpos[0] = tmp[0] + pos[0]; |
| 32 | gpos[1] = tmp[1] + pos[1]; |
| 33 | gpos[2] = tmp[2] + pos[2]; |
| 34 | } |
| 35 | |
| 36 | void posGlobalToLocal(const double* gpos, float *lpos) const { |
| 37 | lpos[0] = (float)(gpos[0] - pos[0]); |
| 38 | lpos[1] = (float)(gpos[1] - pos[1]); |
| 39 | lpos[2] = (float)(gpos[2] - pos[2]); |
| 40 | Math::vmul33(orient, lpos, lpos); |
| 41 | } |
| 42 | |
| 43 | void localToGlobal(const float* local, float *global) const { |
| 44 | Math::tmul33(orient, local, global); |
| 45 | } |
| 46 | |
| 47 | void globalToLocal(const float* global, float *local) const { |
| 48 | Math::vmul33(orient, global, local); |
| 49 | } |
| 50 | |
| 51 | void planeGlobalToLocal(const double* gplane, float *lplane) const { |
| 52 | // First the normal vector transformed to local coordinates. |
| 53 | lplane[0] = (float)-gplane[0]; |
| 54 | lplane[1] = (float)-gplane[1]; |
| 55 | lplane[2] = (float)-gplane[2]; |
| 56 | Math::vmul33(orient, lplane, lplane); |
| 57 | |
| 58 | // Then the distance from the plane to the Aircraft's origin. |
| 59 | lplane[3] = (float)(pos[0]*gplane[0] + pos[1]*gplane[1] |
| 60 | + pos[2]*gplane[2] - gplane[3]); |
| 61 | } |
| 62 | |
| 63 | // used by Airplane::runCruise, runApproach, solveHelicopter and in yasim-test |
| 64 | void setupOrientationFromAoa(float aoa) |
| 65 | { |
| 66 | float cosAoA = Math::cos(aoa); |
| 67 | float sinAoA = Math::sin(aoa); |
| 68 | orient[0] = cosAoA; orient[1] = 0; orient[2] = sinAoA; |
| 69 | orient[3] = 0; orient[4] = 1; orient[5] = 0; |
| 70 | orient[6] = -sinAoA; orient[7] = 0; orient[8] = cosAoA; |
| 71 | } |
| 72 | |
| 73 | void setupSpeedAndPosition(float speed, float gla) |
| 74 | { |
| 75 | |
| 76 | // FIXME check axis, guess sin should go to 2 instead of 1? |
| 77 | v[0] = speed*Math::cos(gla); |
| 78 | v[1] = -speed*Math::sin(gla); |
| 79 | v[2] = 0; |
| 80 | for(int i=0; i<3; i++) { |
| 81 | pos[i] = rot[i] = acc[i] = racc[i] = 0; |
| 82 | } |
| 83 | |
| 84 | pos[2] = 1; |
| 85 | } |
| 86 | |
| 87 | void setupState(float aoa, float speed, float gla) { |
| 88 | setupOrientationFromAoa(aoa); |
| 89 | setupSpeedAndPosition(speed, gla); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | // |
| 94 | // Objects implementing this interface are responsible for calculating |
| 95 | // external forces on a RigidBody object. These will then be used by |
| 96 | // an Integrator to decide on a new solution to the state equations, |
| 97 | // which will be reported to the BodyEnvironment for further action. |
| 98 | // |
| 99 | class BodyEnvironment |
| 100 | { |
| 101 | public: |
| 102 | // This method inspects the "environment" in which a RigidBody |
| 103 | // exists, calculates forces and torques on the body, and sets |
| 104 | // them via addForce()/addTorque(). Because this method is called |
| 105 | // multiple times ("trials") as part of a Runge-Kutta integration, |
| 106 | // this is NOT the place to make decisions about anything but the |
| 107 | // forces on the object. Note that the acc and racc fields of the |
| 108 | // passed-in State object are undefined! (They are calculed BY |
| 109 | // this method). |
| 110 | virtual void calcForces(State* state) = 0; |
| 111 | |
| 112 | // Called when the RK4 integrator has determined a "real" new |
| 113 | // point on the curve of life. Any side-effect producing checks |
| 114 | // of body state vs. the environment can happen here (crashes, |
| 115 | // etc...). |
| 116 | virtual void newState(State* state) = 0; |
| 117 | |
| 118 | virtual ~BodyEnvironment() {} // #!$!?! gcc warning... |
| 119 | }; |
| 120 | |
| 121 | }; // namespace yasim |
| 122 | #endif // _BODYENVIRONMENT_HPP |