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.

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