Force_Drag.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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. #pragma hdrstop
  21. #include "../../idlib/precompiled.h"
  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() {
  31. damping = 0.5f;
  32. physics = NULL;
  33. id = 0;
  34. p = vec3_zero;
  35. dragPosition = vec3_zero;
  36. }
  37. /*
  38. ================
  39. idForce_Drag::~idForce_Drag
  40. ================
  41. */
  42. idForce_Drag::~idForce_Drag() {
  43. }
  44. /*
  45. ================
  46. idForce_Drag::Init
  47. ================
  48. */
  49. void idForce_Drag::Init( float damping ) {
  50. if ( damping >= 0.0f && damping < 1.0f ) {
  51. this->damping = damping;
  52. }
  53. }
  54. /*
  55. ================
  56. idForce_Drag::SetPhysics
  57. ================
  58. */
  59. void idForce_Drag::SetPhysics( idPhysics *phys, int id, const idVec3 &p ) {
  60. this->physics = phys;
  61. this->id = id;
  62. this->p = p;
  63. }
  64. /*
  65. ================
  66. idForce_Drag::SetDragPosition
  67. ================
  68. */
  69. void idForce_Drag::SetDragPosition( const idVec3 &pos ) {
  70. this->dragPosition = pos;
  71. }
  72. /*
  73. ================
  74. idForce_Drag::GetDragPosition
  75. ================
  76. */
  77. const idVec3 &idForce_Drag::GetDragPosition() const {
  78. return this->dragPosition;
  79. }
  80. /*
  81. ================
  82. idForce_Drag::GetDraggedPosition
  83. ================
  84. */
  85. const idVec3 idForce_Drag::GetDraggedPosition() const {
  86. return ( physics->GetOrigin( id ) + p * physics->GetAxis( id ) );
  87. }
  88. /*
  89. ================
  90. idForce_Drag::Evaluate
  91. ================
  92. */
  93. void idForce_Drag::Evaluate( int time ) {
  94. float l1, l2, mass;
  95. idVec3 dragOrigin, dir1, dir2, velocity, centerOfMass;
  96. idMat3 inertiaTensor;
  97. idRotation rotation;
  98. idClipModel *clipModel;
  99. if ( !physics ) {
  100. return;
  101. }
  102. clipModel = physics->GetClipModel( id );
  103. if ( clipModel != NULL && clipModel->IsTraceModel() ) {
  104. clipModel->GetMassProperties( 1.0f, mass, centerOfMass, inertiaTensor );
  105. } else {
  106. centerOfMass.Zero();
  107. }
  108. centerOfMass = physics->GetOrigin( id ) + centerOfMass * physics->GetAxis( id );
  109. dragOrigin = physics->GetOrigin( id ) + p * physics->GetAxis( id );
  110. dir1 = dragPosition - centerOfMass;
  111. dir2 = dragOrigin - centerOfMass;
  112. l1 = dir1.Normalize();
  113. l2 = dir2.Normalize();
  114. rotation.Set( centerOfMass, dir2.Cross( dir1 ), RAD2DEG( idMath::ACos( dir1 * dir2 ) ) );
  115. physics->SetAngularVelocity( rotation.ToAngularVelocity() / MS2SEC( gameLocal.time - gameLocal.previousTime ), id );
  116. velocity = physics->GetLinearVelocity( id ) * damping + dir1 * ( ( l1 - l2 ) * ( 1.0f - damping ) / MS2SEC( gameLocal.time - gameLocal.previousTime ) );
  117. physics->SetLinearVelocity( velocity, id );
  118. }
  119. /*
  120. ================
  121. idForce_Drag::RemovePhysics
  122. ================
  123. */
  124. void idForce_Drag::RemovePhysics( const idPhysics *phys ) {
  125. if ( physics == phys ) {
  126. physics = NULL;
  127. }
  128. }