physicsForce.cpp 6.5 KB

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