PointForceController.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #define _ATTRACTOR_CONTROLLER_H_
  24. #ifndef _PICKING_SCENE_CONTROLLER_H_
  25. #include "2d/controllers/core/pickingSceneController.h"
  26. #endif
  27. #ifndef _VECTOR2_H_
  28. #include "2d/core/vector2.h"
  29. #endif
  30. //------------------------------------------------------------------------------
  31. class PointForceController : public PickingSceneController
  32. {
  33. private:
  34. typedef PickingSceneController Parent;
  35. /// Controller position.
  36. Vector2 mPosition;
  37. /// Controller radius.
  38. F32 mRadius;
  39. /// Controller force.
  40. F32 mForce;
  41. /// Whether to apply the force non-linearly (using the inverse square law) or linearly.
  42. bool mNonLinear;
  43. /// Linear drag co-efficient.
  44. F32 mLinearDrag;
  45. /// Linear drag co-efficient.
  46. F32 mAngularDrag;
  47. /// Tracked object.
  48. SimObjectPtr<SceneObject> mTrackedObject;
  49. public:
  50. PointForceController();
  51. virtual ~PointForceController();
  52. static void initPersistFields();
  53. virtual void copyTo(SimObject* object);
  54. inline void setPosition( const Vector2& position ) { mPosition = position; }
  55. inline const Vector2& getPosition( void ) const { return mPosition; }
  56. inline void setRadius( const F32 radius ) { mRadius = getMax( radius, FLT_MIN ); }
  57. inline F32 getRadius( void ) const { return mRadius; }
  58. inline void setForce( const F32 force ) { mForce = force; }
  59. inline F32 getForce( void ) const { return mForce; }
  60. inline void setNonLinear( const bool nonLinear ) { mNonLinear = nonLinear; }
  61. inline bool getNonLinear( void ) const { return mNonLinear; }
  62. inline void setLinearDrag( const F32 linearDrag ) { mLinearDrag = linearDrag; }
  63. inline F32 getLinearDrag( void ) const { return mLinearDrag; }
  64. inline void setAngularDrag( const F32 angularDrag ) { mAngularDrag = angularDrag; }
  65. inline F32 getAngularDrag( void ) const { return mAngularDrag; }
  66. void setTrackedObject( SceneObject* pSceneObject );
  67. inline SceneObject* getTrackedObject( void ) { return mTrackedObject; }
  68. inline Vector2 getCurrentPosition( void )
  69. {
  70. // Fetch the tracked object.
  71. SceneObject* pSceneObject = mTrackedObject;
  72. // Return the controller position if no tracked object else the tracked object position plus a tracked object local-space position.
  73. return pSceneObject == NULL ? mPosition : b2Mul( pSceneObject->getTransform(), mPosition);
  74. }
  75. /// Integration.
  76. virtual void integrate( Scene* pScene, const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats );
  77. // Scene render.
  78. virtual void renderOverlay( Scene* pScene, const SceneRenderState* pSceneRenderState, BatchRender* pBatchRenderer );
  79. /// Declare Console Object.
  80. DECLARE_CONOBJECT( PointForceController );
  81. };
  82. #endif // _ATTRACTOR_CONTROLLER_H_