b2Fixture.h 9.4 KB

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