physicsUserData.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. :
  51. #ifdef TORQUE_DEBUG
  52. mTypeId( smTypeName ),
  53. #endif
  54. mObject( NULL ),
  55. mBody( NULL )
  56. {}
  57. /// The destructor.
  58. virtual ~PhysicsUserData() {}
  59. ///
  60. void setObject( SceneObject *object ) { mObject = object; }
  61. SceneObject* getObject() const { return mObject; }
  62. void setBody( PhysicsBody *body ) { mBody = body; }
  63. PhysicsBody* getBody() const { return mBody; }
  64. /// Helper method for casting a void pointer to a userdata pointer.
  65. static inline SceneObject* getObject( void *data )
  66. {
  67. PhysicsUserData *result = cast( data );
  68. return result ? result->getObject() : NULL;
  69. }
  70. PhysicsContactSignal& getContactSignal() { return mContactSignal; }
  71. /// Helper method for casting a void pointer to a userdata pointer.
  72. static inline PhysicsUserData* cast( void *data )
  73. {
  74. PhysicsUserData *result = (PhysicsUserData*)data;
  75. // If the typeid doesn't equal the value we assigned to it at
  76. // construction then this isn't a PhysicsUserData object.
  77. #ifdef TORQUE_DEBUG
  78. AssertFatal( !result || result->mTypeId == smTypeName,
  79. "PhysicsUserData::cast - The pointer is the wrong type!" );
  80. #endif
  81. return result;
  82. }
  83. protected:
  84. #ifdef TORQUE_DEBUG
  85. /// The type string used to validate the void* cast.
  86. /// @see cast
  87. static const char *smTypeName;
  88. /// The type string assigned at construction used to
  89. /// validate the void* cast.
  90. /// @see cast
  91. const char *mTypeId;
  92. #endif
  93. PhysicsContactSignal mContactSignal;
  94. SceneObject *mObject;
  95. PhysicsBody *mBody;
  96. };
  97. #endif // _PHYSICS_PHYSICSUSERDATA_H_