| 23 | } |
| 24 | |
| 25 | void PhysicsModule::Initialize(float time, float deltaTime, float theta1, float theta2, |
| 26 | float theta1Dot, float theta2Dot) |
| 27 | { |
| 28 | mTime = time; |
| 29 | |
| 30 | // State variables. |
| 31 | mState[0] = theta1; |
| 32 | mState[1] = theta1Dot; |
| 33 | mState[2] = theta2; |
| 34 | mState[3] = theta2Dot; |
| 35 | |
| 36 | // Auxiliary variables. |
| 37 | mAux[0] = gravity; |
| 38 | mAux[1] = length1; |
| 39 | mAux[2] = length2; |
| 40 | mAux[3] = mass2 / (mass1 + mass2); |
| 41 | |
| 42 | // RK4 differential equation solver. |
| 43 | std::function<Vector4<float>(float, Vector4<float> const&)> odeFunction |
| 44 | = |
| 45 | [this](float, Vector4<float> const& input) -> Vector4<float> |
| 46 | { |
| 47 | float angleD = input[0] - input[2]; |
| 48 | float csD = std::cos(angleD); |
| 49 | float snD = std::sin(angleD); |
| 50 | float invDet = 1.0f / (mAux[1] * mAux[2] * (1.0f - mAux[3] * csD * csD)); |
| 51 | float sn0 = std::sin(input[0]); |
| 52 | float sn2 = std::sin(input[2]); |
| 53 | float b1 = -mAux[0] * sn0 - mAux[3] * mAux[2] * snD*input[3] * input[3]; |
| 54 | float b2 = -mAux[0] * sn2 + mAux[1] * snD*input[1] * input[1]; |
| 55 | float theta1DotFunction = (b1 - mAux[3] * csD * b2) * mAux[2] * invDet; |
| 56 | float theta2DotFunction = (b2 - csD * b1) * mAux[1] * invDet; |
| 57 | |
| 58 | // (theta1, dot(theta1), theta2, dot(theta2) |
| 59 | return Vector4<float>{ input[1], theta1DotFunction, input[3], theta2DotFunction }; |
| 60 | }; |
| 61 | |
| 62 | mSolver = std::make_unique<Solver>(deltaTime, odeFunction); |
| 63 | } |
| 64 | |
| 65 | void PhysicsModule::GetPositions(float& x1, float& y1, float& x2, float& y2) const |
| 66 | { |
no test coverage detected