PointForceController.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. }
  37. //------------------------------------------------------------------------------
  38. PointForceController::~PointForceController()
  39. {
  40. }
  41. //------------------------------------------------------------------------------
  42. void PointForceController::initPersistFields()
  43. {
  44. // Call parent.
  45. Parent::initPersistFields();
  46. // Force.
  47. addProtectedField("Position", TypeVector2, Offset( mPosition, PointForceController), &defaultProtectedSetFn, &defaultProtectedGetFn, "The position of the attractor controller.");
  48. addProtectedField("Radius", TypeF32, Offset( mRadius, PointForceController), &defaultProtectedSetFn, &defaultProtectedGetFn, "The radius of the attractor circle centered on the attractors position.");
  49. addProtectedField("Force", TypeF32, Offset( mForce, PointForceController), &defaultProtectedSetFn, &defaultProtectedGetFn, "The force to apply to attact to the controller position.");
  50. }
  51. //------------------------------------------------------------------------------
  52. void PointForceController::copyTo(SimObject* object)
  53. {
  54. // Call to parent.
  55. Parent::copyTo(object);
  56. // Cast to controller.
  57. PointForceController* pController = static_cast<PointForceController*>(object);
  58. // Sanity!
  59. AssertFatal(pController != NULL, "PointForceController::copyTo() - Object is not the correct type.");
  60. // Copy state.
  61. pController->setForce( getForce() );
  62. }
  63. //------------------------------------------------------------------------------
  64. void PointForceController::integrate( Scene* pScene, const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats )
  65. {
  66. // Finish if the attractor would have no effect.
  67. if ( mIsZero( mForce ) || mIsZero( mRadius ) )
  68. return;
  69. // Fetch world query and clear results.
  70. WorldQuery* pWorldQuery = pScene->getWorldQuery( true );
  71. // Set filter.
  72. WorldQueryFilter queryFilter( 0xFFFFFFFF, 0xFFFFFFFF, true, false, true, true );
  73. pWorldQuery->setQueryFilter( queryFilter );
  74. // Calculate the AABB of the attractor.
  75. b2AABB aabb;
  76. aabb.lowerBound.Set( mPosition.x - mRadius, mPosition.y - mRadius );
  77. aabb.upperBound.Set( mPosition.x + mRadius, mPosition.y + mRadius );
  78. // Query for candidate objects.
  79. pWorldQuery->anyQueryArea( aabb );
  80. // Fetch results.
  81. typeWorldQueryResultVector& queryResults = pWorldQuery->getQueryResults();
  82. // Iterate the results.
  83. for ( U32 n = 0; n < (U32)queryResults.size(); n++ )
  84. {
  85. // Fetch the scene object.
  86. SceneObject* pSceneObject = queryResults[n].mpSceneObject;
  87. // Ignore if it's a static body.
  88. if ( pSceneObject->getBodyType() == b2BodyType::b2_staticBody )
  89. continue;
  90. // Calculate the force direction to the controllers position.
  91. Vector2 forceDirection = mPosition - pSceneObject->getPosition();
  92. // Skip if the position is outside the radius.
  93. if ( forceDirection.Length() > mRadius )
  94. continue;
  95. // Normalize to the specified force.
  96. forceDirection.Normalize( mForce );
  97. // Apply the force.
  98. pSceneObject->applyForce( forceDirection, true );
  99. }
  100. }
  101. //------------------------------------------------------------------------------
  102. void PointForceController::renderOverlay( Scene* pScene, const SceneRenderState* pSceneRenderState, BatchRender* pBatchRenderer )
  103. {
  104. // Call parent.
  105. Parent::renderOverlay( pScene, pSceneRenderState, pBatchRenderer );
  106. // Draw force radius.
  107. pScene->mDebugDraw.DrawCircle( mPosition, mRadius, ColorF(1.0f, 1.0f, 0.0f ) );
  108. }