ode.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /G/wwmath/ode.h 9 9/21/99 5:54p Neal_k $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando *
  24. * *
  25. * $Archive:: /G/wwmath/ode.h $*
  26. * *
  27. * Author:: Greg_h *
  28. * *
  29. * $Modtime:: 9/21/99 5:54p $*
  30. * *
  31. * $Revision:: 9 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #if defined(_MSC_VER)
  37. #pragma once
  38. #endif
  39. #ifndef ODE_H
  40. #define ODE_H
  41. #include "always.h"
  42. #include "vector.h"
  43. #include "wwdebug.h"
  44. /*
  45. ** StateVectorClass
  46. ** The state vector for a system of ordinary differential equations will be
  47. ** stored in this form. It is a dynamically resizeable array so that we don't
  48. ** have to hard-code a maximum size. If needed, in the final product, we could
  49. ** do a slight optimization which makes this a normal fixed size array that
  50. ** we've determined is "big enough".
  51. */
  52. class StateVectorClass : public DynamicVectorClass<float>
  53. {
  54. public:
  55. void Reset(void) { ActiveCount = 0; }
  56. void Resize(int size) { if (size > VectorMax) { DynamicVectorClass<float>::Resize(size); } }
  57. };
  58. /*
  59. ** ODESystemClass
  60. ** If a system of Ordinary Differential Equations (ODE's) are put behind an interface
  61. ** of this type, they can be integrated using the Integrators defined in this module.
  62. */
  63. class ODESystemClass
  64. {
  65. public:
  66. /*
  67. ** Get_Current_State
  68. ** This function should fill the given state vector with the
  69. ** current state of this object. Each state variable should be
  70. ** inserted into the vector using its 'Add' interface.
  71. */
  72. virtual void Get_State(StateVectorClass & set_state) = 0;
  73. /*
  74. ** Set_Current_State
  75. ** This function should read its state from this state vector starting from the
  76. ** given index. The return value should be the index that the next object should
  77. ** read from (i.e. increment the index past your state)
  78. */
  79. virtual int Set_State(const StateVectorClass & new_state,int start_index = 0) = 0;
  80. /*
  81. ** Compute_Derivatives
  82. ** The various ODE solvers will use this interface to ask the ODESystemClass to
  83. ** compute the derivatives of their state. In some cases, the integrator will
  84. ** pass in a new state vector (test_state) to be used when computing the derivatives.
  85. ** NULL will be passed if they want the derivatives for the initial state.
  86. ** This function works similarly to the Set_State function in that it passes you
  87. ** the index to start reading from and you pass it back the index to continue from.
  88. */
  89. virtual int Compute_Derivatives(float t,StateVectorClass * test_state,StateVectorClass * dydt,int start_index = 0) = 0;
  90. };
  91. /*
  92. ** IntegrationSystem
  93. **
  94. ** The Euler_Solve is the simplest but most inaccurate. It requires only
  95. ** a single computation of the derivatives per timestep.
  96. **
  97. ** The Midpoint_Solve function will evaluate the derivatives at two points
  98. **
  99. ** The Runge_Kutta_Solve requires four evaluations of the derivatives.
  100. ** This is the fourth order Runge-Kutta method...
  101. **
  102. ** Runge_Kutta5_Solve is an implementation of fifth order Runge-
  103. ** Kutta. It requires six evaluations of the derivatives.
  104. */
  105. class IntegrationSystem
  106. {
  107. public:
  108. static void Euler_Integrate(ODESystemClass * sys,float dt);
  109. static void Midpoint_Integrate(ODESystemClass * sys,float dt);
  110. static void Runge_Kutta_Integrate(ODESystemClass * sys,float dt);
  111. static void Runge_Kutta5_Integrate(ODESystemClass * odesys,float dt);
  112. };
  113. #endif