b2Fixture.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. b2Fixture::b2Fixture()
  29. {
  30. m_userData = NULL;
  31. m_body = NULL;
  32. m_next = NULL;
  33. m_proxies = NULL;
  34. m_proxyCount = 0;
  35. m_shape = NULL;
  36. m_density = 0.0f;
  37. }
  38. void b2Fixture::Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def)
  39. {
  40. m_userData = def->userData;
  41. m_friction = def->friction;
  42. m_restitution = def->restitution;
  43. m_body = body;
  44. m_next = NULL;
  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 = NULL;
  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 = NULL;
  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 = NULL;
  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 = transform2.p - transform1.p;
  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 == NULL)
  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 == NULL)
  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. b2Log(" b2FixtureDef fd;\n");
  194. b2Log(" fd.friction = %.15lef;\n", m_friction);
  195. b2Log(" fd.restitution = %.15lef;\n", m_restitution);
  196. b2Log(" fd.density = %.15lef;\n", m_density);
  197. b2Log(" fd.isSensor = bool(%d);\n", m_isSensor);
  198. b2Log(" fd.filter.categoryBits = uint16(%d);\n", m_filter.categoryBits);
  199. b2Log(" fd.filter.maskBits = uint16(%d);\n", m_filter.maskBits);
  200. b2Log(" fd.filter.groupIndex = int16(%d);\n", m_filter.groupIndex);
  201. switch (m_shape->m_type)
  202. {
  203. case b2Shape::e_circle:
  204. {
  205. b2CircleShape* s = (b2CircleShape*)m_shape;
  206. b2Log(" b2CircleShape shape;\n");
  207. b2Log(" shape.m_radius = %.15lef;\n", s->m_radius);
  208. b2Log(" shape.m_p.Set(%.15lef, %.15lef);\n", s->m_p.x, s->m_p.y);
  209. }
  210. break;
  211. case b2Shape::e_edge:
  212. {
  213. b2EdgeShape* s = (b2EdgeShape*)m_shape;
  214. b2Log(" b2EdgeShape shape;\n");
  215. b2Log(" shape.m_radius = %.15lef;\n", s->m_radius);
  216. b2Log(" shape.m_vertex0.Set(%.15lef, %.15lef);\n", s->m_vertex0.x, s->m_vertex0.y);
  217. b2Log(" shape.m_vertex1.Set(%.15lef, %.15lef);\n", s->m_vertex1.x, s->m_vertex1.y);
  218. b2Log(" shape.m_vertex2.Set(%.15lef, %.15lef);\n", s->m_vertex2.x, s->m_vertex2.y);
  219. b2Log(" shape.m_vertex3.Set(%.15lef, %.15lef);\n", s->m_vertex3.x, s->m_vertex3.y);
  220. b2Log(" shape.m_hasVertex0 = bool(%d);\n", s->m_hasVertex0);
  221. b2Log(" shape.m_hasVertex3 = bool(%d);\n", s->m_hasVertex3);
  222. }
  223. break;
  224. case b2Shape::e_polygon:
  225. {
  226. b2PolygonShape* s = (b2PolygonShape*)m_shape;
  227. b2Log(" b2PolygonShape shape;\n");
  228. b2Log(" b2Vec2 vs[%d];\n", b2_maxPolygonVertices);
  229. for (int32 i = 0; i < s->m_count; ++i)
  230. {
  231. b2Log(" vs[%d].Set(%.15lef, %.15lef);\n", i, s->m_vertices[i].x, s->m_vertices[i].y);
  232. }
  233. b2Log(" shape.Set(vs, %d);\n", s->m_count);
  234. }
  235. break;
  236. case b2Shape::e_chain:
  237. {
  238. b2ChainShape* s = (b2ChainShape*)m_shape;
  239. b2Log(" b2ChainShape shape;\n");
  240. b2Log(" b2Vec2 vs[%d];\n", s->m_count);
  241. for (int32 i = 0; i < s->m_count; ++i)
  242. {
  243. b2Log(" vs[%d].Set(%.15lef, %.15lef);\n", i, s->m_vertices[i].x, s->m_vertices[i].y);
  244. }
  245. b2Log(" shape.CreateChain(vs, %d);\n", s->m_count);
  246. b2Log(" shape.m_prevVertex.Set(%.15lef, %.15lef);\n", s->m_prevVertex.x, s->m_prevVertex.y);
  247. b2Log(" shape.m_nextVertex.Set(%.15lef, %.15lef);\n", s->m_nextVertex.x, s->m_nextVertex.y);
  248. b2Log(" shape.m_hasPrevVertex = bool(%d);\n", s->m_hasPrevVertex);
  249. b2Log(" shape.m_hasNextVertex = bool(%d);\n", s->m_hasNextVertex);
  250. }
  251. break;
  252. default:
  253. return;
  254. }
  255. b2Log("\n");
  256. b2Log(" fd.shape = &shape;\n");
  257. b2Log("\n");
  258. b2Log(" bodies[%d]->CreateFixture(&fd);\n", bodyIndex);
  259. }