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.

846 lines
22KB

  1. /*
  2. * Copyright (c) 2006-2011 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. #ifndef B2_BODY_H
  19. #define B2_BODY_H
  20. #include "../Common/b2Math.h"
  21. #include "../Collision/Shapes/b2Shape.h"
  22. class b2Fixture;
  23. class b2Joint;
  24. class b2Contact;
  25. class b2Controller;
  26. class b2World;
  27. struct b2FixtureDef;
  28. struct b2JointEdge;
  29. struct b2ContactEdge;
  30. /// The body type.
  31. /// static: zero mass, zero velocity, may be manually moved
  32. /// kinematic: zero mass, non-zero velocity set by user, moved by solver
  33. /// dynamic: positive mass, non-zero velocity determined by forces, moved by solver
  34. enum b2BodyType
  35. {
  36. b2_staticBody = 0,
  37. b2_kinematicBody,
  38. b2_dynamicBody
  39. // TODO_ERIN
  40. //b2_bulletBody,
  41. };
  42. /// A body definition holds all the data needed to construct a rigid body.
  43. /// You can safely re-use body definitions. Shapes are added to a body after construction.
  44. struct b2BodyDef
  45. {
  46. /// This constructor sets the body definition default values.
  47. b2BodyDef()
  48. {
  49. userData = NULL;
  50. position.Set(0.0f, 0.0f);
  51. angle = 0.0f;
  52. linearVelocity.Set(0.0f, 0.0f);
  53. angularVelocity = 0.0f;
  54. linearDamping = 0.0f;
  55. angularDamping = 0.0f;
  56. allowSleep = true;
  57. awake = true;
  58. fixedRotation = false;
  59. bullet = false;
  60. type = b2_staticBody;
  61. active = true;
  62. gravityScale = 1.0f;
  63. }
  64. /// The body type: static, kinematic, or dynamic.
  65. /// Note: if a dynamic body would have zero mass, the mass is set to one.
  66. b2BodyType type;
  67. /// The world position of the body. Avoid creating bodies at the origin
  68. /// since this can lead to many overlapping shapes.
  69. b2Vec2 position;
  70. /// The world angle of the body in radians.
  71. float32 angle;
  72. /// The linear velocity of the body's origin in world co-ordinates.
  73. b2Vec2 linearVelocity;
  74. /// The angular velocity of the body.
  75. float32 angularVelocity;
  76. /// Linear damping is use to reduce the linear velocity. The damping parameter
  77. /// can be larger than 1.0f but the damping effect becomes sensitive to the
  78. /// time step when the damping parameter is large.
  79. float32 linearDamping;
  80. /// Angular damping is use to reduce the angular velocity. The damping parameter
  81. /// can be larger than 1.0f but the damping effect becomes sensitive to the
  82. /// time step when the damping parameter is large.
  83. float32 angularDamping;
  84. /// Set this flag to false if this body should never fall asleep. Note that
  85. /// this increases CPU usage.
  86. bool allowSleep;
  87. /// Is this body initially awake or sleeping?
  88. bool awake;
  89. /// Should this body be prevented from rotating? Useful for characters.
  90. bool fixedRotation;
  91. /// Is this a fast moving body that should be prevented from tunneling through
  92. /// other moving bodies? Note that all bodies are prevented from tunneling through
  93. /// kinematic and static bodies. This setting is only considered on dynamic bodies.
  94. /// @warning You should use this flag sparingly since it increases processing time.
  95. bool bullet;
  96. /// Does this body start out active?
  97. bool active;
  98. /// Use this to store application specific body data.
  99. void* userData;
  100. /// Scale the gravity applied to this body.
  101. float32 gravityScale;
  102. };
  103. /// A rigid body. These are created via b2World::CreateBody.
  104. class b2Body
  105. {
  106. public:
  107. /// Creates a fixture and attach it to this body. Use this function if you need
  108. /// to set some fixture parameters, like friction. Otherwise you can create the
  109. /// fixture directly from a shape.
  110. /// If the density is non-zero, this function automatically updates the mass of the body.
  111. /// Contacts are not created until the next time step.
  112. /// @param def the fixture definition.
  113. /// @warning This function is locked during callbacks.
  114. b2Fixture* CreateFixture(const b2FixtureDef* def);
  115. /// Creates a fixture from a shape and attach it to this body.
  116. /// This is a convenience function. Use b2FixtureDef if you need to set parameters
  117. /// like friction, restitution, user data, or filtering.
  118. /// If the density is non-zero, this function automatically updates the mass of the body.
  119. /// @param shape the shape to be cloned.
  120. /// @param density the shape density (set to zero for static bodies).
  121. /// @warning This function is locked during callbacks.
  122. b2Fixture* CreateFixture(const b2Shape* shape, float32 density);
  123. /// Destroy a fixture. This removes the fixture from the broad-phase and
  124. /// destroys all contacts associated with this fixture. This will
  125. /// automatically adjust the mass of the body if the body is dynamic and the
  126. /// fixture has positive density.
  127. /// All fixtures attached to a body are implicitly destroyed when the body is destroyed.
  128. /// @param fixture the fixture to be removed.
  129. /// @warning This function is locked during callbacks.
  130. void DestroyFixture(b2Fixture* fixture);
  131. /// Set the position of the body's origin and rotation.
  132. /// This breaks any contacts and wakes the other bodies.
  133. /// Manipulating a body's transform may cause non-physical behavior.
  134. /// @param position the world position of the body's local origin.
  135. /// @param angle the world rotation in radians.
  136. void SetTransform(const b2Vec2& position, float32 angle);
  137. /// Get the body transform for the body's origin.
  138. /// @return the world transform of the body's origin.
  139. const b2Transform& GetTransform() const;
  140. /// Get the world body origin position.
  141. /// @return the world position of the body's origin.
  142. const b2Vec2& GetPosition() const;
  143. /// Get the angle in radians.
  144. /// @return the current world rotation angle in radians.
  145. float32 GetAngle() const;
  146. /// Get the world position of the center of mass.
  147. const b2Vec2& GetWorldCenter() const;
  148. /// Get the local position of the center of mass.
  149. const b2Vec2& GetLocalCenter() const;
  150. /// Set the linear velocity of the center of mass.
  151. /// @param v the new linear velocity of the center of mass.
  152. void SetLinearVelocity(const b2Vec2& v);
  153. /// Get the linear velocity of the center of mass.
  154. /// @return the linear velocity of the center of mass.
  155. b2Vec2 GetLinearVelocity() const;
  156. /// Set the angular velocity.
  157. /// @param omega the new angular velocity in radians/second.
  158. void SetAngularVelocity(float32 omega);
  159. /// Get the angular velocity.
  160. /// @return the angular velocity in radians/second.
  161. float32 GetAngularVelocity() const;
  162. /// Apply a force at a world point. If the force is not
  163. /// applied at the center of mass, it will generate a torque and
  164. /// affect the angular velocity. This wakes up the body.
  165. /// @param force the world force vector, usually in Newtons (N).
  166. /// @param point the world position of the point of application.
  167. void ApplyForce(const b2Vec2& force, const b2Vec2& point);
  168. /// Apply a force to the center of mass. This wakes up the body.
  169. /// @param force the world force vector, usually in Newtons (N).
  170. void ApplyForceToCenter(const b2Vec2& force);
  171. /// Apply a torque. This affects the angular velocity
  172. /// without affecting the linear velocity of the center of mass.
  173. /// This wakes up the body.
  174. /// @param torque about the z-axis (out of the screen), usually in N-m.
  175. void ApplyTorque(float32 torque);
  176. /// Apply an impulse at a point. This immediately modifies the velocity.
  177. /// It also modifies the angular velocity if the point of application
  178. /// is not at the center of mass. This wakes up the body.
  179. /// @param impulse the world impulse vector, usually in N-seconds or kg-m/s.
  180. /// @param point the world position of the point of application.
  181. void ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point);
  182. /// Apply an angular impulse.
  183. /// @param impulse the angular impulse in units of kg*m*m/s
  184. void ApplyAngularImpulse(float32 impulse);
  185. /// Get the total mass of the body.
  186. /// @return the mass, usually in kilograms (kg).
  187. float32 GetMass() const;
  188. /// Get the rotational inertia of the body about the local origin.
  189. /// @return the rotational inertia, usually in kg-m^2.
  190. float32 GetInertia() const;
  191. /// Get the mass data of the body.
  192. /// @return a struct containing the mass, inertia and center of the body.
  193. void GetMassData(b2MassData* data) const;
  194. /// Set the mass properties to override the mass properties of the fixtures.
  195. /// Note that this changes the center of mass position.
  196. /// Note that creating or destroying fixtures can also alter the mass.
  197. /// This function has no effect if the body isn't dynamic.
  198. /// @param massData the mass properties.
  199. void SetMassData(const b2MassData* data);
  200. /// This resets the mass properties to the sum of the mass properties of the fixtures.
  201. /// This normally does not need to be called unless you called SetMassData to override
  202. /// the mass and you later want to reset the mass.
  203. void ResetMassData();
  204. /// Get the world coordinates of a point given the local coordinates.
  205. /// @param localPoint a point on the body measured relative the the body's origin.
  206. /// @return the same point expressed in world coordinates.
  207. b2Vec2 GetWorldPoint(const b2Vec2& localPoint) const;
  208. /// Get the world coordinates of a vector given the local coordinates.
  209. /// @param localVector a vector fixed in the body.
  210. /// @return the same vector expressed in world coordinates.
  211. b2Vec2 GetWorldVector(const b2Vec2& localVector) const;
  212. /// Gets a local point relative to the body's origin given a world point.
  213. /// @param a point in world coordinates.
  214. /// @return the corresponding local point relative to the body's origin.
  215. b2Vec2 GetLocalPoint(const b2Vec2& worldPoint) const;
  216. /// Gets a local vector given a world vector.
  217. /// @param a vector in world coordinates.
  218. /// @return the corresponding local vector.
  219. b2Vec2 GetLocalVector(const b2Vec2& worldVector) const;
  220. /// Get the world linear velocity of a world point attached to this body.
  221. /// @param a point in world coordinates.
  222. /// @return the world velocity of a point.
  223. b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const;
  224. /// Get the world velocity of a local point.
  225. /// @param a point in local coordinates.
  226. /// @return the world velocity of a point.
  227. b2Vec2 GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const;
  228. /// Get the linear damping of the body.
  229. float32 GetLinearDamping() const;
  230. /// Set the linear damping of the body.
  231. void SetLinearDamping(float32 linearDamping);
  232. /// Get the angular damping of the body.
  233. float32 GetAngularDamping() const;
  234. /// Set the angular damping of the body.
  235. void SetAngularDamping(float32 angularDamping);
  236. /// Get the gravity scale of the body.
  237. float32 GetGravityScale() const;
  238. /// Set the gravity scale of the body.
  239. void SetGravityScale(float32 scale);
  240. /// Set the type of this body. This may alter the mass and velocity.
  241. void SetType(b2BodyType type);
  242. /// Get the type of this body.
  243. b2BodyType GetType() const;
  244. /// Should this body be treated like a bullet for continuous collision detection?
  245. void SetBullet(bool flag);
  246. /// Is this body treated like a bullet for continuous collision detection?
  247. bool IsBullet() const;
  248. /// You can disable sleeping on this body. If you disable sleeping, the
  249. /// body will be woken.
  250. void SetSleepingAllowed(bool flag);
  251. /// Is this body allowed to sleep
  252. bool IsSleepingAllowed() const;
  253. /// Set the sleep state of the body. A sleeping body has very
  254. /// low CPU cost.
  255. /// @param flag set to true to put body to sleep, false to wake it.
  256. void SetAwake(bool flag);
  257. /// Get the sleeping state of this body.
  258. /// @return true if the body is sleeping.
  259. bool IsAwake() const;
  260. /// Set the active state of the body. An inactive body is not
  261. /// simulated and cannot be collided with or woken up.
  262. /// If you pass a flag of true, all fixtures will be added to the
  263. /// broad-phase.
  264. /// If you pass a flag of false, all fixtures will be removed from
  265. /// the broad-phase and all contacts will be destroyed.
  266. /// Fixtures and joints are otherwise unaffected. You may continue
  267. /// to create/destroy fixtures and joints on inactive bodies.
  268. /// Fixtures on an inactive body are implicitly inactive and will
  269. /// not participate in collisions, ray-casts, or queries.
  270. /// Joints connected to an inactive body are implicitly inactive.
  271. /// An inactive body is still owned by a b2World object and remains
  272. /// in the body list.
  273. void SetActive(bool flag);
  274. /// Get the active state of the body.
  275. bool IsActive() const;
  276. /// Set this body to have fixed rotation. This causes the mass
  277. /// to be reset.
  278. void SetFixedRotation(bool flag);
  279. /// Does this body have fixed rotation?
  280. bool IsFixedRotation() const;
  281. /// Get the list of all fixtures attached to this body.
  282. b2Fixture* GetFixtureList();
  283. const b2Fixture* GetFixtureList() const;
  284. /// Get the list of all joints attached to this body.
  285. b2JointEdge* GetJointList();
  286. const b2JointEdge* GetJointList() const;
  287. /// Get the list of all contacts attached to this body.
  288. /// @warning this list changes during the time step and you may
  289. /// miss some collisions if you don't use b2ContactListener.
  290. b2ContactEdge* GetContactList();
  291. const b2ContactEdge* GetContactList() const;
  292. /// Get the next body in the world's body list.
  293. b2Body* GetNext();
  294. const b2Body* GetNext() const;
  295. /// Get the user data pointer that was provided in the body definition.
  296. void* GetUserData() const;
  297. /// Set the user data. Use this to store your application specific data.
  298. void SetUserData(void* data);
  299. /// Get the parent world of this body.
  300. b2World* GetWorld();
  301. const b2World* GetWorld() const;
  302. /// Dump this body to a log file
  303. void Dump();
  304. private:
  305. friend class b2World;
  306. friend class b2Island;
  307. friend class b2ContactManager;
  308. friend class b2ContactSolver;
  309. friend class b2Contact;
  310. friend class b2DistanceJoint;
  311. friend class b2GearJoint;
  312. friend class b2WheelJoint;
  313. friend class b2MouseJoint;
  314. friend class b2PrismaticJoint;
  315. friend class b2PulleyJoint;
  316. friend class b2RevoluteJoint;
  317. friend class b2WeldJoint;
  318. friend class b2FrictionJoint;
  319. friend class b2RopeJoint;
  320. // m_flags
  321. enum
  322. {
  323. e_islandFlag = 0x0001,
  324. e_awakeFlag = 0x0002,
  325. e_autoSleepFlag = 0x0004,
  326. e_bulletFlag = 0x0008,
  327. e_fixedRotationFlag = 0x0010,
  328. e_activeFlag = 0x0020,
  329. e_toiFlag = 0x0040
  330. };
  331. b2Body(const b2BodyDef* bd, b2World* world);
  332. ~b2Body();
  333. void SynchronizeFixtures();
  334. void SynchronizeTransform();
  335. // This is used to prevent connected bodies from colliding.
  336. // It may lie, depending on the collideConnected flag.
  337. bool ShouldCollide(const b2Body* other) const;
  338. void Advance(float32 t);
  339. b2BodyType m_type;
  340. juce::uint16 m_flags;
  341. juce::int32 m_islandIndex;
  342. b2Transform m_xf; // the body origin transform
  343. b2Sweep m_sweep; // the swept motion for CCD
  344. b2Vec2 m_linearVelocity;
  345. float32 m_angularVelocity;
  346. b2Vec2 m_force;
  347. float32 m_torque;
  348. b2World* m_world;
  349. b2Body* m_prev;
  350. b2Body* m_next;
  351. b2Fixture* m_fixtureList;
  352. juce::int32 m_fixtureCount;
  353. b2JointEdge* m_jointList;
  354. b2ContactEdge* m_contactList;
  355. float32 m_mass, m_invMass;
  356. // Rotational inertia about the center of mass.
  357. float32 m_I, m_invI;
  358. float32 m_linearDamping;
  359. float32 m_angularDamping;
  360. float32 m_gravityScale;
  361. float32 m_sleepTime;
  362. void* m_userData;
  363. };
  364. inline b2BodyType b2Body::GetType() const
  365. {
  366. return m_type;
  367. }
  368. inline const b2Transform& b2Body::GetTransform() const
  369. {
  370. return m_xf;
  371. }
  372. inline const b2Vec2& b2Body::GetPosition() const
  373. {
  374. return m_xf.p;
  375. }
  376. inline float32 b2Body::GetAngle() const
  377. {
  378. return m_sweep.a;
  379. }
  380. inline const b2Vec2& b2Body::GetWorldCenter() const
  381. {
  382. return m_sweep.c;
  383. }
  384. inline const b2Vec2& b2Body::GetLocalCenter() const
  385. {
  386. return m_sweep.localCenter;
  387. }
  388. inline void b2Body::SetLinearVelocity(const b2Vec2& v)
  389. {
  390. if (m_type == b2_staticBody)
  391. {
  392. return;
  393. }
  394. if (b2Dot(v,v) > 0.0f)
  395. {
  396. SetAwake(true);
  397. }
  398. m_linearVelocity = v;
  399. }
  400. inline b2Vec2 b2Body::GetLinearVelocity() const
  401. {
  402. return m_linearVelocity;
  403. }
  404. inline void b2Body::SetAngularVelocity(float32 w)
  405. {
  406. if (m_type == b2_staticBody)
  407. {
  408. return;
  409. }
  410. if (w * w > 0.0f)
  411. {
  412. SetAwake(true);
  413. }
  414. m_angularVelocity = w;
  415. }
  416. inline float32 b2Body::GetAngularVelocity() const
  417. {
  418. return m_angularVelocity;
  419. }
  420. inline float32 b2Body::GetMass() const
  421. {
  422. return m_mass;
  423. }
  424. inline float32 b2Body::GetInertia() const
  425. {
  426. return m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
  427. }
  428. inline void b2Body::GetMassData(b2MassData* data) const
  429. {
  430. data->mass = m_mass;
  431. data->I = m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
  432. data->center = m_sweep.localCenter;
  433. }
  434. inline b2Vec2 b2Body::GetWorldPoint(const b2Vec2& localPoint) const
  435. {
  436. return b2Mul(m_xf, localPoint);
  437. }
  438. inline b2Vec2 b2Body::GetWorldVector(const b2Vec2& localVector) const
  439. {
  440. return b2Mul(m_xf.q, localVector);
  441. }
  442. inline b2Vec2 b2Body::GetLocalPoint(const b2Vec2& worldPoint) const
  443. {
  444. return b2MulT(m_xf, worldPoint);
  445. }
  446. inline b2Vec2 b2Body::GetLocalVector(const b2Vec2& worldVector) const
  447. {
  448. return b2MulT(m_xf.q, worldVector);
  449. }
  450. inline b2Vec2 b2Body::GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const
  451. {
  452. return m_linearVelocity + b2Cross(m_angularVelocity, worldPoint - m_sweep.c);
  453. }
  454. inline b2Vec2 b2Body::GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const
  455. {
  456. return GetLinearVelocityFromWorldPoint(GetWorldPoint(localPoint));
  457. }
  458. inline float32 b2Body::GetLinearDamping() const
  459. {
  460. return m_linearDamping;
  461. }
  462. inline void b2Body::SetLinearDamping(float32 linearDamping)
  463. {
  464. m_linearDamping = linearDamping;
  465. }
  466. inline float32 b2Body::GetAngularDamping() const
  467. {
  468. return m_angularDamping;
  469. }
  470. inline void b2Body::SetAngularDamping(float32 angularDamping)
  471. {
  472. m_angularDamping = angularDamping;
  473. }
  474. inline float32 b2Body::GetGravityScale() const
  475. {
  476. return m_gravityScale;
  477. }
  478. inline void b2Body::SetGravityScale(float32 scale)
  479. {
  480. m_gravityScale = scale;
  481. }
  482. inline void b2Body::SetBullet(bool flag)
  483. {
  484. if (flag)
  485. {
  486. m_flags |= e_bulletFlag;
  487. }
  488. else
  489. {
  490. m_flags &= ~e_bulletFlag;
  491. }
  492. }
  493. inline bool b2Body::IsBullet() const
  494. {
  495. return (m_flags & e_bulletFlag) == e_bulletFlag;
  496. }
  497. inline void b2Body::SetAwake(bool flag)
  498. {
  499. if (flag)
  500. {
  501. if ((m_flags & e_awakeFlag) == 0)
  502. {
  503. m_flags |= e_awakeFlag;
  504. m_sleepTime = 0.0f;
  505. }
  506. }
  507. else
  508. {
  509. m_flags &= ~e_awakeFlag;
  510. m_sleepTime = 0.0f;
  511. m_linearVelocity.SetZero();
  512. m_angularVelocity = 0.0f;
  513. m_force.SetZero();
  514. m_torque = 0.0f;
  515. }
  516. }
  517. inline bool b2Body::IsAwake() const
  518. {
  519. return (m_flags & e_awakeFlag) == e_awakeFlag;
  520. }
  521. inline bool b2Body::IsActive() const
  522. {
  523. return (m_flags & e_activeFlag) == e_activeFlag;
  524. }
  525. inline void b2Body::SetFixedRotation(bool flag)
  526. {
  527. if (flag)
  528. {
  529. m_flags |= e_fixedRotationFlag;
  530. }
  531. else
  532. {
  533. m_flags &= ~e_fixedRotationFlag;
  534. }
  535. ResetMassData();
  536. }
  537. inline bool b2Body::IsFixedRotation() const
  538. {
  539. return (m_flags & e_fixedRotationFlag) == e_fixedRotationFlag;
  540. }
  541. inline void b2Body::SetSleepingAllowed(bool flag)
  542. {
  543. if (flag)
  544. {
  545. m_flags |= e_autoSleepFlag;
  546. }
  547. else
  548. {
  549. m_flags &= ~e_autoSleepFlag;
  550. SetAwake(true);
  551. }
  552. }
  553. inline bool b2Body::IsSleepingAllowed() const
  554. {
  555. return (m_flags & e_autoSleepFlag) == e_autoSleepFlag;
  556. }
  557. inline b2Fixture* b2Body::GetFixtureList()
  558. {
  559. return m_fixtureList;
  560. }
  561. inline const b2Fixture* b2Body::GetFixtureList() const
  562. {
  563. return m_fixtureList;
  564. }
  565. inline b2JointEdge* b2Body::GetJointList()
  566. {
  567. return m_jointList;
  568. }
  569. inline const b2JointEdge* b2Body::GetJointList() const
  570. {
  571. return m_jointList;
  572. }
  573. inline b2ContactEdge* b2Body::GetContactList()
  574. {
  575. return m_contactList;
  576. }
  577. inline const b2ContactEdge* b2Body::GetContactList() const
  578. {
  579. return m_contactList;
  580. }
  581. inline b2Body* b2Body::GetNext()
  582. {
  583. return m_next;
  584. }
  585. inline const b2Body* b2Body::GetNext() const
  586. {
  587. return m_next;
  588. }
  589. inline void b2Body::SetUserData(void* data)
  590. {
  591. m_userData = data;
  592. }
  593. inline void* b2Body::GetUserData() const
  594. {
  595. return m_userData;
  596. }
  597. inline void b2Body::ApplyForce(const b2Vec2& force, const b2Vec2& point)
  598. {
  599. if (m_type != b2_dynamicBody)
  600. {
  601. return;
  602. }
  603. if (IsAwake() == false)
  604. {
  605. SetAwake(true);
  606. }
  607. m_force += force;
  608. m_torque += b2Cross(point - m_sweep.c, force);
  609. }
  610. inline void b2Body::ApplyForceToCenter(const b2Vec2& force)
  611. {
  612. if (m_type != b2_dynamicBody)
  613. {
  614. return;
  615. }
  616. if (IsAwake() == false)
  617. {
  618. SetAwake(true);
  619. }
  620. m_force += force;
  621. }
  622. inline void b2Body::ApplyTorque(float32 torque)
  623. {
  624. if (m_type != b2_dynamicBody)
  625. {
  626. return;
  627. }
  628. if (IsAwake() == false)
  629. {
  630. SetAwake(true);
  631. }
  632. m_torque += torque;
  633. }
  634. inline void b2Body::ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point)
  635. {
  636. if (m_type != b2_dynamicBody)
  637. {
  638. return;
  639. }
  640. if (IsAwake() == false)
  641. {
  642. SetAwake(true);
  643. }
  644. m_linearVelocity += m_invMass * impulse;
  645. m_angularVelocity += m_invI * b2Cross(point - m_sweep.c, impulse);
  646. }
  647. inline void b2Body::ApplyAngularImpulse(float32 impulse)
  648. {
  649. if (m_type != b2_dynamicBody)
  650. {
  651. return;
  652. }
  653. if (IsAwake() == false)
  654. {
  655. SetAwake(true);
  656. }
  657. m_angularVelocity += m_invI * impulse;
  658. }
  659. inline void b2Body::SynchronizeTransform()
  660. {
  661. m_xf.q.Set(m_sweep.a);
  662. m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
  663. }
  664. inline void b2Body::Advance(float32 alpha)
  665. {
  666. // Advance to the new safe time. This doesn't sync the broad-phase.
  667. m_sweep.Advance(alpha);
  668. m_sweep.c = m_sweep.c0;
  669. m_sweep.a = m_sweep.a0;
  670. m_xf.q.Set(m_sweep.a);
  671. m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
  672. }
  673. inline b2World* b2Body::GetWorld()
  674. {
  675. return m_world;
  676. }
  677. inline const b2World* b2Body::GetWorld() const
  678. {
  679. return m_world;
  680. }
  681. #endif