b2_fixture.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include "box2d/b2_fixture.h"
  19. #include "box2d/b2_block_allocator.h"
  20. #include "box2d/b2_broad_phase.h"
  21. #include "box2d/b2_chain_shape.h"
  22. #include "box2d/b2_circle_shape.h"
  23. #include "box2d/b2_collision.h"
  24. #include "box2d/b2_contact.h"
  25. #include "box2d/b2_edge_shape.h"
  26. #include "box2d/b2_polygon_shape.h"
  27. #include "box2d/b2_world.h"
  28. b2Fixture::b2Fixture()
  29. {
  30. m_body = nullptr;
  31. m_next = nullptr;
  32. m_proxies = nullptr;
  33. m_proxyCount = 0;
  34. m_shape = nullptr;
  35. m_density = 0.0f;
  36. }
  37. void b2Fixture::Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def)
  38. {
  39. m_userData = def->userData;
  40. m_friction = def->friction;
  41. m_restitution = def->restitution;
  42. m_restitutionThreshold = def->restitutionThreshold;
  43. m_body = body;
  44. m_next = nullptr;
  45. m_filter = def->filter;
  46. m_isSensor = def->isSensor;
  47. m_shape = def->shape->Clone(allocator);
  48. // Reserve proxy space
  49. int32 childCount = m_shape->GetChildCount();
  50. m_proxies = (b2FixtureProxy*)allocator->Allocate(childCount * sizeof(b2FixtureProxy));
  51. for (int32 i = 0; i < childCount; ++i)
  52. {
  53. m_proxies[i].fixture = nullptr;
  54. m_proxies[i].proxyId = b2BroadPhase::e_nullProxy;
  55. }
  56. m_proxyCount = 0;
  57. m_density = def->density;
  58. }
  59. void b2Fixture::Destroy(b2BlockAllocator* allocator)
  60. {
  61. // The proxies must be destroyed before calling this.
  62. b2Assert(m_proxyCount == 0);
  63. // Free the proxy array.
  64. int32 childCount = m_shape->GetChildCount();
  65. allocator->Free(m_proxies, childCount * sizeof(b2FixtureProxy));
  66. m_proxies = nullptr;
  67. // Free the child shape.
  68. switch (m_shape->m_type)
  69. {
  70. case b2Shape::e_circle:
  71. {
  72. b2CircleShape* s = (b2CircleShape*)m_shape;
  73. s->~b2CircleShape();
  74. allocator->Free(s, sizeof(b2CircleShape));
  75. }
  76. break;
  77. case b2Shape::e_edge:
  78. {
  79. b2EdgeShape* s = (b2EdgeShape*)m_shape;
  80. s->~b2EdgeShape();
  81. allocator->Free(s, sizeof(b2EdgeShape));
  82. }
  83. break;
  84. case b2Shape::e_polygon:
  85. {
  86. b2PolygonShape* s = (b2PolygonShape*)m_shape;
  87. s->~b2PolygonShape();
  88. allocator->Free(s, sizeof(b2PolygonShape));
  89. }
  90. break;
  91. case b2Shape::e_chain:
  92. {
  93. b2ChainShape* s = (b2ChainShape*)m_shape;
  94. s->~b2ChainShape();
  95. allocator->Free(s, sizeof(b2ChainShape));
  96. }
  97. break;
  98. default:
  99. b2Assert(false);
  100. break;
  101. }
  102. m_shape = nullptr;
  103. }
  104. void b2Fixture::CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf)
  105. {
  106. b2Assert(m_proxyCount == 0);
  107. // Create proxies in the broad-phase.
  108. m_proxyCount = m_shape->GetChildCount();
  109. for (int32 i = 0; i < m_proxyCount; ++i)
  110. {
  111. b2FixtureProxy* proxy = m_proxies + i;
  112. m_shape->ComputeAABB(&proxy->aabb, xf, i);
  113. proxy->proxyId = broadPhase->CreateProxy(proxy->aabb, proxy);
  114. proxy->fixture = this;
  115. proxy->childIndex = i;
  116. }
  117. }
  118. void b2Fixture::DestroyProxies(b2BroadPhase* broadPhase)
  119. {
  120. // Destroy proxies in the broad-phase.
  121. for (int32 i = 0; i < m_proxyCount; ++i)
  122. {
  123. b2FixtureProxy* proxy = m_proxies + i;
  124. broadPhase->DestroyProxy(proxy->proxyId);
  125. proxy->proxyId = b2BroadPhase::e_nullProxy;
  126. }
  127. m_proxyCount = 0;
  128. }
  129. void b2Fixture::Synchronize(b2BroadPhase* broadPhase, const b2Transform& transform1, const b2Transform& transform2)
  130. {
  131. if (m_proxyCount == 0)
  132. {
  133. return;
  134. }
  135. for (int32 i = 0; i < m_proxyCount; ++i)
  136. {
  137. b2FixtureProxy* proxy = m_proxies + i;
  138. // Compute an AABB that covers the swept shape (may miss some rotation effect).
  139. b2AABB aabb1, aabb2;
  140. m_shape->ComputeAABB(&aabb1, transform1, proxy->childIndex);
  141. m_shape->ComputeAABB(&aabb2, transform2, proxy->childIndex);
  142. proxy->aabb.Combine(aabb1, aabb2);
  143. b2Vec2 displacement = aabb2.GetCenter() - aabb1.GetCenter();
  144. broadPhase->MoveProxy(proxy->proxyId, proxy->aabb, displacement);
  145. }
  146. }
  147. void b2Fixture::SetFilterData(const b2Filter& filter)
  148. {
  149. m_filter = filter;
  150. Refilter();
  151. }
  152. void b2Fixture::Refilter()
  153. {
  154. if (m_body == nullptr)
  155. {
  156. return;
  157. }
  158. // Flag associated contacts for filtering.
  159. b2ContactEdge* edge = m_body->GetContactList();
  160. while (edge)
  161. {
  162. b2Contact* contact = edge->contact;
  163. b2Fixture* fixtureA = contact->GetFixtureA();
  164. b2Fixture* fixtureB = contact->GetFixtureB();
  165. if (fixtureA == this || fixtureB == this)
  166. {
  167. contact->FlagForFiltering();
  168. }
  169. edge = edge->next;
  170. }
  171. b2World* world = m_body->GetWorld();
  172. if (world == nullptr)
  173. {
  174. return;
  175. }
  176. // Touch each proxy so that new pairs may be created
  177. b2BroadPhase* broadPhase = &world->m_contactManager.m_broadPhase;
  178. for (int32 i = 0; i < m_proxyCount; ++i)
  179. {
  180. broadPhase->TouchProxy(m_proxies[i].proxyId);
  181. }
  182. }
  183. void b2Fixture::SetSensor(bool sensor)
  184. {
  185. if (sensor != m_isSensor)
  186. {
  187. m_body->SetAwake(true);
  188. m_isSensor = sensor;
  189. }
  190. }
  191. void b2Fixture::Dump(int32 bodyIndex)
  192. {
  193. b2Dump(" b2FixtureDef fd;\n");
  194. b2Dump(" fd.friction = %.9g;\n", m_friction);
  195. b2Dump(" fd.restitution = %.9g;\n", m_restitution);
  196. b2Dump(" fd.restitutionThreshold = %.9g;\n", m_restitutionThreshold);
  197. b2Dump(" fd.density = %.9g;\n", m_density);
  198. b2Dump(" fd.isSensor = bool(%d);\n", m_isSensor);
  199. b2Dump(" fd.filter.categoryBits = uint16(%d);\n", m_filter.categoryBits);
  200. b2Dump(" fd.filter.maskBits = uint16(%d);\n", m_filter.maskBits);
  201. b2Dump(" fd.filter.groupIndex = int16(%d);\n", m_filter.groupIndex);
  202. switch (m_shape->m_type)
  203. {
  204. case b2Shape::e_circle:
  205. {
  206. b2CircleShape* s = (b2CircleShape*)m_shape;
  207. b2Dump(" b2CircleShape shape;\n");
  208. b2Dump(" shape.m_radius = %.9g;\n", s->m_radius);
  209. b2Dump(" shape.m_p.Set(%.9g, %.9g);\n", s->m_p.x, s->m_p.y);
  210. }
  211. break;
  212. case b2Shape::e_edge:
  213. {
  214. b2EdgeShape* s = (b2EdgeShape*)m_shape;
  215. b2Dump(" b2EdgeShape shape;\n");
  216. b2Dump(" shape.m_radius = %.9g;\n", s->m_radius);
  217. b2Dump(" shape.m_vertex0.Set(%.9g, %.9g);\n", s->m_vertex0.x, s->m_vertex0.y);
  218. b2Dump(" shape.m_vertex1.Set(%.9g, %.9g);\n", s->m_vertex1.x, s->m_vertex1.y);
  219. b2Dump(" shape.m_vertex2.Set(%.9g, %.9g);\n", s->m_vertex2.x, s->m_vertex2.y);
  220. b2Dump(" shape.m_vertex3.Set(%.9g, %.9g);\n", s->m_vertex3.x, s->m_vertex3.y);
  221. b2Dump(" shape.m_oneSided = bool(%d);\n", s->m_oneSided);
  222. }
  223. break;
  224. case b2Shape::e_polygon:
  225. {
  226. b2PolygonShape* s = (b2PolygonShape*)m_shape;
  227. b2Dump(" b2PolygonShape shape;\n");
  228. b2Dump(" b2Vec2 vs[%d];\n", b2_maxPolygonVertices);
  229. for (int32 i = 0; i < s->m_count; ++i)
  230. {
  231. b2Dump(" vs[%d].Set(%.9g, %.9g);\n", i, s->m_vertices[i].x, s->m_vertices[i].y);
  232. }
  233. b2Dump(" shape.Set(vs, %d);\n", s->m_count);
  234. }
  235. break;
  236. case b2Shape::e_chain:
  237. {
  238. b2ChainShape* s = (b2ChainShape*)m_shape;
  239. b2Dump(" b2ChainShape shape;\n");
  240. b2Dump(" b2Vec2 vs[%d];\n", s->m_count);
  241. for (int32 i = 0; i < s->m_count; ++i)
  242. {
  243. b2Dump(" vs[%d].Set(%.9g, %.9g);\n", i, s->m_vertices[i].x, s->m_vertices[i].y);
  244. }
  245. b2Dump(" shape.CreateChain(vs, %d);\n", s->m_count);
  246. b2Dump(" shape.m_prevVertex.Set(%.9g, %.9g);\n", s->m_prevVertex.x, s->m_prevVertex.y);
  247. b2Dump(" shape.m_nextVertex.Set(%.9g, %.9g);\n", s->m_nextVertex.x, s->m_nextVertex.y);
  248. }
  249. break;
  250. default:
  251. return;
  252. }
  253. b2Dump("\n");
  254. b2Dump(" fd.shape = &shape;\n");
  255. b2Dump("\n");
  256. b2Dump(" bodies[%d]->CreateFixture(&fd);\n", bodyIndex);
  257. }