b2Fixture.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  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_FIXTURE_H
  19. #define B2_FIXTURE_H
  20. #include <Box2D/Dynamics/b2Body.h>
  21. #include <Box2D/Collision/b2Collision.h>
  22. #include <Box2D/Collision/Shapes/b2Shape.h>
  23. class b2BlockAllocator;
  24. class b2Body;
  25. class b2BroadPhase;
  26. class b2Fixture;
  27. /// This holds contact filtering data.
  28. struct b2Filter
  29. {
  30. b2Filter()
  31. {
  32. categoryBits = 0x0001;
  33. maskBits = 0xFFFF;
  34. groupIndex = 0;
  35. }
  36. /// The collision category bits. Normally you would just set one bit.
  37. uint16 categoryBits;
  38. /// The collision mask bits. This states the categories that this
  39. /// shape would accept for collision.
  40. uint16 maskBits;
  41. /// Collision groups allow a certain group of objects to never collide (negative)
  42. /// or always collide (positive). Zero means no collision group. Non-zero group
  43. /// filtering always wins against the mask bits.
  44. int16 groupIndex;
  45. };
  46. /// A fixture definition is used to create a fixture. This class defines an
  47. /// abstract fixture definition. You can reuse fixture definitions safely.
  48. struct b2FixtureDef
  49. {
  50. /// The constructor sets the default fixture definition values.
  51. b2FixtureDef()
  52. {
  53. shape = NULL;
  54. userData = NULL;
  55. friction = 0.2f;
  56. restitution = 0.0f;
  57. density = 0.0f;
  58. isSensor = false;
  59. }
  60. /// The shape, this must be set. The shape will be cloned, so you
  61. /// can create the shape on the stack.
  62. const b2Shape* shape;
  63. /// Use this to store application specific fixture data.
  64. void* userData;
  65. /// The friction coefficient, usually in the range [0,1].
  66. float32 friction;
  67. /// The restitution (elasticity) usually in the range [0,1].
  68. float32 restitution;
  69. /// The density, usually in kg/m^2.
  70. float32 density;
  71. /// A sensor shape collects contact information but never generates a collision
  72. /// response.
  73. bool isSensor;
  74. /// Contact filtering data.
  75. b2Filter filter;
  76. };
  77. /// This proxy is used internally to connect fixtures to the broad-phase.
  78. struct b2FixtureProxy
  79. {
  80. b2AABB aabb;
  81. b2Fixture* fixture;
  82. int32 childIndex;
  83. int32 proxyId;
  84. };
  85. /// A fixture is used to attach a shape to a body for collision detection. A fixture
  86. /// inherits its transform from its parent. Fixtures hold additional non-geometric data
  87. /// such as friction, collision filters, etc.
  88. /// Fixtures are created via b2Body::CreateFixture.
  89. /// @warning you cannot reuse fixtures.
  90. class b2Fixture
  91. {
  92. public:
  93. /// Get the type of the child shape. You can use this to down cast to the concrete shape.
  94. /// @return the shape type.
  95. b2Shape::Type GetType() const;
  96. /// Get the child shape. You can modify the child shape, however you should not change the
  97. /// number of vertices because this will crash some collision caching mechanisms.
  98. /// Manipulating the shape may lead to non-physical behavior.
  99. b2Shape* GetShape();
  100. const b2Shape* GetShape() const;
  101. /// Set if this fixture is a sensor.
  102. void SetSensor(bool sensor);
  103. /// Is this fixture a sensor (non-solid)?
  104. /// @return the true if the shape is a sensor.
  105. bool IsSensor() const;
  106. /// Set the contact filtering data. This will not update contacts until the next time
  107. /// step when either parent body is active and awake.
  108. /// This automatically calls Refilter.
  109. void SetFilterData(const b2Filter& filter);
  110. /// Get the contact filtering data.
  111. const b2Filter& GetFilterData() const;
  112. /// Call this if you want to establish collision that was previously disabled by b2ContactFilter::ShouldCollide.
  113. void Refilter();
  114. /// Get the parent body of this fixture. This is NULL if the fixture is not attached.
  115. /// @return the parent body.
  116. b2Body* GetBody();
  117. const b2Body* GetBody() const;
  118. /// Get the next fixture in the parent body's fixture list.
  119. /// @return the next shape.
  120. b2Fixture* GetNext();
  121. const b2Fixture* GetNext() const;
  122. /// Get the user data that was assigned in the fixture definition. Use this to
  123. /// store your application specific data.
  124. void* GetUserData() const;
  125. /// Set the user data. Use this to store your application specific data.
  126. void SetUserData(void* data);
  127. /// Test a point for containment in this fixture.
  128. /// @param p a point in world coordinates.
  129. bool TestPoint(const b2Vec2& p) const;
  130. /// Cast a ray against this shape.
  131. /// @param output the ray-cast results.
  132. /// @param input the ray-cast input parameters.
  133. bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const;
  134. /// Get the mass data for this fixture. The mass data is based on the density and
  135. /// the shape. The rotational inertia is about the shape's origin. This operation
  136. /// may be expensive.
  137. void GetMassData(b2MassData* massData) const;
  138. /// Set the density of this fixture. This will _not_ automatically adjust the mass
  139. /// of the body. You must call b2Body::ResetMassData to update the body's mass.
  140. void SetDensity(float32 density);
  141. /// Get the density of this fixture.
  142. float32 GetDensity() const;
  143. /// Get the coefficient of friction.
  144. float32 GetFriction() const;
  145. /// Set the coefficient of friction. This will _not_ change the friction of
  146. /// existing contacts.
  147. void SetFriction(float32 friction);
  148. /// Get the coefficient of restitution.
  149. float32 GetRestitution() const;
  150. /// Set the coefficient of restitution. This will _not_ change the restitution of
  151. /// existing contacts.
  152. void SetRestitution(float32 restitution);
  153. /// Get the fixture's AABB. This AABB may be enlarge and/or stale.
  154. /// If you need a more accurate AABB, compute it using the shape and
  155. /// the body transform.
  156. const b2AABB& GetAABB(int32 childIndex) const;
  157. /// Dump this fixture to the log file.
  158. void Dump(int32 bodyIndex);
  159. protected:
  160. friend class b2Body;
  161. friend class b2World;
  162. friend class b2Contact;
  163. friend class b2ContactManager;
  164. b2Fixture();
  165. // We need separation create/destroy functions from the constructor/destructor because
  166. // the destructor cannot access the allocator (no destructor arguments allowed by C++).
  167. void Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def);
  168. void Destroy(b2BlockAllocator* allocator);
  169. // These support body activation/deactivation.
  170. void CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf);
  171. void DestroyProxies(b2BroadPhase* broadPhase);
  172. void Synchronize(b2BroadPhase* broadPhase, const b2Transform& xf1, const b2Transform& xf2);
  173. float32 m_density;
  174. b2Fixture* m_next;
  175. b2Body* m_body;
  176. b2Shape* m_shape;
  177. float32 m_friction;
  178. float32 m_restitution;
  179. b2FixtureProxy* m_proxies;
  180. int32 m_proxyCount;
  181. b2Filter m_filter;
  182. bool m_isSensor;
  183. void* m_userData;
  184. };
  185. inline b2Shape::Type b2Fixture::GetType() const
  186. {
  187. return m_shape->GetType();
  188. }
  189. inline b2Shape* b2Fixture::GetShape()
  190. {
  191. return m_shape;
  192. }
  193. inline const b2Shape* b2Fixture::GetShape() const
  194. {
  195. return m_shape;
  196. }
  197. inline bool b2Fixture::IsSensor() const
  198. {
  199. return m_isSensor;
  200. }
  201. inline const b2Filter& b2Fixture::GetFilterData() const
  202. {
  203. return m_filter;
  204. }
  205. inline void* b2Fixture::GetUserData() const
  206. {
  207. return m_userData;
  208. }
  209. inline void b2Fixture::SetUserData(void* data)
  210. {
  211. m_userData = data;
  212. }
  213. inline b2Body* b2Fixture::GetBody()
  214. {
  215. return m_body;
  216. }
  217. inline const b2Body* b2Fixture::GetBody() const
  218. {
  219. return m_body;
  220. }
  221. inline b2Fixture* b2Fixture::GetNext()
  222. {
  223. return m_next;
  224. }
  225. inline const b2Fixture* b2Fixture::GetNext() const
  226. {
  227. return m_next;
  228. }
  229. inline void b2Fixture::SetDensity(float32 density)
  230. {
  231. b2Assert(b2IsValid(density) && density >= 0.0f);
  232. m_density = density;
  233. }
  234. inline float32 b2Fixture::GetDensity() const
  235. {
  236. return m_density;
  237. }
  238. inline float32 b2Fixture::GetFriction() const
  239. {
  240. return m_friction;
  241. }
  242. inline void b2Fixture::SetFriction(float32 friction)
  243. {
  244. m_friction = friction;
  245. }
  246. inline float32 b2Fixture::GetRestitution() const
  247. {
  248. return m_restitution;
  249. }
  250. inline void b2Fixture::SetRestitution(float32 restitution)
  251. {
  252. m_restitution = restitution;
  253. }
  254. inline bool b2Fixture::TestPoint(const b2Vec2& p) const
  255. {
  256. return m_shape->TestPoint(m_body->GetTransform(), p);
  257. }
  258. inline bool b2Fixture::RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const
  259. {
  260. return m_shape->RayCast(output, input, m_body->GetTransform(), childIndex);
  261. }
  262. inline void b2Fixture::GetMassData(b2MassData* massData) const
  263. {
  264. m_shape->ComputeMass(massData, m_density);
  265. }
  266. inline const b2AABB& b2Fixture::GetAABB(int32 childIndex) const
  267. {
  268. b2Assert(0 <= childIndex && childIndex < m_proxyCount);
  269. return m_proxies[childIndex].aabb;
  270. }
  271. #endif