physicsForce.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "T3D/physics/physicsForce.h"
  24. #include "T3D/physics/physicsPlugin.h"
  25. #include "T3D/physics/physicsWorld.h"
  26. #include "T3D/physics/physicsBody.h"
  27. #include "console/engineAPI.h"
  28. IMPLEMENT_CONOBJECT(PhysicsForce);
  29. ConsoleDocClass( PhysicsForce,
  30. "@brief Helper object for gameplay physical forces.\n\n"
  31. "%PhysicsForces can be created and \"attached\" to other @link PhysicsBody PhysicsBodies@endlink "
  32. "to attract them to the position of the PhysicsForce."
  33. "@ingroup Physics\n"
  34. );
  35. PhysicsForce::PhysicsForce()
  36. :
  37. mWorld( NULL ),
  38. mForce(0.0f),
  39. mPhysicsTick( false ),
  40. mBody( NULL )
  41. {
  42. }
  43. PhysicsForce::~PhysicsForce()
  44. {
  45. }
  46. void PhysicsForce::initPersistFields()
  47. {
  48. Parent::initPersistFields();
  49. }
  50. bool PhysicsForce::onAdd()
  51. {
  52. if ( !Parent::onAdd() )
  53. return false;
  54. // Find the physics world we're in.
  55. if ( PHYSICSMGR )
  56. mWorld = PHYSICSMGR->getWorld( isServerObject() ? "server" : "client" );
  57. setProcessTick( true );
  58. getProcessList()->preTickSignal().notify( this, &PhysicsForce::_preTick );
  59. return true;
  60. }
  61. void PhysicsForce::onRemove()
  62. {
  63. mWorld = NULL;
  64. mBody = NULL;
  65. getProcessList()->preTickSignal().remove( this, &PhysicsForce::_preTick );
  66. setProcessTick( false );
  67. Parent::onRemove();
  68. }
  69. void PhysicsForce::attach( const Point3F &start, const Point3F &direction, F32 maxDist )
  70. {
  71. detach();
  72. // If there is no physics world then we cannot apply any forces.
  73. if ( !mWorld )
  74. return;
  75. PhysicsBody *body = mWorld->castRay( start, start + ( direction * maxDist ), PhysicsWorld::BT_Dynamic );
  76. if ( !body )
  77. return;
  78. mBody = body;
  79. }
  80. void PhysicsForce::detach( const Point3F &force )
  81. {
  82. if ( mBody && !force.isZero() )
  83. {
  84. Point3F cMass = mBody->getCMassPosition();
  85. F32 mass = mBody->getMass();
  86. Point3F impulse = ( mass * force ) / TickSec;
  87. mBody->applyImpulse( cMass, impulse );
  88. }
  89. mBody = NULL;
  90. }
  91. void PhysicsForce::onMount( SceneObject *obj, S32 node )
  92. {
  93. Parent::onMount( obj, node );
  94. processAfter( obj );
  95. MatrixF mat( true );
  96. mMount.object->getMountTransform( mMount.node, mMount.xfm, &mat );
  97. setTransform( mat );
  98. }
  99. void PhysicsForce::onUnmount( SceneObject *obj, S32 node )
  100. {
  101. clearProcessAfter();
  102. Parent::onUnmount( obj, node );
  103. }
  104. void PhysicsForce::_preTick()
  105. {
  106. // How Torque works we sometimes get double or more
  107. // ticks within a single simulation step. This occurs
  108. // for various reasons including odd timesteps and low
  109. // framerate.
  110. //
  111. // In order to keep performance from plumeting we do
  112. // not tick physics multiple times... instead it just
  113. // looses time.
  114. //
  115. // We set this variable below to let processTick know
  116. // that physics hasn't stepped yet and to skip processing.
  117. //
  118. // This doesn't completely solve the issue, but it does
  119. // make things much better.
  120. //
  121. mPhysicsTick = true;
  122. }
  123. void PhysicsForce::processTick( const Move * )
  124. {
  125. if ( isMounted() )
  126. {
  127. MatrixF test( true );
  128. test.setPosition( Point3F( 0, 4, 0 ) );
  129. AssertFatal( test != mMount.xfm, "Error!" );
  130. MatrixF mat( true );
  131. mMount.object->getMountTransform( mMount.node, mMount.xfm, &mat );
  132. setTransform( mat );
  133. }
  134. // Nothing to do without a body or if physics hasn't ticked.
  135. if ( !mBody || !mPhysicsTick )
  136. return;
  137. mPhysicsTick = false;
  138. // If we lost the body then release it.
  139. if ( !mBody->isDynamic() || !mBody->isSimulationEnabled() )
  140. {
  141. detach();
  142. return;
  143. }
  144. // Get our distance to the body.
  145. Point3F cMass = mBody->getCMassPosition();
  146. Point3F vector = getPosition() - cMass;
  147. // Apply the force!
  148. F32 mass = mBody->getMass();
  149. Point3F impulse = ( mass * vector ) / TickSec;
  150. // Counter balance the linear impulse.
  151. Point3F linVel = mBody->getLinVelocity();
  152. Point3F currentForce = linVel * mass;
  153. // Apply it.
  154. mBody->applyImpulse( cMass, impulse - currentForce );
  155. }
  156. DefineEngineMethod( PhysicsForce, attach, void, ( Point3F start, Point3F direction, F32 maxDist ),,
  157. "@brief Attempts to associate the PhysicsForce with a PhysicsBody.\n\n"
  158. "Performs a physics ray cast of the provided length and direction. The %PhysicsForce "
  159. "will attach itself to the first dynamic PhysicsBody the ray collides with. "
  160. "On every tick, the attached body will be attracted towards the position of the %PhysicsForce.\n\n"
  161. "A %PhysicsForce can only be attached to one body at a time.\n\n"
  162. "@note To determine if an %attach was successful, check isAttached() immediately after "
  163. "calling this function.n\n")
  164. {
  165. object->attach( start, direction, maxDist );
  166. }
  167. DefineEngineMethod( PhysicsForce, detach, void, ( Point3F force ), ( Point3F::Zero ),
  168. "@brief Disassociates the PhysicsForce from any attached PhysicsBody.\n\n"
  169. "@param force Optional force to apply to the attached PhysicsBody "
  170. "before detaching.\n\n"
  171. "@note Has no effect if the %PhysicsForce is not attached to anything.\n\n")
  172. {
  173. object->detach( force );
  174. }
  175. DefineEngineMethod( PhysicsForce, isAttached, bool, (),,
  176. "@brief Returns true if the %PhysicsForce is currently attached to an object.\n\n"
  177. "@see PhysicsForce::attach()")
  178. {
  179. return object->isAttached();
  180. }