b2_fixture.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_FIXTURE_H
  19. #define B2_FIXTURE_H
  20. #include "b2_api.h"
  21. #include "b2_body.h"
  22. #include "b2_collision.h"
  23. #include "b2_shape.h"
  24. class b2BlockAllocator;
  25. class b2Body;
  26. class b2BroadPhase;
  27. class b2Fixture;
  28. /// This holds contact filtering data.
  29. struct B2_API 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 B2_API b2FixtureDef
  50. {
  51. /// The constructor sets the default fixture definition values.
  52. b2FixtureDef()
  53. {
  54. shape = nullptr;
  55. friction = 0.2f;
  56. restitution = 0.0f;
  57. restitutionThreshold = 1.0f * b2_lengthUnitsPerMeter;
  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. b2FixtureUserData userData;
  66. /// The friction coefficient, usually in the range [0,1].
  67. float friction;
  68. /// The restitution (elasticity) usually in the range [0,1].
  69. float restitution;
  70. /// Restitution velocity threshold, usually in m/s. Collisions above this
  71. /// speed have restitution applied (will bounce).
  72. float restitutionThreshold;
  73. /// The density, usually in kg/m^2.
  74. float density;
  75. /// A sensor shape collects contact information but never generates a collision
  76. /// response.
  77. bool isSensor;
  78. /// Contact filtering data.
  79. b2Filter filter;
  80. };
  81. /// This proxy is used internally to connect fixtures to the broad-phase.
  82. struct B2_API b2FixtureProxy
  83. {
  84. b2AABB aabb;
  85. b2Fixture* fixture;
  86. int32 childIndex;
  87. int32 proxyId;
  88. };
  89. /// A fixture is used to attach a shape to a body for collision detection. A fixture
  90. /// inherits its transform from its parent. Fixtures hold additional non-geometric data
  91. /// such as friction, collision filters, etc.
  92. /// Fixtures are created via b2Body::CreateFixture.
  93. /// @warning you cannot reuse fixtures.
  94. class B2_API b2Fixture
  95. {
  96. public:
  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 nullptr 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. b2FixtureUserData& GetUserData();
  129. const b2FixtureUserData& GetUserData() const;
  130. /// Test a point for containment in this fixture.
  131. /// @param p a point in world coordinates.
  132. bool TestPoint(const b2Vec2& p) const;
  133. /// Cast a ray against this shape.
  134. /// @param output the ray-cast results.
  135. /// @param input the ray-cast input parameters.
  136. /// @param childIndex the child shape index (e.g. edge index)
  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(float density);
  145. /// Get the density of this fixture.
  146. float GetDensity() const;
  147. /// Get the coefficient of friction.
  148. float GetFriction() const;
  149. /// Set the coefficient of friction. This will _not_ change the friction of
  150. /// existing contacts.
  151. void SetFriction(float friction);
  152. /// Get the coefficient of restitution.
  153. float GetRestitution() const;
  154. /// Set the coefficient of restitution. This will _not_ change the restitution of
  155. /// existing contacts.
  156. void SetRestitution(float restitution);
  157. /// Get the restitution velocity threshold.
  158. float GetRestitutionThreshold() const;
  159. /// Set the restitution threshold. This will _not_ change the restitution threshold of
  160. /// existing contacts.
  161. void SetRestitutionThreshold(float threshold);
  162. /// Get the fixture's AABB. This AABB may be enlarge and/or stale.
  163. /// If you need a more accurate AABB, compute it using the shape and
  164. /// the body transform.
  165. const b2AABB& GetAABB(int32 childIndex) const;
  166. /// Dump this fixture to the log file.
  167. void Dump(int32 bodyIndex);
  168. protected:
  169. friend class b2Body;
  170. friend class b2World;
  171. friend class b2Contact;
  172. friend class b2ContactManager;
  173. b2Fixture();
  174. // We need separation create/destroy functions from the constructor/destructor because
  175. // the destructor cannot access the allocator (no destructor arguments allowed by C++).
  176. void Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def);
  177. void Destroy(b2BlockAllocator* allocator);
  178. // These support body activation/deactivation.
  179. void CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf);
  180. void DestroyProxies(b2BroadPhase* broadPhase);
  181. void Synchronize(b2BroadPhase* broadPhase, const b2Transform& xf1, const b2Transform& xf2);
  182. float m_density;
  183. b2Fixture* m_next;
  184. b2Body* m_body;
  185. b2Shape* m_shape;
  186. float m_friction;
  187. float m_restitution;
  188. float m_restitutionThreshold;
  189. b2FixtureProxy* m_proxies;
  190. int32 m_proxyCount;
  191. b2Filter m_filter;
  192. bool m_isSensor;
  193. b2FixtureUserData m_userData;
  194. };
  195. inline b2Shape::Type b2Fixture::GetType() const
  196. {
  197. return m_shape->GetType();
  198. }
  199. inline b2Shape* b2Fixture::GetShape()
  200. {
  201. return m_shape;
  202. }
  203. inline const b2Shape* b2Fixture::GetShape() const
  204. {
  205. return m_shape;
  206. }
  207. inline bool b2Fixture::IsSensor() const
  208. {
  209. return m_isSensor;
  210. }
  211. inline const b2Filter& b2Fixture::GetFilterData() const
  212. {
  213. return m_filter;
  214. }
  215. inline b2FixtureUserData& b2Fixture::GetUserData()
  216. {
  217. return m_userData;
  218. }
  219. inline const b2FixtureUserData& b2Fixture::GetUserData() const
  220. {
  221. return m_userData;
  222. }
  223. inline b2Body* b2Fixture::GetBody()
  224. {
  225. return m_body;
  226. }
  227. inline const b2Body* b2Fixture::GetBody() const
  228. {
  229. return m_body;
  230. }
  231. inline b2Fixture* b2Fixture::GetNext()
  232. {
  233. return m_next;
  234. }
  235. inline const b2Fixture* b2Fixture::GetNext() const
  236. {
  237. return m_next;
  238. }
  239. inline void b2Fixture::SetDensity(float density)
  240. {
  241. b2Assert(b2IsValid(density) && density >= 0.0f);
  242. m_density = density;
  243. }
  244. inline float b2Fixture::GetDensity() const
  245. {
  246. return m_density;
  247. }
  248. inline float b2Fixture::GetFriction() const
  249. {
  250. return m_friction;
  251. }
  252. inline void b2Fixture::SetFriction(float friction)
  253. {
  254. m_friction = friction;
  255. }
  256. inline float b2Fixture::GetRestitution() const
  257. {
  258. return m_restitution;
  259. }
  260. inline void b2Fixture::SetRestitution(float restitution)
  261. {
  262. m_restitution = restitution;
  263. }
  264. inline float b2Fixture::GetRestitutionThreshold() const
  265. {
  266. return m_restitutionThreshold;
  267. }
  268. inline void b2Fixture::SetRestitutionThreshold(float threshold)
  269. {
  270. m_restitutionThreshold = threshold;
  271. }
  272. inline bool b2Fixture::TestPoint(const b2Vec2& p) const
  273. {
  274. return m_shape->TestPoint(m_body->GetTransform(), p);
  275. }
  276. inline bool b2Fixture::RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const
  277. {
  278. return m_shape->RayCast(output, input, m_body->GetTransform(), childIndex);
  279. }
  280. inline void b2Fixture::GetMassData(b2MassData* massData) const
  281. {
  282. m_shape->ComputeMass(massData, m_density);
  283. }
  284. inline const b2AABB& b2Fixture::GetAABB(int32 childIndex) const
  285. {
  286. b2Assert(0 <= childIndex && childIndex < m_proxyCount);
  287. return m_proxies[childIndex].aabb;
  288. }
  289. #endif