b2Shape.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef B2_SHAPE_H
  19. #define B2_SHAPE_H
  20. #include "../../Common/b2Math.h"
  21. #include "../b2Collision.h"
  22. class b2BlockAllocator;
  23. class b2Body;
  24. class b2BroadPhase;
  25. /// This holds the mass data computed for a shape.
  26. struct b2MassData
  27. {
  28. /// The mass of the shape, usually in kilograms.
  29. float32 mass;
  30. /// The position of the shape's centroid relative to the shape's origin.
  31. b2Vec2 center;
  32. /// The rotational inertia of the shape.
  33. float32 I;
  34. };
  35. /// This holds contact filtering data.
  36. struct b2FilterData
  37. {
  38. /// The collision category bits. Normally you would just set one bit.
  39. uint16 categoryBits;
  40. /// The collision mask bits. This states the categories that this
  41. /// shape would accept for collision.
  42. uint16 maskBits;
  43. /// Collision groups allow a certain group of objects to never collide (negative)
  44. /// or always collide (positive). Zero means no collision group. Non-zero group
  45. /// filtering always wins against the mask bits.
  46. int16 groupIndex;
  47. };
  48. /// The various collision shape types supported by Box2D.
  49. enum b2ShapeType
  50. {
  51. e_unknownShape = -1,
  52. e_circleShape,
  53. e_polygonShape,
  54. e_edgeShape,
  55. e_shapeTypeCount,
  56. };
  57. /// Return codes from TestSegment
  58. enum b2SegmentCollide
  59. {
  60. e_startsInsideCollide = -1,
  61. e_missCollide = 0,
  62. e_hitCollide = 1
  63. };
  64. /// A shape definition is used to construct a shape. This class defines an
  65. /// abstract shape definition. You can reuse shape definitions safely.
  66. struct b2ShapeDef
  67. {
  68. /// The constructor sets the default shape definition values.
  69. b2ShapeDef()
  70. {
  71. type = e_unknownShape;
  72. userData = NULL;
  73. friction = 0.2f;
  74. restitution = 0.0f;
  75. density = 0.0f;
  76. filter.categoryBits = 0x0001;
  77. filter.maskBits = 0xFFFF;
  78. filter.groupIndex = 0;
  79. isSensor = false;
  80. }
  81. virtual ~b2ShapeDef() {}
  82. /// Holds the shape type for down-casting.
  83. b2ShapeType type;
  84. /// Use this to store application specify shape data.
  85. void* userData;
  86. /// The shape's friction coefficient, usually in the range [0,1].
  87. float32 friction;
  88. /// The shape's restitution (elasticity) usually in the range [0,1].
  89. float32 restitution;
  90. /// The shape's density, usually in kg/m^2.
  91. float32 density;
  92. /// A sensor shape collects contact information but never generates a collision
  93. /// response.
  94. bool isSensor;
  95. /// Contact filtering data.
  96. b2FilterData filter;
  97. };
  98. /// A shape is used for collision detection. Shapes are created in b2World.
  99. /// You can use shape for collision detection before they are attached to the world.
  100. /// @warning you cannot reuse shapes.
  101. class b2Shape
  102. {
  103. public:
  104. /// Get the type of this shape. You can use this to down cast to the concrete shape.
  105. /// @return the shape type.
  106. b2ShapeType GetType() const;
  107. /// Is this shape a sensor (non-solid)?
  108. /// @return the true if the shape is a sensor.
  109. bool IsSensor() const;
  110. /// Set the contact filtering data. You must call b2World::Refilter to correct
  111. /// existing contacts/non-contacts.
  112. void SetFilterData(const b2FilterData& filter);
  113. /// Get the contact filtering data.
  114. const b2FilterData& GetFilterData() const;
  115. /// Get the parent body of this shape. This is NULL if the shape is not attached.
  116. /// @return the parent body.
  117. b2Body* GetBody();
  118. /// Get the next shape in the parent body's shape list.
  119. /// @return the next shape.
  120. b2Shape* GetNext();
  121. /// Get the user data that was assigned in the shape definition. Use this to
  122. /// store your application specific data.
  123. void* GetUserData();
  124. /// Set the user data. Use this to store your application specific data.
  125. void SetUserData(void* data);
  126. /// Test a point for containment in this shape. This only works for convex shapes.
  127. /// @param xf the shape world transform.
  128. /// @param p a point in world coordinates.
  129. virtual bool TestPoint(const b2XForm& xf, const b2Vec2& p) const = 0;
  130. /// Perform a ray cast against this shape.
  131. /// @param xf the shape world transform.
  132. /// @param lambda returns the hit fraction. You can use this to compute the contact point
  133. /// p = (1 - lambda) * segment.p1 + lambda * segment.p2.
  134. /// @param normal returns the normal at the contact point. If there is no intersection, the normal
  135. /// is not set.
  136. /// @param segment defines the begin and end point of the ray cast.
  137. /// @param maxLambda a number typically in the range [0,1].
  138. virtual b2SegmentCollide TestSegment( const b2XForm& xf,
  139. float32* lambda,
  140. b2Vec2* normal,
  141. const b2Segment& segment,
  142. float32 maxLambda) const = 0;
  143. /// Given a transform, compute the associated axis aligned bounding box for this shape.
  144. /// @param aabb returns the axis aligned box.
  145. /// @param xf the world transform of the shape.
  146. virtual void ComputeAABB(b2AABB* aabb, const b2XForm& xf) const = 0;
  147. /// Given two transforms, compute the associated swept axis aligned bounding box for this shape.
  148. /// @param aabb returns the axis aligned box.
  149. /// @param xf1 the starting shape world transform.
  150. /// @param xf2 the ending shape world transform.
  151. virtual void ComputeSweptAABB( b2AABB* aabb,
  152. const b2XForm& xf1,
  153. const b2XForm& xf2) const = 0;
  154. /// Compute the mass properties of this shape using its dimensions and density.
  155. /// The inertia tensor is computed about the local origin, not the centroid.
  156. /// @param massData returns the mass data for this shape.
  157. virtual void ComputeMass(b2MassData* massData) const = 0;
  158. /// Compute the volume and centroid of this shape intersected with a half plane
  159. /// @param normal the surface normal
  160. /// @param offset the surface offset along normal
  161. /// @param xf the shape transform
  162. /// @param c returns the centroid
  163. /// @return the total volume less than offset along normal
  164. virtual float32 ComputeSubmergedArea( const b2Vec2& normal,
  165. float32 offset,
  166. const b2XForm& xf,
  167. b2Vec2* c) const = 0;
  168. /// Get the maximum radius about the parent body's center of mass.
  169. float32 GetSweepRadius() const;
  170. /// Get the coefficient of friction.
  171. float32 GetFriction() const;
  172. /// Set the coefficient of friction.
  173. void SetFriction(float32 friction);
  174. /// Get the coefficient of restitution.
  175. float32 GetRestitution() const;
  176. /// Set the coefficient of restitution.
  177. void SetRestitution(float32 restitution);
  178. /// Get the density of the shape.
  179. float32 GetDensity() const;
  180. /// Set the density of the shape.
  181. void SetDensity(float32 density);
  182. protected:
  183. friend class b2Body;
  184. friend class b2World;
  185. static b2Shape* Create(const b2ShapeDef* def, b2BlockAllocator* allocator);
  186. static void Destroy(b2Shape* shape, b2BlockAllocator* allocator);
  187. b2Shape(const b2ShapeDef* def);
  188. virtual ~b2Shape();
  189. void CreateProxy(b2BroadPhase* broadPhase, const b2XForm& xf);
  190. void DestroyProxy(b2BroadPhase* broadPhase);
  191. bool Synchronize(b2BroadPhase* broadPhase, const b2XForm& xf1, const b2XForm& xf2);
  192. void RefilterProxy(b2BroadPhase* broadPhase, const b2XForm& xf);
  193. virtual void UpdateSweepRadius(const b2Vec2& center) = 0;
  194. b2ShapeType m_type;
  195. b2Shape* m_next;
  196. b2Body* m_body;
  197. // Sweep radius relative to the parent body's center of mass.
  198. float32 m_sweepRadius;
  199. float32 m_density;
  200. float32 m_friction;
  201. float32 m_restitution;
  202. uint16 m_proxyId;
  203. b2FilterData m_filter;
  204. bool m_isSensor;
  205. void* m_userData;
  206. };
  207. inline b2ShapeType b2Shape::GetType() const
  208. {
  209. return m_type;
  210. }
  211. inline bool b2Shape::IsSensor() const
  212. {
  213. return m_isSensor;
  214. }
  215. inline void b2Shape::SetFilterData(const b2FilterData& filter)
  216. {
  217. m_filter = filter;
  218. }
  219. inline const b2FilterData& b2Shape::GetFilterData() const
  220. {
  221. return m_filter;
  222. }
  223. inline void* b2Shape::GetUserData()
  224. {
  225. return m_userData;
  226. }
  227. inline void b2Shape::SetUserData(void* data)
  228. {
  229. m_userData = data;
  230. }
  231. inline b2Body* b2Shape::GetBody()
  232. {
  233. return m_body;
  234. }
  235. inline b2Shape* b2Shape::GetNext()
  236. {
  237. return m_next;
  238. }
  239. inline float32 b2Shape::GetSweepRadius() const
  240. {
  241. return m_sweepRadius;
  242. }
  243. inline float32 b2Shape::GetFriction() const
  244. {
  245. return m_friction;
  246. }
  247. inline void b2Shape::SetFriction(float32 friction)
  248. {
  249. m_friction = friction;
  250. }
  251. inline float32 b2Shape::GetRestitution() const
  252. {
  253. return m_restitution;
  254. }
  255. inline void b2Shape::SetRestitution(float32 restitution)
  256. {
  257. m_restitution = restitution;
  258. }
  259. inline float32 b2Shape::GetDensity() const
  260. {
  261. return m_density;
  262. }
  263. inline void b2Shape::SetDensity(float32 density)
  264. {
  265. m_density = density;
  266. }
  267. #endif