b2Body.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Copyright (c) 2006-2007 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/b2Body.h>
  19. #include <Box2D/Dynamics/b2Fixture.h>
  20. #include <Box2D/Dynamics/b2World.h>
  21. #include <Box2D/Dynamics/Contacts/b2Contact.h>
  22. #include <Box2D/Dynamics/Joints/b2Joint.h>
  23. b2Body::b2Body(const b2BodyDef* bd, b2World* world)
  24. {
  25. b2Assert(bd->position.IsValid());
  26. b2Assert(bd->linearVelocity.IsValid());
  27. b2Assert(b2IsValid(bd->angle));
  28. b2Assert(b2IsValid(bd->angularVelocity));
  29. b2Assert(b2IsValid(bd->angularDamping) && bd->angularDamping >= 0.0f);
  30. b2Assert(b2IsValid(bd->linearDamping) && bd->linearDamping >= 0.0f);
  31. m_flags = 0;
  32. if (bd->bullet)
  33. {
  34. m_flags |= e_bulletFlag;
  35. }
  36. if (bd->fixedRotation)
  37. {
  38. m_flags |= e_fixedRotationFlag;
  39. }
  40. if (bd->allowSleep)
  41. {
  42. m_flags |= e_autoSleepFlag;
  43. }
  44. if (bd->awake)
  45. {
  46. m_flags |= e_awakeFlag;
  47. }
  48. if (bd->active)
  49. {
  50. m_flags |= e_activeFlag;
  51. }
  52. m_world = world;
  53. m_xf.p = bd->position;
  54. m_xf.q.Set(bd->angle);
  55. m_sweep.localCenter.SetZero();
  56. m_sweep.c0 = m_xf.p;
  57. m_sweep.c = m_xf.p;
  58. m_sweep.a0 = bd->angle;
  59. m_sweep.a = bd->angle;
  60. m_sweep.alpha0 = 0.0f;
  61. m_jointList = NULL;
  62. m_contactList = NULL;
  63. m_prev = NULL;
  64. m_next = NULL;
  65. m_linearVelocity = bd->linearVelocity;
  66. m_angularVelocity = bd->angularVelocity;
  67. m_linearDamping = bd->linearDamping;
  68. m_angularDamping = bd->angularDamping;
  69. m_gravityScale = bd->gravityScale;
  70. m_force.SetZero();
  71. m_torque = 0.0f;
  72. m_sleepTime = 0.0f;
  73. m_type = bd->type;
  74. if (m_type == b2_dynamicBody)
  75. {
  76. m_mass = 1.0f;
  77. m_invMass = 1.0f;
  78. }
  79. else
  80. {
  81. m_mass = 0.0f;
  82. m_invMass = 0.0f;
  83. }
  84. m_I = 0.0f;
  85. m_invI = 0.0f;
  86. m_userData = bd->userData;
  87. m_fixtureList = NULL;
  88. m_fixtureCount = 0;
  89. }
  90. b2Body::~b2Body()
  91. {
  92. // shapes and joints are destroyed in b2World::Destroy
  93. }
  94. void b2Body::SetType(b2BodyType type)
  95. {
  96. b2Assert(m_world->IsLocked() == false);
  97. if (m_world->IsLocked() == true)
  98. {
  99. return;
  100. }
  101. if (m_type == type)
  102. {
  103. return;
  104. }
  105. m_type = type;
  106. ResetMassData();
  107. if (m_type == b2_staticBody)
  108. {
  109. m_linearVelocity.SetZero();
  110. m_angularVelocity = 0.0f;
  111. m_sweep.a0 = m_sweep.a;
  112. m_sweep.c0 = m_sweep.c;
  113. SynchronizeFixtures();
  114. }
  115. SetAwake(true);
  116. m_force.SetZero();
  117. m_torque = 0.0f;
  118. // Delete the attached contacts.
  119. b2ContactEdge* ce = m_contactList;
  120. while (ce)
  121. {
  122. b2ContactEdge* ce0 = ce;
  123. ce = ce->next;
  124. m_world->m_contactManager.Destroy(ce0->contact);
  125. }
  126. m_contactList = NULL;
  127. // Touch the proxies so that new contacts will be created (when appropriate)
  128. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  129. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  130. {
  131. int32 proxyCount = f->m_proxyCount;
  132. for (int32 i = 0; i < proxyCount; ++i)
  133. {
  134. broadPhase->TouchProxy(f->m_proxies[i].proxyId);
  135. }
  136. }
  137. }
  138. b2Fixture* b2Body::CreateFixture(const b2FixtureDef* def)
  139. {
  140. b2Assert(m_world->IsLocked() == false);
  141. if (m_world->IsLocked() == true)
  142. {
  143. return NULL;
  144. }
  145. b2BlockAllocator* allocator = &m_world->m_blockAllocator;
  146. void* memory = allocator->Allocate(sizeof(b2Fixture));
  147. b2Fixture* fixture = new (memory) b2Fixture;
  148. fixture->Create(allocator, this, def);
  149. if (m_flags & e_activeFlag)
  150. {
  151. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  152. fixture->CreateProxies(broadPhase, m_xf);
  153. }
  154. fixture->m_next = m_fixtureList;
  155. m_fixtureList = fixture;
  156. ++m_fixtureCount;
  157. fixture->m_body = this;
  158. // Adjust mass properties if needed.
  159. if (fixture->m_density > 0.0f)
  160. {
  161. ResetMassData();
  162. }
  163. // Let the world know we have a new fixture. This will cause new contacts
  164. // to be created at the beginning of the next time step.
  165. m_world->m_flags |= b2World::e_newFixture;
  166. return fixture;
  167. }
  168. b2Fixture* b2Body::CreateFixture(const b2Shape* shape, float32 density)
  169. {
  170. b2FixtureDef def;
  171. def.shape = shape;
  172. def.density = density;
  173. return CreateFixture(&def);
  174. }
  175. void b2Body::DestroyFixture(b2Fixture* fixture)
  176. {
  177. b2Assert(m_world->IsLocked() == false);
  178. if (m_world->IsLocked() == true)
  179. {
  180. return;
  181. }
  182. b2Assert(fixture->m_body == this);
  183. // Remove the fixture from this body's singly linked list.
  184. b2Assert(m_fixtureCount > 0);
  185. b2Fixture** node = &m_fixtureList;
  186. bool found = false;
  187. while (*node != NULL)
  188. {
  189. if (*node == fixture)
  190. {
  191. *node = fixture->m_next;
  192. found = true;
  193. break;
  194. }
  195. node = &(*node)->m_next;
  196. }
  197. // You tried to remove a shape that is not attached to this body.
  198. b2Assert(found);
  199. // Destroy any contacts associated with the fixture.
  200. b2ContactEdge* edge = m_contactList;
  201. while (edge)
  202. {
  203. b2Contact* c = edge->contact;
  204. edge = edge->next;
  205. b2Fixture* fixtureA = c->GetFixtureA();
  206. b2Fixture* fixtureB = c->GetFixtureB();
  207. if (fixture == fixtureA || fixture == fixtureB)
  208. {
  209. // This destroys the contact and removes it from
  210. // this body's contact list.
  211. m_world->m_contactManager.Destroy(c);
  212. }
  213. }
  214. b2BlockAllocator* allocator = &m_world->m_blockAllocator;
  215. if (m_flags & e_activeFlag)
  216. {
  217. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  218. fixture->DestroyProxies(broadPhase);
  219. }
  220. fixture->Destroy(allocator);
  221. fixture->m_body = NULL;
  222. fixture->m_next = NULL;
  223. fixture->~b2Fixture();
  224. allocator->Free(fixture, sizeof(b2Fixture));
  225. --m_fixtureCount;
  226. // Reset the mass data.
  227. ResetMassData();
  228. }
  229. void b2Body::ResetMassData()
  230. {
  231. // Compute mass data from shapes. Each shape has its own density.
  232. m_mass = 0.0f;
  233. m_invMass = 0.0f;
  234. m_I = 0.0f;
  235. m_invI = 0.0f;
  236. m_sweep.localCenter.SetZero();
  237. // Static and kinematic bodies have zero mass.
  238. if (m_type == b2_staticBody || m_type == b2_kinematicBody)
  239. {
  240. m_sweep.c0 = m_xf.p;
  241. m_sweep.c = m_xf.p;
  242. m_sweep.a0 = m_sweep.a;
  243. return;
  244. }
  245. b2Assert(m_type == b2_dynamicBody);
  246. // Accumulate mass over all fixtures.
  247. b2Vec2 localCenter = b2Vec2_zero;
  248. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  249. {
  250. if (f->m_density == 0.0f)
  251. {
  252. continue;
  253. }
  254. b2MassData massData;
  255. f->GetMassData(&massData);
  256. m_mass += massData.mass;
  257. localCenter += massData.mass * massData.center;
  258. m_I += massData.I;
  259. }
  260. // Compute center of mass.
  261. if (m_mass > 0.0f)
  262. {
  263. m_invMass = 1.0f / m_mass;
  264. localCenter *= m_invMass;
  265. }
  266. else
  267. {
  268. // Force all dynamic bodies to have a positive mass.
  269. m_mass = 1.0f;
  270. m_invMass = 1.0f;
  271. }
  272. if (m_I > 0.0f && (m_flags & e_fixedRotationFlag) == 0)
  273. {
  274. // Center the inertia about the center of mass.
  275. m_I -= m_mass * b2Dot(localCenter, localCenter);
  276. b2Assert(m_I > 0.0f);
  277. m_invI = 1.0f / m_I;
  278. }
  279. else
  280. {
  281. m_I = 0.0f;
  282. m_invI = 0.0f;
  283. }
  284. // Move center of mass.
  285. b2Vec2 oldCenter = m_sweep.c;
  286. m_sweep.localCenter = localCenter;
  287. m_sweep.c0 = m_sweep.c = b2Mul(m_xf, m_sweep.localCenter);
  288. // Update center of mass velocity.
  289. m_linearVelocity += b2Cross(m_angularVelocity, m_sweep.c - oldCenter);
  290. }
  291. void b2Body::SetMassData(const b2MassData* massData)
  292. {
  293. b2Assert(m_world->IsLocked() == false);
  294. if (m_world->IsLocked() == true)
  295. {
  296. return;
  297. }
  298. if (m_type != b2_dynamicBody)
  299. {
  300. return;
  301. }
  302. m_invMass = 0.0f;
  303. m_I = 0.0f;
  304. m_invI = 0.0f;
  305. m_mass = massData->mass;
  306. if (m_mass <= 0.0f)
  307. {
  308. m_mass = 1.0f;
  309. }
  310. m_invMass = 1.0f / m_mass;
  311. if (massData->I > 0.0f && (m_flags & b2Body::e_fixedRotationFlag) == 0)
  312. {
  313. m_I = massData->I - m_mass * b2Dot(massData->center, massData->center);
  314. b2Assert(m_I > 0.0f);
  315. m_invI = 1.0f / m_I;
  316. }
  317. // Move center of mass.
  318. b2Vec2 oldCenter = m_sweep.c;
  319. m_sweep.localCenter = massData->center;
  320. m_sweep.c0 = m_sweep.c = b2Mul(m_xf, m_sweep.localCenter);
  321. // Update center of mass velocity.
  322. m_linearVelocity += b2Cross(m_angularVelocity, m_sweep.c - oldCenter);
  323. }
  324. bool b2Body::ShouldCollide(const b2Body* other) const
  325. {
  326. // At least one body should be dynamic.
  327. if (m_type != b2_dynamicBody && other->m_type != b2_dynamicBody)
  328. {
  329. return false;
  330. }
  331. // Does a joint prevent collision?
  332. for (b2JointEdge* jn = m_jointList; jn; jn = jn->next)
  333. {
  334. if (jn->other == other)
  335. {
  336. if (jn->joint->m_collideConnected == false)
  337. {
  338. return false;
  339. }
  340. }
  341. }
  342. return true;
  343. }
  344. void b2Body::SetTransform(const b2Vec2& position, float32 angle)
  345. {
  346. b2Assert(m_world->IsLocked() == false);
  347. if (m_world->IsLocked() == true)
  348. {
  349. return;
  350. }
  351. m_xf.q.Set(angle);
  352. m_xf.p = position;
  353. m_sweep.c = b2Mul(m_xf, m_sweep.localCenter);
  354. m_sweep.a = angle;
  355. m_sweep.c0 = m_sweep.c;
  356. m_sweep.a0 = angle;
  357. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  358. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  359. {
  360. f->Synchronize(broadPhase, m_xf, m_xf);
  361. }
  362. }
  363. void b2Body::SynchronizeFixtures()
  364. {
  365. b2Transform xf1;
  366. xf1.q.Set(m_sweep.a0);
  367. xf1.p = m_sweep.c0 - b2Mul(xf1.q, m_sweep.localCenter);
  368. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  369. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  370. {
  371. f->Synchronize(broadPhase, xf1, m_xf);
  372. }
  373. }
  374. void b2Body::SetActive(bool flag)
  375. {
  376. b2Assert(m_world->IsLocked() == false);
  377. if (flag == IsActive())
  378. {
  379. return;
  380. }
  381. if (flag)
  382. {
  383. m_flags |= e_activeFlag;
  384. // Create all proxies.
  385. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  386. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  387. {
  388. f->CreateProxies(broadPhase, m_xf);
  389. }
  390. // Contacts are created the next time step.
  391. }
  392. else
  393. {
  394. m_flags &= ~e_activeFlag;
  395. // Destroy all proxies.
  396. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  397. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  398. {
  399. f->DestroyProxies(broadPhase);
  400. }
  401. // Destroy the attached contacts.
  402. b2ContactEdge* ce = m_contactList;
  403. while (ce)
  404. {
  405. b2ContactEdge* ce0 = ce;
  406. ce = ce->next;
  407. m_world->m_contactManager.Destroy(ce0->contact);
  408. }
  409. m_contactList = NULL;
  410. }
  411. }
  412. void b2Body::SetFixedRotation(bool flag)
  413. {
  414. bool status = (m_flags & e_fixedRotationFlag) == e_fixedRotationFlag;
  415. if (status == flag)
  416. {
  417. return;
  418. }
  419. if (flag)
  420. {
  421. m_flags |= e_fixedRotationFlag;
  422. }
  423. else
  424. {
  425. m_flags &= ~e_fixedRotationFlag;
  426. }
  427. m_angularVelocity = 0.0f;
  428. ResetMassData();
  429. }
  430. void b2Body::Dump()
  431. {
  432. int32 bodyIndex = m_islandIndex;
  433. b2Log("{\n");
  434. b2Log(" b2BodyDef bd;\n");
  435. b2Log(" bd.type = b2BodyType(%d);\n", m_type);
  436. b2Log(" bd.position.Set(%.15lef, %.15lef);\n", m_xf.p.x, m_xf.p.y);
  437. b2Log(" bd.angle = %.15lef;\n", m_sweep.a);
  438. b2Log(" bd.linearVelocity.Set(%.15lef, %.15lef);\n", m_linearVelocity.x, m_linearVelocity.y);
  439. b2Log(" bd.angularVelocity = %.15lef;\n", m_angularVelocity);
  440. b2Log(" bd.linearDamping = %.15lef;\n", m_linearDamping);
  441. b2Log(" bd.angularDamping = %.15lef;\n", m_angularDamping);
  442. b2Log(" bd.allowSleep = bool(%d);\n", m_flags & e_autoSleepFlag);
  443. b2Log(" bd.awake = bool(%d);\n", m_flags & e_awakeFlag);
  444. b2Log(" bd.fixedRotation = bool(%d);\n", m_flags & e_fixedRotationFlag);
  445. b2Log(" bd.bullet = bool(%d);\n", m_flags & e_bulletFlag);
  446. b2Log(" bd.active = bool(%d);\n", m_flags & e_activeFlag);
  447. b2Log(" bd.gravityScale = %.15lef;\n", m_gravityScale);
  448. b2Log(" bodies[%d] = m_world->CreateBody(&bd);\n", m_islandIndex);
  449. b2Log("\n");
  450. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  451. {
  452. b2Log(" {\n");
  453. f->Dump(bodyIndex);
  454. b2Log(" }\n");
  455. }
  456. b2Log("}\n");
  457. }