physicsForce.cpp 6.4 KB

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