projectile.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/wwphys/projectile.h $*
  25. * *
  26. * Author:: Byon_g *
  27. * *
  28. * $Modtime:: 8/17/01 8:25p $*
  29. * *
  30. * $Revision:: 26 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * ProjectileClass::Get_Velocity -- returns the velocity of this projectile *
  35. * ProjectileClass::Get_Angular_Velocity -- returns the angular velocity *
  36. * ProjectileClass::Set_Velocity -- Sets the velocity of this projectile *
  37. * ProjectileClass::Set_Angular_Velocity -- This will set the tumble rate for the projectile *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. #if defined(_MSC_VER)
  40. #pragma once
  41. #endif
  42. #ifndef PROJECTILE_H
  43. #define PROJECTILE_H
  44. #include "always.h"
  45. #include "movephys.h"
  46. #include "ode.h"
  47. /*
  48. ** Projectile State Struct
  49. ** This is the state vector for a Projectile. It has three
  50. ** degrees of freedom in translation
  51. */
  52. struct ProjectileStateStruct
  53. {
  54. Vector3 Position;
  55. Vector3 Velocity;
  56. };
  57. class ProjectileDefClass;
  58. /*
  59. ** ProjectileClass
  60. **
  61. ** This class is used for things like grenades which do not need to have "size" and
  62. ** do not allow other objects to collide with them. Projectiles are basically
  63. ** an optimization, they are represented only by a point to the collision system
  64. ** and nothing will ever try to collide with them.
  65. */
  66. class ProjectileClass : public MoveablePhysClass
  67. {
  68. public:
  69. ProjectileClass(void);
  70. virtual ~ProjectileClass(void);
  71. virtual ProjectileClass * As_ProjectileClass(void) { return this; }
  72. void Init(const ProjectileDefClass & def);
  73. virtual void Timestep(float dt);
  74. virtual bool Cast_Ray(PhysRayCollisionTestClass & raytest);
  75. virtual const AABoxClass & Get_Bounding_Box(void) const;
  76. virtual const Matrix3D & Get_Transform(void) const;
  77. virtual void Set_Transform(const Matrix3D & m);
  78. virtual void Get_Velocity(Vector3 * set_vel) const;
  79. virtual void Get_Angular_Velocity(Vector3 * set_avel) const;
  80. virtual void Set_Velocity(const Vector3 & newvel);
  81. virtual void Set_Angular_Velocity(const Vector3 & newavel);
  82. virtual void Set_Lifetime( float time );
  83. virtual float Get_Lifetime( void );
  84. virtual void Set_Bounce_Count(int count);
  85. virtual int Get_Bounce_Count(void);
  86. virtual void Apply_Impulse(const Vector3 & imp) { }
  87. virtual void Apply_Impulse(const Vector3 & imp,const Vector3 & wpos) { }
  88. /*
  89. ** Collides on Move
  90. */
  91. virtual void Set_Collides_On_Move( bool onoff ) { CollidesOnMove = onoff; }
  92. /*
  93. ** Control over the method used to generate the orientation of the projectile. Projectiles
  94. ** don't have true "physical" orientation; they provide a couple of methods for generating
  95. ** an orientation: aligned with their path, fixed, or tumbling.
  96. */
  97. void Set_Orientation_Mode_Aligned(void) { OrientationMode = ORIENTATION_ALIGNED; }
  98. void Set_Orientation_Mode_Fixed(void) { OrientationMode = ORIENTATION_FIXED; }
  99. void Set_Orientation_Mode_Aligned_Fixed(void);
  100. void Set_Orientation_Mode_Tumbling(void);
  101. void Set_Orientation_Mode_Tumbling(const Vector3 & axis,float rate);
  102. /*
  103. ** Save-Load system
  104. */
  105. virtual const PersistFactoryClass & Get_Factory (void) const;
  106. virtual bool Save (ChunkSaveClass &csave);
  107. virtual bool Load (ChunkLoadClass &cload);
  108. protected:
  109. /*
  110. ** Internal functions
  111. */
  112. void Integrate(float dt);
  113. void Update_Transform(float dt);
  114. // state of the projectile
  115. ProjectileStateStruct State;
  116. // Collides on Move
  117. bool CollidesOnMove;
  118. // Orientation of the projectile
  119. enum
  120. {
  121. ORIENTATION_ALIGNED = 0,
  122. ORIENTATION_FIXED,
  123. ORIENTATION_TUMBLING,
  124. ORIENTATION_ALIGNED_FIXED,
  125. };
  126. int OrientationMode;
  127. Vector3 TumbleAxis;
  128. float TumbleRate;
  129. float Lifetime;
  130. int BounceCount;
  131. private:
  132. // not implemented
  133. ProjectileClass(const ProjectileClass & that);
  134. ProjectileClass & operator = (const ProjectileClass & that);
  135. friend class ProjectileDefClass;
  136. };
  137. /***********************************************************************************************
  138. * ProjectileClass::Get_Velocity -- returns the velocity of this projectile *
  139. * *
  140. * INPUT: *
  141. * *
  142. * OUTPUT: *
  143. * *
  144. * WARNINGS: *
  145. * *
  146. * HISTORY: *
  147. * 9/18/98 GTH : Created. *
  148. *=============================================================================================*/
  149. inline void ProjectileClass::Get_Velocity(Vector3 * set_vel) const
  150. {
  151. *set_vel = State.Velocity;
  152. }
  153. /***********************************************************************************************
  154. * ProjectileClass::Get_Angular_Velocity -- returns the angular velocity *
  155. * *
  156. * Projectiles dont really have angular velocity so this always returns 0,0,0 *
  157. * *
  158. * INPUT: *
  159. * *
  160. * OUTPUT: *
  161. * *
  162. * WARNINGS: *
  163. * *
  164. * HISTORY: *
  165. * 9/18/98 GTH : Created. *
  166. *=============================================================================================*/
  167. inline void ProjectileClass::Get_Angular_Velocity(Vector3 * set_avel) const
  168. {
  169. set_avel->Set(0,0,0);
  170. }
  171. /***********************************************************************************************
  172. * ProjectileClass::Set_Velocity -- Sets the velocity of this projectile *
  173. * *
  174. * INPUT: *
  175. * *
  176. * OUTPUT: *
  177. * *
  178. * WARNINGS: *
  179. * *
  180. * HISTORY: *
  181. * 9/18/98 GTH : Created. *
  182. *=============================================================================================*/
  183. inline void ProjectileClass::Set_Velocity(const Vector3 & newvel)
  184. {
  185. State.Velocity = newvel;
  186. if (OrientationMode == ORIENTATION_ALIGNED_FIXED) {
  187. Matrix3D tm;
  188. tm.Obj_Look_At(State.Position,State.Position + State.Velocity,0.0f);
  189. Model->Set_Transform(tm);
  190. }
  191. }
  192. /***********************************************************************************************
  193. * ProjectileClass::Set_Angular_Velocity -- This will set the tumble rate for the projectile *
  194. * *
  195. * INPUT: *
  196. * *
  197. * OUTPUT: *
  198. * *
  199. * WARNINGS: *
  200. * Note that the tumble rate is only used if you put the projectile into "tumble" mode *
  201. * *
  202. * HISTORY: *
  203. * 9/18/98 GTH : Created. *
  204. *=============================================================================================*/
  205. inline void ProjectileClass::Set_Angular_Velocity(const Vector3 & newavel)
  206. {
  207. TumbleAxis = newavel;
  208. TumbleRate = TumbleAxis.Length();
  209. TumbleAxis *= (1.0f / TumbleRate);
  210. }
  211. /**
  212. ** ProjectileDefClass
  213. ** Initialization/Game-Database support for Phys3Class
  214. */
  215. class ProjectileDefClass : public MoveablePhysDefClass
  216. {
  217. public:
  218. ProjectileDefClass(void);
  219. // From Definition
  220. virtual uint32 Get_Class_ID (void) const;
  221. virtual PersistClass * Create(void) const;
  222. // From PhysDefClass
  223. virtual const char * Get_Type_Name(void) { return "ProjectileDef"; }
  224. virtual bool Is_Type(const char *);
  225. // From PersistClass
  226. virtual const PersistFactoryClass & Get_Factory (void) const;
  227. virtual bool Save(ChunkSaveClass &csave);
  228. virtual bool Load(ChunkLoadClass &cload);
  229. // Editable interface requirements
  230. DECLARE_EDITABLE(ProjectileDefClass,MoveablePhysDefClass);
  231. protected:
  232. bool CollidesOnMove;
  233. int OrientationMode;
  234. Vector3 TumbleAxis;
  235. float TumbleRate;
  236. float Lifetime;
  237. int BounceCount;
  238. friend class ProjectileClass;
  239. };
  240. #endif