physcon.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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: /Commando/Code/wwphys/physcon.cpp 13 9/20/01 5:12p Greg_h $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando *
  24. * *
  25. * $Archive:: /Commando/Code/wwphys/physcon.cpp $*
  26. * *
  27. * Author:: Greg_h *
  28. * *
  29. * $Modtime:: 9/19/01 7:56p $*
  30. * *
  31. * $Revision:: 13 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include "physcon.h"
  37. #include "chunkio.h"
  38. #include "wwdebug.h"
  39. #include "w3d_file.h"
  40. /*
  41. ** Static instance of PhysicsConstants so that the constructor gets called and all arrays are filled
  42. */
  43. static PhysicsConstants _PhysicsConstantsObject;
  44. /*
  45. ** Physics Constants - see the header file for explanation
  46. */
  47. Vector3 PhysicsConstants::GravityAcceleration(0.0f,0.0f,-9.8f);
  48. float PhysicsConstants::LinearDamping = 0.01f;
  49. float PhysicsConstants::AngularDamping = 0.05f;
  50. float PhysicsConstants::RestingContactVelocity = 0.1f;
  51. float PhysicsConstants::MinFrictionVelocity = 0.05f;
  52. float PhysicsConstants::DefaultContactFriction = 0.5f;
  53. float PhysicsConstants::DefaultContactDrag = 0.0f;
  54. int PhysicsConstants::SurfaceTypeOverride = -1;
  55. float PhysicsConstants::OverrideDrag = 0.0f;
  56. float PhysicsConstants::OverrideFriction = 0.0f;
  57. /*
  58. ** these are all constants computed from the above constants
  59. */
  60. float PhysicsConstants::MinFrictionVelocity2 = (PhysicsConstants::MinFrictionVelocity * PhysicsConstants::MinFrictionVelocity);
  61. /*
  62. ** These are constants which are not exposed in the header file
  63. ** Note that SURFACE_TYPE_MAX comes from w3d_file.h
  64. */
  65. float ContactFrictionCoefficient[PhysicsConstants::DYNAMIC_OBJ_TYPE_MAX][SURFACE_TYPE_MAX];
  66. float ContactDragCoefficient[PhysicsConstants::DYNAMIC_OBJ_TYPE_MAX][SURFACE_TYPE_MAX];
  67. float test[2][2] = { 1,2,3,4 };
  68. /*
  69. ** PhysicsConstants Member functions
  70. */
  71. void PhysicsConstants::Init(void)
  72. {
  73. for (int obj_type=0; obj_type < DYNAMIC_OBJ_TYPE_MAX; obj_type++) {
  74. for (int surf_type=0; surf_type < SURFACE_TYPE_MAX; surf_type++) {
  75. ContactFrictionCoefficient[obj_type][surf_type] = DefaultContactFriction;
  76. ContactDragCoefficient[obj_type][surf_type] = DefaultContactDrag;
  77. }
  78. }
  79. }
  80. void PhysicsConstants::Set_Contact_Friction_Coefficient(int obj_type,int surface_type,float friction)
  81. {
  82. if ((obj_type < 0) || (obj_type > DYNAMIC_OBJ_TYPE_MAX)) {
  83. WWDEBUG_SAY(("PhysicsConstants -> Out of range dynamic object type!\n"));
  84. return;
  85. }
  86. if ((surface_type < 0) || (surface_type > SURFACE_TYPE_MAX)) {
  87. WWDEBUG_SAY(("PhysicsConstants -> Out of range surface type!\n"));
  88. return;
  89. }
  90. ContactFrictionCoefficient[obj_type][surface_type] = friction;
  91. }
  92. float PhysicsConstants::Get_Contact_Friction_Coefficient(int obj_type,int surface_type)
  93. {
  94. if (SurfaceTypeOverride != -1) surface_type = SurfaceTypeOverride;
  95. if (OverrideFriction != 0.0f) return OverrideFriction;
  96. if ((obj_type < 0) || (obj_type > DYNAMIC_OBJ_TYPE_MAX)) {
  97. WWDEBUG_SAY(("PhysicsConstants -> Out of range dynamic object type!\n"));
  98. return ContactFrictionCoefficient[0][0];
  99. }
  100. if ((surface_type < 0) || (surface_type > SURFACE_TYPE_MAX)) {
  101. WWDEBUG_SAY(("PhysicsConstants -> Out of range surface type!\n"));
  102. return ContactFrictionCoefficient[0][0];
  103. }
  104. return ContactFrictionCoefficient[obj_type][surface_type];
  105. }
  106. void PhysicsConstants::Set_Contact_Drag_Coefficient(int obj_type,int surface_type,float drag)
  107. {
  108. if ((obj_type < 0) || (obj_type > DYNAMIC_OBJ_TYPE_MAX)) {
  109. WWDEBUG_SAY(("PhysicsConstants -> Out of range dynamic object type!\n"));
  110. return;
  111. }
  112. if ((surface_type < 0) || (surface_type > SURFACE_TYPE_MAX)) {
  113. WWDEBUG_SAY(("PhysicsConstants -> Out of range surface type!\n"));
  114. return;
  115. }
  116. ContactDragCoefficient[obj_type][surface_type] = drag;
  117. }
  118. float PhysicsConstants::Get_Contact_Drag_Coefficient(int obj_type,int surface_type)
  119. {
  120. if (SurfaceTypeOverride != -1) surface_type = SurfaceTypeOverride;
  121. if (OverrideDrag != 0.0f) return OverrideDrag;
  122. if ((obj_type < 0) || (obj_type > DYNAMIC_OBJ_TYPE_MAX)) {
  123. WWDEBUG_SAY(("PhysicsConstants -> Out of range dynamic object type!\n"));
  124. return ContactDragCoefficient[0][0];
  125. }
  126. if ((surface_type < 0) || (surface_type > SURFACE_TYPE_MAX)) {
  127. WWDEBUG_SAY(("PhysicsConstants -> Out of range surface type!\n"));
  128. return ContactDragCoefficient[0][0];
  129. }
  130. return ContactDragCoefficient[obj_type][surface_type];
  131. }
  132. void PhysicsConstants::Set_Override_Surface_Type(int type)
  133. {
  134. if ((type <0) || (type > SURFACE_TYPE_MAX)) {
  135. SurfaceTypeOverride = -1;
  136. } else {
  137. SurfaceTypeOverride = type;
  138. }
  139. }
  140. void PhysicsConstants::Set_Override_Surface_Friction(float friction)
  141. {
  142. OverrideFriction = WWMath::Clamp(friction);
  143. }
  144. void PhysicsConstants::Set_Override_Surface_Drag(float drag)
  145. {
  146. OverrideDrag = drag;
  147. }
  148. /*
  149. ** Save-Load Support
  150. */
  151. enum
  152. {
  153. PHYSCONSTANTS_CHUNK_VARIABLES = 0x00044005,
  154. PHYSCONSTANT_GRAVITYACCELERATION = 0x00,
  155. PHYSCONSTANT_LINEARDAMPING,
  156. PHYSCONSTANT_ANGULARDAMPING,
  157. PHYSCONSTANT_RESTINGCONTACTVELOCITY,
  158. PHYSCONSTANT_MINFRICTIONVELOCITY,
  159. PHYSCONSTANT_DEFAULTCONTACTFRICTION,
  160. PHYSCONSTANT_MINFRICTIONVELOCITY2
  161. };
  162. void PhysicsConstants::Save(ChunkSaveClass & csave)
  163. {
  164. csave.Begin_Chunk(PHYSCONSTANTS_CHUNK_VARIABLES);
  165. WRITE_MICRO_CHUNK(csave,PHYSCONSTANT_GRAVITYACCELERATION,GravityAcceleration);
  166. WRITE_MICRO_CHUNK(csave,PHYSCONSTANT_LINEARDAMPING,LinearDamping);
  167. WRITE_MICRO_CHUNK(csave,PHYSCONSTANT_ANGULARDAMPING,AngularDamping);
  168. WRITE_MICRO_CHUNK(csave,PHYSCONSTANT_RESTINGCONTACTVELOCITY,RestingContactVelocity);
  169. WRITE_MICRO_CHUNK(csave,PHYSCONSTANT_MINFRICTIONVELOCITY,MinFrictionVelocity);
  170. WRITE_MICRO_CHUNK(csave,PHYSCONSTANT_DEFAULTCONTACTFRICTION,DefaultContactFriction);
  171. WRITE_MICRO_CHUNK(csave,PHYSCONSTANT_MINFRICTIONVELOCITY2,MinFrictionVelocity2);
  172. csave.End_Chunk();
  173. }
  174. void PhysicsConstants::Load(ChunkLoadClass & cload)
  175. {
  176. while (cload.Open_Chunk()) {
  177. switch(cload.Cur_Chunk_ID())
  178. {
  179. case PHYSCONSTANTS_CHUNK_VARIABLES:
  180. while (cload.Open_Micro_Chunk()) {
  181. switch(cload.Cur_Micro_Chunk_ID()) {
  182. READ_MICRO_CHUNK(cload,PHYSCONSTANT_GRAVITYACCELERATION,GravityAcceleration);
  183. READ_MICRO_CHUNK(cload,PHYSCONSTANT_LINEARDAMPING,LinearDamping);
  184. READ_MICRO_CHUNK(cload,PHYSCONSTANT_ANGULARDAMPING,AngularDamping);
  185. READ_MICRO_CHUNK(cload,PHYSCONSTANT_RESTINGCONTACTVELOCITY,RestingContactVelocity);
  186. READ_MICRO_CHUNK(cload,PHYSCONSTANT_MINFRICTIONVELOCITY,MinFrictionVelocity);
  187. READ_MICRO_CHUNK(cload,PHYSCONSTANT_DEFAULTCONTACTFRICTION,DefaultContactFriction);
  188. READ_MICRO_CHUNK(cload,PHYSCONSTANT_MINFRICTIONVELOCITY2,MinFrictionVelocity2);
  189. }
  190. cload.Close_Micro_Chunk();
  191. }
  192. break;
  193. default:
  194. WWDEBUG_SAY(("Unhandled Chunk: 0x%X File: %s Line: %d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  195. break;
  196. }
  197. cload.Close_Chunk();
  198. }
  199. }