Physics_Actor.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "../Game_local.h"
  23. CLASS_DECLARATION( idPhysics_Base, idPhysics_Actor )
  24. END_CLASS
  25. /*
  26. ================
  27. idPhysics_Actor::idPhysics_Actor
  28. ================
  29. */
  30. idPhysics_Actor::idPhysics_Actor( void ) {
  31. clipModel = NULL;
  32. SetClipModelAxis();
  33. mass = 100.0f;
  34. invMass = 1.0f / mass;
  35. masterEntity = NULL;
  36. masterYaw = 0.0f;
  37. masterDeltaYaw = 0.0f;
  38. groundEntityPtr = NULL;
  39. }
  40. /*
  41. ================
  42. idPhysics_Actor::~idPhysics_Actor
  43. ================
  44. */
  45. idPhysics_Actor::~idPhysics_Actor( void ) {
  46. if ( clipModel ) {
  47. delete clipModel;
  48. clipModel = NULL;
  49. }
  50. }
  51. /*
  52. ================
  53. idPhysics_Actor::Save
  54. ================
  55. */
  56. void idPhysics_Actor::Save( idSaveGame *savefile ) const {
  57. savefile->WriteClipModel( clipModel );
  58. savefile->WriteMat3( clipModelAxis );
  59. savefile->WriteFloat( mass );
  60. savefile->WriteFloat( invMass );
  61. savefile->WriteObject( masterEntity );
  62. savefile->WriteFloat( masterYaw );
  63. savefile->WriteFloat( masterDeltaYaw );
  64. groundEntityPtr.Save( savefile );
  65. }
  66. /*
  67. ================
  68. idPhysics_Actor::Restore
  69. ================
  70. */
  71. void idPhysics_Actor::Restore( idRestoreGame *savefile ) {
  72. savefile->ReadClipModel( clipModel );
  73. savefile->ReadMat3( clipModelAxis );
  74. savefile->ReadFloat( mass );
  75. savefile->ReadFloat( invMass );
  76. savefile->ReadObject( reinterpret_cast<idClass *&>( masterEntity ) );
  77. savefile->ReadFloat( masterYaw );
  78. savefile->ReadFloat( masterDeltaYaw );
  79. groundEntityPtr.Restore( savefile );
  80. }
  81. /*
  82. ================
  83. idPhysics_Actor::SetClipModelAxis
  84. ================
  85. */
  86. void idPhysics_Actor::SetClipModelAxis( void ) {
  87. // align clip model to gravity direction
  88. if ( ( gravityNormal[2] == -1.0f ) || ( gravityNormal == vec3_zero ) ) {
  89. clipModelAxis.Identity();
  90. }
  91. else {
  92. clipModelAxis[2] = -gravityNormal;
  93. clipModelAxis[2].NormalVectors( clipModelAxis[0], clipModelAxis[1] );
  94. clipModelAxis[1] = -clipModelAxis[1];
  95. }
  96. if ( clipModel ) {
  97. clipModel->Link( gameLocal.clip, self, 0, clipModel->GetOrigin(), clipModelAxis );
  98. }
  99. }
  100. /*
  101. ================
  102. idPhysics_Actor::GetGravityAxis
  103. ================
  104. */
  105. const idMat3 &idPhysics_Actor::GetGravityAxis( void ) const {
  106. return clipModelAxis;
  107. }
  108. /*
  109. ================
  110. idPhysics_Actor::GetMasterDeltaYaw
  111. ================
  112. */
  113. float idPhysics_Actor::GetMasterDeltaYaw( void ) const {
  114. return masterDeltaYaw;
  115. }
  116. /*
  117. ================
  118. idPhysics_Actor::GetGroundEntity
  119. ================
  120. */
  121. idEntity *idPhysics_Actor::GetGroundEntity( void ) const {
  122. return groundEntityPtr.GetEntity();
  123. }
  124. /*
  125. ================
  126. idPhysics_Actor::SetClipModel
  127. ================
  128. */
  129. void idPhysics_Actor::SetClipModel( idClipModel *model, const float density, int id, bool freeOld ) {
  130. assert( self );
  131. assert( model ); // a clip model is required
  132. assert( model->IsTraceModel() ); // and it should be a trace model
  133. assert( density > 0.0f ); // density should be valid
  134. if ( clipModel && clipModel != model && freeOld ) {
  135. delete clipModel;
  136. }
  137. clipModel = model;
  138. clipModel->Link( gameLocal.clip, self, 0, clipModel->GetOrigin(), clipModelAxis );
  139. }
  140. /*
  141. ================
  142. idPhysics_Actor::GetClipModel
  143. ================
  144. */
  145. idClipModel *idPhysics_Actor::GetClipModel( int id ) const {
  146. return clipModel;
  147. }
  148. /*
  149. ================
  150. idPhysics_Actor::GetNumClipModels
  151. ================
  152. */
  153. int idPhysics_Actor::GetNumClipModels( void ) const {
  154. return 1;
  155. }
  156. /*
  157. ================
  158. idPhysics_Actor::SetMass
  159. ================
  160. */
  161. void idPhysics_Actor::SetMass( float _mass, int id ) {
  162. assert( _mass > 0.0f );
  163. mass = _mass;
  164. invMass = 1.0f / _mass;
  165. }
  166. /*
  167. ================
  168. idPhysics_Actor::GetMass
  169. ================
  170. */
  171. float idPhysics_Actor::GetMass( int id ) const {
  172. return mass;
  173. }
  174. /*
  175. ================
  176. idPhysics_Actor::SetClipMask
  177. ================
  178. */
  179. void idPhysics_Actor::SetContents( int contents, int id ) {
  180. clipModel->SetContents( contents );
  181. }
  182. /*
  183. ================
  184. idPhysics_Actor::SetClipMask
  185. ================
  186. */
  187. int idPhysics_Actor::GetContents( int id ) const {
  188. return clipModel->GetContents();
  189. }
  190. /*
  191. ================
  192. idPhysics_Actor::GetBounds
  193. ================
  194. */
  195. const idBounds &idPhysics_Actor::GetBounds( int id ) const {
  196. return clipModel->GetBounds();
  197. }
  198. /*
  199. ================
  200. idPhysics_Actor::GetAbsBounds
  201. ================
  202. */
  203. const idBounds &idPhysics_Actor::GetAbsBounds( int id ) const {
  204. return clipModel->GetAbsBounds();
  205. }
  206. /*
  207. ================
  208. idPhysics_Actor::IsPushable
  209. ================
  210. */
  211. bool idPhysics_Actor::IsPushable( void ) const {
  212. return ( masterEntity == NULL );
  213. }
  214. /*
  215. ================
  216. idPhysics_Actor::GetOrigin
  217. ================
  218. */
  219. const idVec3 &idPhysics_Actor::GetOrigin( int id ) const {
  220. return clipModel->GetOrigin();
  221. }
  222. /*
  223. ================
  224. idPhysics_Player::GetAxis
  225. ================
  226. */
  227. const idMat3 &idPhysics_Actor::GetAxis( int id ) const {
  228. return clipModel->GetAxis();
  229. }
  230. /*
  231. ================
  232. idPhysics_Actor::SetGravity
  233. ================
  234. */
  235. void idPhysics_Actor::SetGravity( const idVec3 &newGravity ) {
  236. if ( newGravity != gravityVector ) {
  237. idPhysics_Base::SetGravity( newGravity );
  238. SetClipModelAxis();
  239. }
  240. }
  241. /*
  242. ================
  243. idPhysics_Actor::ClipTranslation
  244. ================
  245. */
  246. void idPhysics_Actor::ClipTranslation( trace_t &results, const idVec3 &translation, const idClipModel *model ) const {
  247. if ( model ) {
  248. gameLocal.clip.TranslationModel( results, clipModel->GetOrigin(), clipModel->GetOrigin() + translation,
  249. clipModel, clipModel->GetAxis(), clipMask,
  250. model->Handle(), model->GetOrigin(), model->GetAxis() );
  251. }
  252. else {
  253. gameLocal.clip.Translation( results, clipModel->GetOrigin(), clipModel->GetOrigin() + translation,
  254. clipModel, clipModel->GetAxis(), clipMask, self );
  255. }
  256. }
  257. /*
  258. ================
  259. idPhysics_Actor::ClipRotation
  260. ================
  261. */
  262. void idPhysics_Actor::ClipRotation( trace_t &results, const idRotation &rotation, const idClipModel *model ) const {
  263. if ( model ) {
  264. gameLocal.clip.RotationModel( results, clipModel->GetOrigin(), rotation,
  265. clipModel, clipModel->GetAxis(), clipMask,
  266. model->Handle(), model->GetOrigin(), model->GetAxis() );
  267. }
  268. else {
  269. gameLocal.clip.Rotation( results, clipModel->GetOrigin(), rotation,
  270. clipModel, clipModel->GetAxis(), clipMask, self );
  271. }
  272. }
  273. /*
  274. ================
  275. idPhysics_Actor::ClipContents
  276. ================
  277. */
  278. int idPhysics_Actor::ClipContents( const idClipModel *model ) const {
  279. if ( model ) {
  280. return gameLocal.clip.ContentsModel( clipModel->GetOrigin(), clipModel, clipModel->GetAxis(), -1,
  281. model->Handle(), model->GetOrigin(), model->GetAxis() );
  282. }
  283. else {
  284. return gameLocal.clip.Contents( clipModel->GetOrigin(), clipModel, clipModel->GetAxis(), -1, NULL );
  285. }
  286. }
  287. /*
  288. ================
  289. idPhysics_Actor::DisableClip
  290. ================
  291. */
  292. void idPhysics_Actor::DisableClip( void ) {
  293. clipModel->Disable();
  294. }
  295. /*
  296. ================
  297. idPhysics_Actor::EnableClip
  298. ================
  299. */
  300. void idPhysics_Actor::EnableClip( void ) {
  301. clipModel->Enable();
  302. }
  303. /*
  304. ================
  305. idPhysics_Actor::UnlinkClip
  306. ================
  307. */
  308. void idPhysics_Actor::UnlinkClip( void ) {
  309. clipModel->Unlink();
  310. }
  311. /*
  312. ================
  313. idPhysics_Actor::LinkClip
  314. ================
  315. */
  316. void idPhysics_Actor::LinkClip( void ) {
  317. clipModel->Link( gameLocal.clip, self, 0, clipModel->GetOrigin(), clipModel->GetAxis() );
  318. }
  319. /*
  320. ================
  321. idPhysics_Actor::EvaluateContacts
  322. ================
  323. */
  324. bool idPhysics_Actor::EvaluateContacts( void ) {
  325. // get all the ground contacts
  326. ClearContacts();
  327. AddGroundContacts( clipModel );
  328. AddContactEntitiesForContacts();
  329. return ( contacts.Num() != 0 );
  330. }