| 22 | } |
| 23 | |
| 24 | void PhysicsModule::Initialize(float time, float deltaTime, float y1, float y2, float y1Dot, float y2Dot) |
| 25 | { |
| 26 | mTime = time; |
| 27 | |
| 28 | // state variables |
| 29 | mState[0] = y1; // y1(0) |
| 30 | mState[1] = y1Dot; // y1'(0) |
| 31 | mState[2] = y2; // y2(0) |
| 32 | mState[3] = y2Dot; // y2'(0) |
| 33 | |
| 34 | // auxiliary variables |
| 35 | mAux[0] = a1 * a1; // a1^2 |
| 36 | mAux[1] = a2 * a2; // a2^2 |
| 37 | mAux[2] = gravity; // g |
| 38 | |
| 39 | // RK4 differential equation solver. |
| 40 | std::function<Vector4<float>(float, Vector4<float> const&)> odeFunction |
| 41 | = |
| 42 | [this](float, Vector4<float> const& input) -> Vector4<float> |
| 43 | { |
| 44 | float mat00 = mAux[0] + 4.0f * input[0] * input[0]; |
| 45 | float mat01 = 4.0f * input[0] * input[2]; |
| 46 | float mat11 = mAux[1] + 4.0f * input[2] * input[2]; |
| 47 | float invDet = 1.0f / (mat00*mat11 - mat01*mat01); |
| 48 | float sqrLen = input[1] * input[1] + input[3] * input[3]; |
| 49 | float rhs0 = 2.0f * input[0] * (mAux[2] - 2.0f * input[0] * sqrLen); |
| 50 | float rhs1 = 2.0f * input[2] * (mAux[2] - 2.0f * input[2] * sqrLen); |
| 51 | float y1Dot = (mat11 * rhs0 - mat01 * rhs1) * invDet; |
| 52 | float y2Dot = (mat00 * rhs1 - mat01 * rhs0) * invDet; |
| 53 | |
| 54 | // (y1, dot(y1), y2, dot(y2)) |
| 55 | return Vector4<float>{ input[1], y1Dot, input[3], y2Dot }; |
| 56 | }; |
| 57 | |
| 58 | mSolver = std::make_unique<Solver>(deltaTime, odeFunction); |
| 59 | } |
| 60 | |
| 61 | void PhysicsModule::GetData(Vector4<float>& center, Matrix4x4<float>& incrRot) const |
| 62 | { |
no outgoing calls
no test coverage detected