The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

515 lines
11KB

  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 "b2Body.h"
  19. #include "b2Fixture.h"
  20. #include "b2World.h"
  21. #include "Contacts/b2Contact.h"
  22. #include "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. // Since the body type changed, we need to flag contacts for filtering.
  119. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  120. {
  121. f->Refilter();
  122. }
  123. }
  124. b2Fixture* b2Body::CreateFixture(const b2FixtureDef* def)
  125. {
  126. b2Assert(m_world->IsLocked() == false);
  127. if (m_world->IsLocked() == true)
  128. {
  129. return NULL;
  130. }
  131. b2BlockAllocator* allocator = &m_world->m_blockAllocator;
  132. void* memory = allocator->Allocate(sizeof(b2Fixture));
  133. b2Fixture* fixture = new (memory) b2Fixture;
  134. fixture->Create(allocator, this, def);
  135. if (m_flags & e_activeFlag)
  136. {
  137. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  138. fixture->CreateProxies(broadPhase, m_xf);
  139. }
  140. fixture->m_next = m_fixtureList;
  141. m_fixtureList = fixture;
  142. ++m_fixtureCount;
  143. fixture->m_body = this;
  144. // Adjust mass properties if needed.
  145. if (fixture->m_density > 0.0f)
  146. {
  147. ResetMassData();
  148. }
  149. // Let the world know we have a new fixture. This will cause new contacts
  150. // to be created at the beginning of the next time step.
  151. m_world->m_flags |= b2World::e_newFixture;
  152. return fixture;
  153. }
  154. b2Fixture* b2Body::CreateFixture(const b2Shape* shape, float32 density)
  155. {
  156. b2FixtureDef def;
  157. def.shape = shape;
  158. def.density = density;
  159. return CreateFixture(&def);
  160. }
  161. void b2Body::DestroyFixture(b2Fixture* fixture)
  162. {
  163. b2Assert(m_world->IsLocked() == false);
  164. if (m_world->IsLocked() == true)
  165. {
  166. return;
  167. }
  168. b2Assert(fixture->m_body == this);
  169. // Remove the fixture from this body's singly linked list.
  170. b2Assert(m_fixtureCount > 0);
  171. b2Fixture** node = &m_fixtureList;
  172. bool found = false;
  173. while (*node != NULL)
  174. {
  175. if (*node == fixture)
  176. {
  177. *node = fixture->m_next;
  178. found = true;
  179. break;
  180. }
  181. node = &(*node)->m_next;
  182. }
  183. // You tried to remove a shape that is not attached to this body.
  184. b2Assert(found);
  185. // Destroy any contacts associated with the fixture.
  186. b2ContactEdge* edge = m_contactList;
  187. while (edge)
  188. {
  189. b2Contact* c = edge->contact;
  190. edge = edge->next;
  191. b2Fixture* fixtureA = c->GetFixtureA();
  192. b2Fixture* fixtureB = c->GetFixtureB();
  193. if (fixture == fixtureA || fixture == fixtureB)
  194. {
  195. // This destroys the contact and removes it from
  196. // this body's contact list.
  197. m_world->m_contactManager.Destroy(c);
  198. }
  199. }
  200. b2BlockAllocator* allocator = &m_world->m_blockAllocator;
  201. if (m_flags & e_activeFlag)
  202. {
  203. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  204. fixture->DestroyProxies(broadPhase);
  205. }
  206. fixture->Destroy(allocator);
  207. fixture->m_body = NULL;
  208. fixture->m_next = NULL;
  209. fixture->~b2Fixture();
  210. allocator->Free(fixture, sizeof(b2Fixture));
  211. --m_fixtureCount;
  212. // Reset the mass data.
  213. ResetMassData();
  214. }
  215. void b2Body::ResetMassData()
  216. {
  217. // Compute mass data from shapes. Each shape has its own density.
  218. m_mass = 0.0f;
  219. m_invMass = 0.0f;
  220. m_I = 0.0f;
  221. m_invI = 0.0f;
  222. m_sweep.localCenter.SetZero();
  223. // Static and kinematic bodies have zero mass.
  224. if (m_type == b2_staticBody || m_type == b2_kinematicBody)
  225. {
  226. m_sweep.c0 = m_xf.p;
  227. m_sweep.c = m_xf.p;
  228. m_sweep.a0 = m_sweep.a;
  229. return;
  230. }
  231. b2Assert(m_type == b2_dynamicBody);
  232. // Accumulate mass over all fixtures.
  233. b2Vec2 localCenter = b2Vec2_zero;
  234. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  235. {
  236. if (f->m_density == 0.0f)
  237. {
  238. continue;
  239. }
  240. b2MassData massData;
  241. f->GetMassData(&massData);
  242. m_mass += massData.mass;
  243. localCenter += massData.mass * massData.center;
  244. m_I += massData.I;
  245. }
  246. // Compute center of mass.
  247. if (m_mass > 0.0f)
  248. {
  249. m_invMass = 1.0f / m_mass;
  250. localCenter *= m_invMass;
  251. }
  252. else
  253. {
  254. // Force all dynamic bodies to have a positive mass.
  255. m_mass = 1.0f;
  256. m_invMass = 1.0f;
  257. }
  258. if (m_I > 0.0f && (m_flags & e_fixedRotationFlag) == 0)
  259. {
  260. // Center the inertia about the center of mass.
  261. m_I -= m_mass * b2Dot(localCenter, localCenter);
  262. b2Assert(m_I > 0.0f);
  263. m_invI = 1.0f / m_I;
  264. }
  265. else
  266. {
  267. m_I = 0.0f;
  268. m_invI = 0.0f;
  269. }
  270. // Move center of mass.
  271. b2Vec2 oldCenter = m_sweep.c;
  272. m_sweep.localCenter = localCenter;
  273. m_sweep.c0 = m_sweep.c = b2Mul(m_xf, m_sweep.localCenter);
  274. // Update center of mass velocity.
  275. m_linearVelocity += b2Cross(m_angularVelocity, m_sweep.c - oldCenter);
  276. }
  277. void b2Body::SetMassData(const b2MassData* massData)
  278. {
  279. b2Assert(m_world->IsLocked() == false);
  280. if (m_world->IsLocked() == true)
  281. {
  282. return;
  283. }
  284. if (m_type != b2_dynamicBody)
  285. {
  286. return;
  287. }
  288. m_invMass = 0.0f;
  289. m_I = 0.0f;
  290. m_invI = 0.0f;
  291. m_mass = massData->mass;
  292. if (m_mass <= 0.0f)
  293. {
  294. m_mass = 1.0f;
  295. }
  296. m_invMass = 1.0f / m_mass;
  297. if (massData->I > 0.0f && (m_flags & b2Body::e_fixedRotationFlag) == 0)
  298. {
  299. m_I = massData->I - m_mass * b2Dot(massData->center, massData->center);
  300. b2Assert(m_I > 0.0f);
  301. m_invI = 1.0f / m_I;
  302. }
  303. // Move center of mass.
  304. b2Vec2 oldCenter = m_sweep.c;
  305. m_sweep.localCenter = massData->center;
  306. m_sweep.c0 = m_sweep.c = b2Mul(m_xf, m_sweep.localCenter);
  307. // Update center of mass velocity.
  308. m_linearVelocity += b2Cross(m_angularVelocity, m_sweep.c - oldCenter);
  309. }
  310. bool b2Body::ShouldCollide(const b2Body* other) const
  311. {
  312. // At least one body should be dynamic.
  313. if (m_type != b2_dynamicBody && other->m_type != b2_dynamicBody)
  314. {
  315. return false;
  316. }
  317. // Does a joint prevent collision?
  318. for (b2JointEdge* jn = m_jointList; jn; jn = jn->next)
  319. {
  320. if (jn->other == other)
  321. {
  322. if (jn->joint->m_collideConnected == false)
  323. {
  324. return false;
  325. }
  326. }
  327. }
  328. return true;
  329. }
  330. void b2Body::SetTransform(const b2Vec2& position, float32 angle)
  331. {
  332. b2Assert(m_world->IsLocked() == false);
  333. if (m_world->IsLocked() == true)
  334. {
  335. return;
  336. }
  337. m_xf.q.Set(angle);
  338. m_xf.p = position;
  339. m_sweep.c = b2Mul(m_xf, m_sweep.localCenter);
  340. m_sweep.a = angle;
  341. m_sweep.c0 = m_sweep.c;
  342. m_sweep.a0 = angle;
  343. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  344. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  345. {
  346. f->Synchronize(broadPhase, m_xf, m_xf);
  347. }
  348. m_world->m_contactManager.FindNewContacts();
  349. }
  350. void b2Body::SynchronizeFixtures()
  351. {
  352. b2Transform xf1;
  353. xf1.q.Set(m_sweep.a0);
  354. xf1.p = m_sweep.c0 - b2Mul(xf1.q, m_sweep.localCenter);
  355. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  356. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  357. {
  358. f->Synchronize(broadPhase, xf1, m_xf);
  359. }
  360. }
  361. void b2Body::SetActive(bool flag)
  362. {
  363. b2Assert(m_world->IsLocked() == false);
  364. if (flag == IsActive())
  365. {
  366. return;
  367. }
  368. if (flag)
  369. {
  370. m_flags |= e_activeFlag;
  371. // Create all proxies.
  372. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  373. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  374. {
  375. f->CreateProxies(broadPhase, m_xf);
  376. }
  377. // Contacts are created the next time step.
  378. }
  379. else
  380. {
  381. m_flags &= ~e_activeFlag;
  382. // Destroy all proxies.
  383. b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase;
  384. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  385. {
  386. f->DestroyProxies(broadPhase);
  387. }
  388. // Destroy the attached contacts.
  389. b2ContactEdge* ce = m_contactList;
  390. while (ce)
  391. {
  392. b2ContactEdge* ce0 = ce;
  393. ce = ce->next;
  394. m_world->m_contactManager.Destroy(ce0->contact);
  395. }
  396. m_contactList = NULL;
  397. }
  398. }
  399. void b2Body::Dump()
  400. {
  401. int32 bodyIndex = m_islandIndex;
  402. b2Log("{\n");
  403. b2Log(" b2BodyDef bd;\n");
  404. b2Log(" bd.type = b2BodyType(%d);\n", m_type);
  405. b2Log(" bd.position.Set(%.15lef, %.15lef);\n", m_xf.p.x, m_xf.p.y);
  406. b2Log(" bd.angle = %.15lef;\n", m_sweep.a);
  407. b2Log(" bd.linearVelocity.Set(%.15lef, %.15lef);\n", m_linearVelocity.x, m_linearVelocity.y);
  408. b2Log(" bd.angularVelocity = %.15lef;\n", m_angularVelocity);
  409. b2Log(" bd.linearDamping = %.15lef;\n", m_linearDamping);
  410. b2Log(" bd.angularDamping = %.15lef;\n", m_angularDamping);
  411. b2Log(" bd.allowSleep = bool(%d);\n", m_flags & e_autoSleepFlag);
  412. b2Log(" bd.awake = bool(%d);\n", m_flags & e_awakeFlag);
  413. b2Log(" bd.fixedRotation = bool(%d);\n", m_flags & e_fixedRotationFlag);
  414. b2Log(" bd.bullet = bool(%d);\n", m_flags & e_bulletFlag);
  415. b2Log(" bd.active = bool(%d);\n", m_flags & e_activeFlag);
  416. b2Log(" bd.gravityScale = %.15lef;\n", m_gravityScale);
  417. b2Log(" bodies[%d] = m_world->CreateBody(&bd);\n", m_islandIndex);
  418. b2Log("\n");
  419. for (b2Fixture* f = m_fixtureList; f; f = f->m_next)
  420. {
  421. b2Log(" {\n");
  422. f->Dump(bodyIndex);
  423. b2Log(" }\n");
  424. }
  425. b2Log("}\n");
  426. }