physicsUserData.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #ifndef _PHYSICS_PHYSICSUSERDATA_H_
  23. #define _PHYSICS_PHYSICSUSERDATA_H_
  24. #ifndef _SIGNAL_H_
  25. #include "core/util/tSignal.h"
  26. #endif
  27. class PhysicsUserData;
  28. class SceneObject;
  29. class Point3F;
  30. class PhysicsBody;
  31. /// Signal used for contact reports.
  32. ///
  33. /// @param us The physics user data for the signaling object.
  34. /// @param them The other physics user data involved in the contact.
  35. /// @param hitPoint The approximate position of the impact.
  36. /// @param hitForce
  37. ///
  38. /// @see PhysicsUserData
  39. ///
  40. typedef Signal<void( PhysicsUserData *us,
  41. PhysicsUserData *them,
  42. const Point3F &hitPoint,
  43. const Point3F &hitForce )> PhysicsContactSignal;
  44. /// The base class for physics user data.
  45. class PhysicsUserData
  46. {
  47. public:
  48. /// The constructor.
  49. PhysicsUserData()
  50. : mObject( NULL ),
  51. mBody( NULL )
  52. #ifdef TORQUE_DEBUG
  53. , mTypeId( smTypeName )
  54. #endif
  55. {}
  56. /// The destructor.
  57. virtual ~PhysicsUserData() {}
  58. ///
  59. void setObject( SceneObject *object ) { mObject = object; }
  60. SceneObject* getObject() const { return mObject; }
  61. void setBody( PhysicsBody *body ) { mBody = body; }
  62. PhysicsBody* getBody() const { return mBody; }
  63. /// Helper method for casting a void pointer to a userdata pointer.
  64. static inline SceneObject* getObject( void *data )
  65. {
  66. PhysicsUserData *result = cast( data );
  67. return result ? result->getObject() : NULL;
  68. }
  69. PhysicsContactSignal& getContactSignal() { return mContactSignal; }
  70. /// Helper method for casting a void pointer to a userdata pointer.
  71. static inline PhysicsUserData* cast( void *data )
  72. {
  73. PhysicsUserData *result = (PhysicsUserData*)data;
  74. // If the typeid doesn't equal the value we assigned to it at
  75. // construction then this isn't a PhysicsUserData object.
  76. #ifdef TORQUE_DEBUG
  77. AssertFatal( !result || result->mTypeId == smTypeName,
  78. "PhysicsUserData::cast - The pointer is the wrong type!" );
  79. #endif
  80. return result;
  81. }
  82. protected:
  83. #ifdef TORQUE_DEBUG
  84. /// The type string used to validate the void* cast.
  85. /// @see cast
  86. static const char *smTypeName;
  87. /// The type string assigned at construction used to
  88. /// validate the void* cast.
  89. /// @see cast
  90. const char *mTypeId;
  91. #endif
  92. PhysicsContactSignal mContactSignal;
  93. SceneObject *mObject;
  94. PhysicsBody *mBody;
  95. };
  96. #endif // _PHYSICS_PHYSICSUSERDATA_H_