Force_Drag.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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( idForce, idForce_Drag )
  24. END_CLASS
  25. /*
  26. ================
  27. idForce_Drag::idForce_Drag
  28. ================
  29. */
  30. idForce_Drag::idForce_Drag( void ) {
  31. damping = 0.5f;
  32. dragPosition = vec3_zero;
  33. physics = NULL;
  34. id = 0;
  35. p = vec3_zero;
  36. dragPosition = vec3_zero;
  37. }
  38. /*
  39. ================
  40. idForce_Drag::~idForce_Drag
  41. ================
  42. */
  43. idForce_Drag::~idForce_Drag( void ) {
  44. }
  45. /*
  46. ================
  47. idForce_Drag::Init
  48. ================
  49. */
  50. void idForce_Drag::Init( float damping ) {
  51. if ( damping >= 0.0f && damping < 1.0f ) {
  52. this->damping = damping;
  53. }
  54. }
  55. /*
  56. ================
  57. idForce_Drag::SetPhysics
  58. ================
  59. */
  60. void idForce_Drag::SetPhysics( idPhysics *phys, int id, const idVec3 &p ) {
  61. this->physics = phys;
  62. this->id = id;
  63. this->p = p;
  64. }
  65. /*
  66. ================
  67. idForce_Drag::SetDragPosition
  68. ================
  69. */
  70. void idForce_Drag::SetDragPosition( const idVec3 &pos ) {
  71. this->dragPosition = pos;
  72. }
  73. /*
  74. ================
  75. idForce_Drag::GetDragPosition
  76. ================
  77. */
  78. const idVec3 &idForce_Drag::GetDragPosition( void ) const {
  79. return this->dragPosition;
  80. }
  81. /*
  82. ================
  83. idForce_Drag::GetDraggedPosition
  84. ================
  85. */
  86. const idVec3 idForce_Drag::GetDraggedPosition( void ) const {
  87. return ( physics->GetOrigin( id ) + p * physics->GetAxis( id ) );
  88. }
  89. /*
  90. ================
  91. idForce_Drag::Evaluate
  92. ================
  93. */
  94. void idForce_Drag::Evaluate( int time ) {
  95. float l1, l2, mass;
  96. idVec3 dragOrigin, dir1, dir2, velocity, centerOfMass;
  97. idMat3 inertiaTensor;
  98. idRotation rotation;
  99. idClipModel *clipModel;
  100. if ( !physics ) {
  101. return;
  102. }
  103. clipModel = physics->GetClipModel( id );
  104. if ( clipModel != NULL && clipModel->IsTraceModel() ) {
  105. clipModel->GetMassProperties( 1.0f, mass, centerOfMass, inertiaTensor );
  106. } else {
  107. centerOfMass.Zero();
  108. }
  109. centerOfMass = physics->GetOrigin( id ) + centerOfMass * physics->GetAxis( id );
  110. dragOrigin = physics->GetOrigin( id ) + p * physics->GetAxis( id );
  111. dir1 = dragPosition - centerOfMass;
  112. dir2 = dragOrigin - centerOfMass;
  113. l1 = dir1.Normalize();
  114. l2 = dir2.Normalize();
  115. rotation.Set( centerOfMass, dir2.Cross( dir1 ), RAD2DEG( idMath::ACos( dir1 * dir2 ) ) );
  116. physics->SetAngularVelocity( rotation.ToAngularVelocity() / MS2SEC( USERCMD_MSEC ), id );
  117. velocity = physics->GetLinearVelocity( id ) * damping + dir1 * ( ( l1 - l2 ) * ( 1.0f - damping ) / MS2SEC( USERCMD_MSEC ) );
  118. physics->SetLinearVelocity( velocity, id );
  119. }
  120. /*
  121. ================
  122. idForce_Drag::RemovePhysics
  123. ================
  124. */
  125. void idForce_Drag::RemovePhysics( const idPhysics *phys ) {
  126. if ( physics == phys ) {
  127. physics = NULL;
  128. }
  129. }