PointForceController.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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::integrate( Scene* pScene, const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats )
  71. {
  72. // Finish if the attractor would have no effect.
  73. if ( mIsZero( mForce ) || mIsZero( mRadius ) )
  74. return;
  75. // Prepare query filter.
  76. WorldQuery* pWorldQuery = prepareQueryFilter( pScene );
  77. // Calculate the AABB of the attractor.
  78. b2AABB aabb;
  79. aabb.lowerBound.Set( mPosition.x - mRadius, mPosition.y - mRadius );
  80. aabb.upperBound.Set( mPosition.x + mRadius, mPosition.y + mRadius );
  81. // Query for candidate objects.
  82. pWorldQuery->anyQueryArea( aabb );
  83. // Fetch results.
  84. typeWorldQueryResultVector& queryResults = pWorldQuery->getQueryResults();
  85. // Fetch result count.
  86. const U32 resultCount = (U32)queryResults.size();
  87. // Finish if nothing to process.
  88. if ( resultCount == 0 )
  89. return;
  90. // Calculate the radius squared.
  91. const F32 radiusSqr = mRadius * mRadius;
  92. // Calculate the force squared in-case we need it.
  93. const F32 forceSqr = mForce * mForce * (( mForce < 0.0f ) ? -1.0f : 1.0f);
  94. // Calculate drag coefficients (time-integrated).
  95. const F32 linearDrag = mClampF( mLinearDrag, 0.0f, 1.0f ) * elapsedTime;
  96. const F32 angularDrag = mClampF( mAngularDrag, 0.0f, 1.0f ) * elapsedTime;
  97. // Iterate the results.
  98. for ( U32 n = 0; n < resultCount; n++ )
  99. {
  100. // Fetch the scene object.
  101. SceneObject* pSceneObject = queryResults[n].mpSceneObject;
  102. // Ignore if it's a static body.
  103. if ( pSceneObject->getBodyType() == b2_staticBody )
  104. continue;
  105. // Calculate the force distance to the controllers position.
  106. Vector2 distanceForce = mPosition - pSceneObject->getPosition();
  107. // Skip if the position is outside the radius.
  108. if ( distanceForce.LengthSquared() > radiusSqr )
  109. continue;
  110. // Non-Linear force?
  111. if ( mNonLinear )
  112. {
  113. // Yes, so use an approximation of the inverse-square law.
  114. distanceForce *= (1.0f / distanceForce.LengthSquared()) * forceSqr;
  115. }
  116. else
  117. {
  118. // No, so normalize to the specified force (linear).
  119. distanceForce.Normalize( mForce );
  120. }
  121. // Apply the force.
  122. pSceneObject->applyForce( distanceForce, true );
  123. // Linear drag?
  124. if ( linearDrag > 0.0f )
  125. {
  126. // Yes, so fetch linear velocity.
  127. Vector2 linearVelocity = pSceneObject->getLinearVelocity();
  128. // Calculate linear velocity change.
  129. const Vector2 linearVelocityDelta = linearVelocity * linearDrag;
  130. // Set linear velocity.
  131. pSceneObject->setLinearVelocity( linearVelocity - linearVelocityDelta );
  132. }
  133. // Angular drag?
  134. if ( angularDrag > 0.0f )
  135. {
  136. // Yes, so fetch angular velocity.
  137. F32 angularVelocity = pSceneObject->getAngularVelocity();
  138. // Calculate angular velocity change.
  139. const F32 angularVelocityDelta = angularVelocity * angularDrag;
  140. // Set angular velocity.
  141. pSceneObject->setAngularVelocity( angularVelocity - angularVelocityDelta );
  142. }
  143. }
  144. }
  145. //------------------------------------------------------------------------------
  146. void PointForceController::renderOverlay( Scene* pScene, const SceneRenderState* pSceneRenderState, BatchRender* pBatchRenderer )
  147. {
  148. // Call parent.
  149. Parent::renderOverlay( pScene, pSceneRenderState, pBatchRenderer );
  150. // Draw force radius.
  151. pScene->mDebugDraw.DrawCircle( mPosition, mRadius, ColorF(1.0f, 1.0f, 0.0f ) );
  152. }