/Box2D/Dynamics/b2Fixture.h

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