physicsForce.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. mPhysicsTick( false ),
  39. mBody( NULL )
  40. {
  41. }
  42. PhysicsForce::~PhysicsForce()
  43. {
  44. }
  45. void PhysicsForce::initPersistFields()
  46. {
  47. Parent::initPersistFields();
  48. }
  49. bool PhysicsForce::onAdd()
  50. {
  51. if ( !Parent::onAdd() )
  52. return false;
  53. // Find the physics world we're in.
  54. if ( PHYSICSMGR )
  55. mWorld = PHYSICSMGR->getWorld( isServerObject() ? "server" : "client" );
  56. setProcessTick( true );
  57. getProcessList()->preTickSignal().notify( this, &PhysicsForce::_preTick );
  58. return true;
  59. }
  60. void PhysicsForce::onRemove()
  61. {
  62. mWorld = NULL;
  63. mBody = NULL;
  64. getProcessList()->preTickSignal().remove( this, &PhysicsForce::_preTick );
  65. setProcessTick( false );
  66. Parent::onRemove();
  67. }
  68. void PhysicsForce::attach( const Point3F &start, const Point3F &direction, F32 maxDist )
  69. {
  70. detach();
  71. // If there is no physics world then we cannot apply any forces.
  72. if ( !mWorld )
  73. return;
  74. PhysicsBody *body = mWorld->castRay( start, start + ( direction * maxDist ), PhysicsWorld::BT_Dynamic );
  75. if ( !body )
  76. return;
  77. mBody = body;
  78. }
  79. void PhysicsForce::detach( const Point3F &force )
  80. {
  81. if ( mBody && !force.isZero() )
  82. {
  83. Point3F cMass = mBody->getCMassPosition();
  84. F32 mass = mBody->getMass();
  85. Point3F impulse = ( mass * force ) / TickSec;
  86. mBody->applyImpulse( cMass, impulse );
  87. }
  88. mBody = NULL;
  89. }
  90. void PhysicsForce::onMount( SceneObject *obj, S32 node )
  91. {
  92. Parent::onMount( obj, node );
  93. processAfter( obj );
  94. MatrixF mat( true );
  95. mMount.object->getMountTransform( mMount.node, mMount.xfm, &mat );
  96. setTransform( mat );
  97. }
  98. void PhysicsForce::onUnmount( SceneObject *obj, S32 node )
  99. {
  100. clearProcessAfter();
  101. Parent::onUnmount( obj, node );
  102. }
  103. void PhysicsForce::_preTick()
  104. {
  105. // How Torque works we sometimes get double or more
  106. // ticks within a single simulation step. This occurs
  107. // for various reasons including odd timesteps and low
  108. // framerate.
  109. //
  110. // In order to keep performance from plumeting we do
  111. // not tick physics multiple times... instead it just
  112. // looses time.
  113. //
  114. // We set this variable below to let processTick know
  115. // that physics hasn't stepped yet and to skip processing.
  116. //
  117. // This doesn't completely solve the issue, but it does
  118. // make things much better.
  119. //
  120. mPhysicsTick = true;
  121. }
  122. void PhysicsForce::processTick( const Move * )
  123. {
  124. if ( isMounted() )
  125. {
  126. MatrixF test( true );
  127. test.setPosition( Point3F( 0, 4, 0 ) );
  128. AssertFatal( test != mMount.xfm, "Error!" );
  129. MatrixF mat( true );
  130. mMount.object->getMountTransform( mMount.node, mMount.xfm, &mat );
  131. setTransform( mat );
  132. }
  133. // Nothing to do without a body or if physics hasn't ticked.
  134. if ( !mBody || !mPhysicsTick )
  135. return;
  136. mPhysicsTick = false;
  137. // If we lost the body then release it.
  138. if ( !mBody->isDynamic() || !mBody->isSimulationEnabled() )
  139. {
  140. detach();
  141. return;
  142. }
  143. // Get our distance to the body.
  144. Point3F cMass = mBody->getCMassPosition();
  145. Point3F vector = getPosition() - cMass;
  146. // Apply the force!
  147. F32 mass = mBody->getMass();
  148. Point3F impulse = ( mass * vector ) / TickSec;
  149. // Counter balance the linear impulse.
  150. Point3F linVel = mBody->getLinVelocity();
  151. Point3F currentForce = linVel * mass;
  152. // Apply it.
  153. mBody->applyImpulse( cMass, impulse - currentForce );
  154. }
  155. DefineEngineMethod( PhysicsForce, attach, void, ( Point3F start, Point3F direction, F32 maxDist ),,
  156. "@brief Attempts to associate the PhysicsForce with a PhysicsBody.\n\n"
  157. "Performs a physics ray cast of the provided length and direction. The %PhysicsForce "
  158. "will attach itself to the first dynamic PhysicsBody the ray collides with. "
  159. "On every tick, the attached body will be attracted towards the position of the %PhysicsForce.\n\n"
  160. "A %PhysicsForce can only be attached to one body at a time.\n\n"
  161. "@note To determine if an %attach was successful, check isAttached() immediately after "
  162. "calling this function.n\n")
  163. {
  164. object->attach( start, direction, maxDist );
  165. }
  166. DefineEngineMethod( PhysicsForce, detach, void, ( Point3F force ), ( Point3F::Zero ),
  167. "@brief Disassociates the PhysicsForce from any attached PhysicsBody.\n\n"
  168. "@param force Optional force to apply to the attached PhysicsBody "
  169. "before detaching.\n\n"
  170. "@note Has no effect if the %PhysicsForce is not attached to anything.\n\n")
  171. {
  172. object->detach( force );
  173. }
  174. DefineEngineMethod( PhysicsForce, isAttached, bool, (),,
  175. "@brief Returns true if the %PhysicsForce is currently attached to an object.\n\n"
  176. "@see PhysicsForce::attach()")
  177. {
  178. return object->isAttached();
  179. }