PointForceController.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef _ATTRACTOR_CONTROLLER_H_
  23. #include "2d/controllers/PointForceController.h"
  24. #endif
  25. // Script bindings.
  26. #include "PointForceController_ScriptBinding.h"
  27. //------------------------------------------------------------------------------
  28. IMPLEMENT_CONOBJECT(PointForceController);
  29. //------------------------------------------------------------------------------
  30. PointForceController::PointForceController()
  31. {
  32. // Reset he controller.
  33. mPosition.SetZero();
  34. mRadius = 1.0f;
  35. mForce = 0.0f;
  36. mNonLinear = true;
  37. mLinearDrag = 0.0f;
  38. mAngularDrag = 0.0f;
  39. }
  40. //------------------------------------------------------------------------------
  41. PointForceController::~PointForceController()
  42. {
  43. }
  44. //------------------------------------------------------------------------------
  45. void PointForceController::initPersistFields()
  46. {
  47. // Call parent.
  48. Parent::initPersistFields();
  49. // Force.
  50. addField( "Position", TypeVector2, Offset( mPosition, PointForceController), "The position of the attractor controller.");
  51. addField( "Radius", TypeF32, Offset( mRadius, PointForceController), "The radius of the attractor circle centered on the attractors position.");
  52. addField( "Force", TypeF32, Offset( mForce, PointForceController), "The force to apply to attact to the controller position.");
  53. addField( "NonLinear", TypeBool, Offset( mNonLinear, PointForceController), "Whether to apply the force non-linearly (using the inverse square law) or linearly.");
  54. addField( "LinearDrag", TypeF32, Offset(mLinearDrag, PointForceController), "The linear drag co-efficient for the fluid." );
  55. addField( "AngularDrag", TypeF32, Offset(mAngularDrag, PointForceController), "The angular drag co-efficient for the fluid." );
  56. }
  57. //------------------------------------------------------------------------------
  58. void PointForceController::copyTo(SimObject* object)
  59. {
  60. // Call to parent.
  61. Parent::copyTo(object);
  62. // Cast to controller.
  63. PointForceController* pController = static_cast<PointForceController*>(object);
  64. // Sanity!
  65. AssertFatal(pController != NULL, "PointForceController::copyTo() - Object is not the correct type.");
  66. // Copy state.
  67. pController->setForce( getForce() );
  68. }
  69. //------------------------------------------------------------------------------
  70. void PointForceController::setTrackedObject( SceneObject* pSceneObject )
  71. {
  72. // Set tracked object.
  73. mTrackedObject = pSceneObject;
  74. }
  75. //------------------------------------------------------------------------------
  76. void PointForceController::integrate( Scene* pScene, const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats )
  77. {
  78. // Finish if the attractor would have no effect.
  79. if ( mIsZero( mForce ) || mIsZero( mRadius ) )
  80. return;
  81. // Prepare query filter.
  82. WorldQuery* pWorldQuery = prepareQueryFilter( pScene );
  83. // Fetch the current position.
  84. const Vector2 currentPosition = getCurrentPosition();
  85. // Calculate the AABB of the attractor.
  86. b2AABB aabb;
  87. aabb.lowerBound.Set( currentPosition.x - mRadius, currentPosition.y - mRadius );
  88. aabb.upperBound.Set( currentPosition.x + mRadius, currentPosition.y + mRadius );
  89. // Query for candidate objects.
  90. pWorldQuery->anyQueryArea( aabb );
  91. // Fetch results.
  92. typeWorldQueryResultVector& queryResults = pWorldQuery->getQueryResults();
  93. // Fetch result count.
  94. const U32 resultCount = (U32)queryResults.size();
  95. // Finish if nothing to process.
  96. if ( resultCount == 0 )
  97. return;
  98. // Calculate the radius squared.
  99. const F32 radiusSqr = mRadius * mRadius;
  100. // Calculate the force squared in-case we need it.
  101. const F32 forceSqr = mForce * mForce * (( mForce < 0.0f ) ? -1.0f : 1.0f);
  102. // Calculate drag coefficients (time-integrated).
  103. const F32 linearDrag = mClampF( mLinearDrag, 0.0f, 1.0f ) * elapsedTime;
  104. const F32 angularDrag = mClampF( mAngularDrag, 0.0f, 1.0f ) * elapsedTime;
  105. // Fetch the tracked object.
  106. const SceneObject* pTrackedObject = mTrackedObject;
  107. // Iterate the results.
  108. for ( U32 n = 0; n < resultCount; n++ )
  109. {
  110. // Fetch the scene object.
  111. SceneObject* pSceneObject = queryResults[n].mpSceneObject;
  112. // Ignore if it's the tracked object.
  113. if ( pSceneObject == pTrackedObject )
  114. continue;
  115. // Ignore if it's a static body.
  116. if ( pSceneObject->getBodyType() == b2_staticBody )
  117. continue;
  118. // Calculate the force distance to the controllers current position.
  119. Vector2 distanceForce = currentPosition - pSceneObject->getPosition();
  120. // Fetch distance squared.
  121. const F32 distanceSqr = distanceForce.LengthSquared();
  122. // Skip if the position is outside the radius or is centered on the controller.
  123. if ( distanceSqr > radiusSqr || distanceSqr < FLT_EPSILON )
  124. continue;
  125. // Non-Linear force?
  126. if ( mNonLinear )
  127. {
  128. // Yes, so use an approximation of the inverse-square law.
  129. distanceForce *= (1.0f / distanceSqr) * forceSqr;
  130. }
  131. else
  132. {
  133. // No, so normalize to the specified force (linear).
  134. distanceForce.Normalize( mForce );
  135. }
  136. // Apply the force.
  137. pSceneObject->applyForce( distanceForce, true );
  138. // Linear drag?
  139. if ( linearDrag > 0.0f )
  140. {
  141. // Yes, so fetch linear velocity.
  142. Vector2 linearVelocity = pSceneObject->getLinearVelocity();
  143. // Calculate linear velocity change.
  144. const Vector2 linearVelocityDelta = linearVelocity * linearDrag;
  145. // Set linear velocity.
  146. pSceneObject->setLinearVelocity( linearVelocity - linearVelocityDelta );
  147. }
  148. // Angular drag?
  149. if ( angularDrag > 0.0f )
  150. {
  151. // Yes, so fetch angular velocity.
  152. F32 angularVelocity = pSceneObject->getAngularVelocity();
  153. // Calculate angular velocity change.
  154. const F32 angularVelocityDelta = angularVelocity * angularDrag;
  155. // Set angular velocity.
  156. pSceneObject->setAngularVelocity( angularVelocity - angularVelocityDelta );
  157. }
  158. }
  159. }
  160. //------------------------------------------------------------------------------
  161. void PointForceController::renderOverlay( Scene* pScene, const SceneRenderState* pSceneRenderState, BatchRender* pBatchRenderer )
  162. {
  163. // Call parent.
  164. Parent::renderOverlay( pScene, pSceneRenderState, pBatchRenderer );
  165. // Draw force radius.
  166. pScene->mDebugDraw.DrawCircle( getCurrentPosition(), mRadius, ColorF(1.0f, 1.0f, 0.0f ) );
  167. }