b2Fixture.cpp 8.8 KB

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