b2_body.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_BODY_H
  19. #define B2_BODY_H
  20. #include "b2_api.h"
  21. #include "b2_math.h"
  22. #include "b2_shape.h"
  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. };
  41. /// A body definition holds all the data needed to construct a rigid body.
  42. /// You can safely re-use body definitions. Shapes are added to a body after construction.
  43. struct B2_API b2BodyDef
  44. {
  45. /// This constructor sets the body definition default values.
  46. b2BodyDef()
  47. {
  48. position.Set(0.0f, 0.0f);
  49. angle = 0.0f;
  50. linearVelocity.Set(0.0f, 0.0f);
  51. angularVelocity = 0.0f;
  52. linearDamping = 0.0f;
  53. angularDamping = 0.0f;
  54. allowSleep = true;
  55. awake = true;
  56. fixedRotation = false;
  57. bullet = false;
  58. type = b2_staticBody;
  59. enabled = true;
  60. gravityScale = 1.0f;
  61. }
  62. /// The body type: static, kinematic, or dynamic.
  63. /// Note: if a dynamic body would have zero mass, the mass is set to one.
  64. b2BodyType type;
  65. /// The world position of the body. Avoid creating bodies at the origin
  66. /// since this can lead to many overlapping shapes.
  67. b2Vec2 position;
  68. /// The world angle of the body in radians.
  69. float angle;
  70. /// The linear velocity of the body's origin in world co-ordinates.
  71. b2Vec2 linearVelocity;
  72. /// The angular velocity of the body.
  73. float angularVelocity;
  74. /// Linear damping is use to reduce the linear velocity. The damping parameter
  75. /// can be larger than 1.0f but the damping effect becomes sensitive to the
  76. /// time step when the damping parameter is large.
  77. /// Units are 1/time
  78. float linearDamping;
  79. /// Angular damping is use to reduce the angular velocity. The damping parameter
  80. /// can be larger than 1.0f but the damping effect becomes sensitive to the
  81. /// time step when the damping parameter is large.
  82. /// Units are 1/time
  83. float 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 enabled?
  97. bool enabled;
  98. /// Use this to store application specific body data.
  99. b2BodyUserData userData;
  100. /// Scale the gravity applied to this body.
  101. float gravityScale;
  102. };
  103. /// A rigid body. These are created via b2World::CreateBody.
  104. class B2_API 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, float 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. /// Manipulating a body's transform may cause non-physical behavior.
  133. /// Note: contacts are updated on the next call to b2World::Step.
  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, float 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. float 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. const b2Vec2& GetLinearVelocity() const;
  156. /// Set the angular velocity.
  157. /// @param omega the new angular velocity in radians/second.
  158. void SetAngularVelocity(float omega);
  159. /// Get the angular velocity.
  160. /// @return the angular velocity in radians/second.
  161. float 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. /// @param wake also wake up the body
  168. void ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake);
  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. /// @param wake also wake up the body
  172. void ApplyForceToCenter(const b2Vec2& force, bool wake);
  173. /// Apply a torque. This affects the angular velocity
  174. /// without affecting the linear velocity of the center of mass.
  175. /// @param torque about the z-axis (out of the screen), usually in N-m.
  176. /// @param wake also wake up the body
  177. void ApplyTorque(float torque, bool wake);
  178. /// Apply an impulse at a point. This immediately modifies the velocity.
  179. /// It also modifies the angular velocity if the point of application
  180. /// is not at the center of mass. This wakes up the body.
  181. /// @param impulse the world impulse vector, usually in N-seconds or kg-m/s.
  182. /// @param point the world position of the point of application.
  183. /// @param wake also wake up the body
  184. void ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake);
  185. /// Apply an impulse to the center of mass. This immediately modifies the velocity.
  186. /// @param impulse the world impulse vector, usually in N-seconds or kg-m/s.
  187. /// @param wake also wake up the body
  188. void ApplyLinearImpulseToCenter(const b2Vec2& impulse, bool wake);
  189. /// Apply an angular impulse.
  190. /// @param impulse the angular impulse in units of kg*m*m/s
  191. /// @param wake also wake up the body
  192. void ApplyAngularImpulse(float impulse, bool wake);
  193. /// Get the total mass of the body.
  194. /// @return the mass, usually in kilograms (kg).
  195. float GetMass() const;
  196. /// Get the rotational inertia of the body about the local origin.
  197. /// @return the rotational inertia, usually in kg-m^2.
  198. float GetInertia() const;
  199. /// Get the mass data of the body.
  200. /// @return a struct containing the mass, inertia and center of the body.
  201. b2MassData GetMassData() const;
  202. /// Set the mass properties to override the mass properties of the fixtures.
  203. /// Note that this changes the center of mass position.
  204. /// Note that creating or destroying fixtures can also alter the mass.
  205. /// This function has no effect if the body isn't dynamic.
  206. /// @param data the mass properties.
  207. void SetMassData(const b2MassData* data);
  208. /// This resets the mass properties to the sum of the mass properties of the fixtures.
  209. /// This normally does not need to be called unless you called SetMassData to override
  210. /// the mass and you later want to reset the mass.
  211. void ResetMassData();
  212. /// Get the world coordinates of a point given the local coordinates.
  213. /// @param localPoint a point on the body measured relative the the body's origin.
  214. /// @return the same point expressed in world coordinates.
  215. b2Vec2 GetWorldPoint(const b2Vec2& localPoint) const;
  216. /// Get the world coordinates of a vector given the local coordinates.
  217. /// @param localVector a vector fixed in the body.
  218. /// @return the same vector expressed in world coordinates.
  219. b2Vec2 GetWorldVector(const b2Vec2& localVector) const;
  220. /// Gets a local point relative to the body's origin given a world point.
  221. /// @param worldPoint a point in world coordinates.
  222. /// @return the corresponding local point relative to the body's origin.
  223. b2Vec2 GetLocalPoint(const b2Vec2& worldPoint) const;
  224. /// Gets a local vector given a world vector.
  225. /// @param worldVector a vector in world coordinates.
  226. /// @return the corresponding local vector.
  227. b2Vec2 GetLocalVector(const b2Vec2& worldVector) const;
  228. /// Get the world linear velocity of a world point attached to this body.
  229. /// @param worldPoint a point in world coordinates.
  230. /// @return the world velocity of a point.
  231. b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const;
  232. /// Get the world velocity of a local point.
  233. /// @param localPoint a point in local coordinates.
  234. /// @return the world velocity of a point.
  235. b2Vec2 GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const;
  236. /// Get the linear damping of the body.
  237. float GetLinearDamping() const;
  238. /// Set the linear damping of the body.
  239. void SetLinearDamping(float linearDamping);
  240. /// Get the angular damping of the body.
  241. float GetAngularDamping() const;
  242. /// Set the angular damping of the body.
  243. void SetAngularDamping(float angularDamping);
  244. /// Get the gravity scale of the body.
  245. float GetGravityScale() const;
  246. /// Set the gravity scale of the body.
  247. void SetGravityScale(float scale);
  248. /// Set the type of this body. This may alter the mass and velocity.
  249. void SetType(b2BodyType type);
  250. /// Get the type of this body.
  251. b2BodyType GetType() const;
  252. /// Should this body be treated like a bullet for continuous collision detection?
  253. void SetBullet(bool flag);
  254. /// Is this body treated like a bullet for continuous collision detection?
  255. bool IsBullet() const;
  256. /// You can disable sleeping on this body. If you disable sleeping, the
  257. /// body will be woken.
  258. void SetSleepingAllowed(bool flag);
  259. /// Is this body allowed to sleep
  260. bool IsSleepingAllowed() const;
  261. /// Set the sleep state of the body. A sleeping body has very
  262. /// low CPU cost.
  263. /// @param flag set to true to wake the body, false to put it to sleep.
  264. void SetAwake(bool flag);
  265. /// Get the sleeping state of this body.
  266. /// @return true if the body is awake.
  267. bool IsAwake() const;
  268. /// Allow a body to be disabled. A disabled body is not simulated and cannot
  269. /// be collided with or woken up.
  270. /// If you pass a flag of true, all fixtures will be added to the broad-phase.
  271. /// If you pass a flag of false, all fixtures will be removed from the
  272. /// broad-phase and all contacts will be destroyed.
  273. /// Fixtures and joints are otherwise unaffected. You may continue
  274. /// to create/destroy fixtures and joints on disabled bodies.
  275. /// Fixtures on a disabled body are implicitly disabled and will
  276. /// not participate in collisions, ray-casts, or queries.
  277. /// Joints connected to a disabled body are implicitly disabled.
  278. /// An diabled body is still owned by a b2World object and remains
  279. /// in the body list.
  280. void SetEnabled(bool flag);
  281. /// Get the active state of the body.
  282. bool IsEnabled() const;
  283. /// Set this body to have fixed rotation. This causes the mass
  284. /// to be reset.
  285. void SetFixedRotation(bool flag);
  286. /// Does this body have fixed rotation?
  287. bool IsFixedRotation() const;
  288. /// Get the list of all fixtures attached to this body.
  289. b2Fixture* GetFixtureList();
  290. const b2Fixture* GetFixtureList() const;
  291. /// Get the list of all joints attached to this body.
  292. b2JointEdge* GetJointList();
  293. const b2JointEdge* GetJointList() const;
  294. /// Get the list of all contacts attached to this body.
  295. /// @warning this list changes during the time step and you may
  296. /// miss some collisions if you don't use b2ContactListener.
  297. b2ContactEdge* GetContactList();
  298. const b2ContactEdge* GetContactList() const;
  299. /// Get the next body in the world's body list.
  300. b2Body* GetNext();
  301. const b2Body* GetNext() const;
  302. /// Get the user data pointer that was provided in the body definition.
  303. b2BodyUserData& GetUserData();
  304. const b2BodyUserData& GetUserData() const;
  305. /// Get the parent world of this body.
  306. b2World* GetWorld();
  307. const b2World* GetWorld() const;
  308. /// Dump this body to a file
  309. void Dump();
  310. private:
  311. friend class b2World;
  312. friend class b2Island;
  313. friend class b2ContactManager;
  314. friend class b2ContactSolver;
  315. friend class b2Contact;
  316. friend class b2DistanceJoint;
  317. friend class b2FrictionJoint;
  318. friend class b2GearJoint;
  319. friend class b2MotorJoint;
  320. friend class b2MouseJoint;
  321. friend class b2PrismaticJoint;
  322. friend class b2PulleyJoint;
  323. friend class b2RevoluteJoint;
  324. friend class b2WeldJoint;
  325. friend class b2WheelJoint;
  326. // m_flags
  327. enum
  328. {
  329. e_islandFlag = 0x0001,
  330. e_awakeFlag = 0x0002,
  331. e_autoSleepFlag = 0x0004,
  332. e_bulletFlag = 0x0008,
  333. e_fixedRotationFlag = 0x0010,
  334. e_enabledFlag = 0x0020,
  335. e_toiFlag = 0x0040
  336. };
  337. b2Body(const b2BodyDef* bd, b2World* world);
  338. ~b2Body();
  339. void SynchronizeFixtures();
  340. void SynchronizeTransform();
  341. // This is used to prevent connected bodies from colliding.
  342. // It may lie, depending on the collideConnected flag.
  343. bool ShouldCollide(const b2Body* other) const;
  344. void Advance(float t);
  345. b2BodyType m_type;
  346. uint16 m_flags;
  347. int32 m_islandIndex;
  348. b2Transform m_xf; // the body origin transform
  349. b2Sweep m_sweep; // the swept motion for CCD
  350. b2Vec2 m_linearVelocity;
  351. float m_angularVelocity;
  352. b2Vec2 m_force;
  353. float m_torque;
  354. b2World* m_world;
  355. b2Body* m_prev;
  356. b2Body* m_next;
  357. b2Fixture* m_fixtureList;
  358. int32 m_fixtureCount;
  359. b2JointEdge* m_jointList;
  360. b2ContactEdge* m_contactList;
  361. float m_mass, m_invMass;
  362. // Rotational inertia about the center of mass.
  363. float m_I, m_invI;
  364. float m_linearDamping;
  365. float m_angularDamping;
  366. float m_gravityScale;
  367. float m_sleepTime;
  368. b2BodyUserData m_userData;
  369. };
  370. inline b2BodyType b2Body::GetType() const
  371. {
  372. return m_type;
  373. }
  374. inline const b2Transform& b2Body::GetTransform() const
  375. {
  376. return m_xf;
  377. }
  378. inline const b2Vec2& b2Body::GetPosition() const
  379. {
  380. return m_xf.p;
  381. }
  382. inline float b2Body::GetAngle() const
  383. {
  384. return m_sweep.a;
  385. }
  386. inline const b2Vec2& b2Body::GetWorldCenter() const
  387. {
  388. return m_sweep.c;
  389. }
  390. inline const b2Vec2& b2Body::GetLocalCenter() const
  391. {
  392. return m_sweep.localCenter;
  393. }
  394. inline void b2Body::SetLinearVelocity(const b2Vec2& v)
  395. {
  396. if (m_type == b2_staticBody)
  397. {
  398. return;
  399. }
  400. if (b2Dot(v,v) > 0.0f)
  401. {
  402. SetAwake(true);
  403. }
  404. m_linearVelocity = v;
  405. }
  406. inline const b2Vec2& b2Body::GetLinearVelocity() const
  407. {
  408. return m_linearVelocity;
  409. }
  410. inline void b2Body::SetAngularVelocity(float w)
  411. {
  412. if (m_type == b2_staticBody)
  413. {
  414. return;
  415. }
  416. if (w * w > 0.0f)
  417. {
  418. SetAwake(true);
  419. }
  420. m_angularVelocity = w;
  421. }
  422. inline float b2Body::GetAngularVelocity() const
  423. {
  424. return m_angularVelocity;
  425. }
  426. inline float b2Body::GetMass() const
  427. {
  428. return m_mass;
  429. }
  430. inline float b2Body::GetInertia() const
  431. {
  432. return m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
  433. }
  434. inline b2MassData b2Body::GetMassData() const
  435. {
  436. b2MassData data;
  437. data.mass = m_mass;
  438. data.I = m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
  439. data.center = m_sweep.localCenter;
  440. return data;
  441. }
  442. inline b2Vec2 b2Body::GetWorldPoint(const b2Vec2& localPoint) const
  443. {
  444. return b2Mul(m_xf, localPoint);
  445. }
  446. inline b2Vec2 b2Body::GetWorldVector(const b2Vec2& localVector) const
  447. {
  448. return b2Mul(m_xf.q, localVector);
  449. }
  450. inline b2Vec2 b2Body::GetLocalPoint(const b2Vec2& worldPoint) const
  451. {
  452. return b2MulT(m_xf, worldPoint);
  453. }
  454. inline b2Vec2 b2Body::GetLocalVector(const b2Vec2& worldVector) const
  455. {
  456. return b2MulT(m_xf.q, worldVector);
  457. }
  458. inline b2Vec2 b2Body::GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const
  459. {
  460. return m_linearVelocity + b2Cross(m_angularVelocity, worldPoint - m_sweep.c);
  461. }
  462. inline b2Vec2 b2Body::GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const
  463. {
  464. return GetLinearVelocityFromWorldPoint(GetWorldPoint(localPoint));
  465. }
  466. inline float b2Body::GetLinearDamping() const
  467. {
  468. return m_linearDamping;
  469. }
  470. inline void b2Body::SetLinearDamping(float linearDamping)
  471. {
  472. m_linearDamping = linearDamping;
  473. }
  474. inline float b2Body::GetAngularDamping() const
  475. {
  476. return m_angularDamping;
  477. }
  478. inline void b2Body::SetAngularDamping(float angularDamping)
  479. {
  480. m_angularDamping = angularDamping;
  481. }
  482. inline float b2Body::GetGravityScale() const
  483. {
  484. return m_gravityScale;
  485. }
  486. inline void b2Body::SetGravityScale(float scale)
  487. {
  488. m_gravityScale = scale;
  489. }
  490. inline void b2Body::SetBullet(bool flag)
  491. {
  492. if (flag)
  493. {
  494. m_flags |= e_bulletFlag;
  495. }
  496. else
  497. {
  498. m_flags &= ~e_bulletFlag;
  499. }
  500. }
  501. inline bool b2Body::IsBullet() const
  502. {
  503. return (m_flags & e_bulletFlag) == e_bulletFlag;
  504. }
  505. inline void b2Body::SetAwake(bool flag)
  506. {
  507. if (m_type == b2_staticBody)
  508. {
  509. return;
  510. }
  511. if (flag)
  512. {
  513. m_flags |= e_awakeFlag;
  514. m_sleepTime = 0.0f;
  515. }
  516. else
  517. {
  518. m_flags &= ~e_awakeFlag;
  519. m_sleepTime = 0.0f;
  520. m_linearVelocity.SetZero();
  521. m_angularVelocity = 0.0f;
  522. m_force.SetZero();
  523. m_torque = 0.0f;
  524. }
  525. }
  526. inline bool b2Body::IsAwake() const
  527. {
  528. return (m_flags & e_awakeFlag) == e_awakeFlag;
  529. }
  530. inline bool b2Body::IsEnabled() const
  531. {
  532. return (m_flags & e_enabledFlag) == e_enabledFlag;
  533. }
  534. inline bool b2Body::IsFixedRotation() const
  535. {
  536. return (m_flags & e_fixedRotationFlag) == e_fixedRotationFlag;
  537. }
  538. inline void b2Body::SetSleepingAllowed(bool flag)
  539. {
  540. if (flag)
  541. {
  542. m_flags |= e_autoSleepFlag;
  543. }
  544. else
  545. {
  546. m_flags &= ~e_autoSleepFlag;
  547. SetAwake(true);
  548. }
  549. }
  550. inline bool b2Body::IsSleepingAllowed() const
  551. {
  552. return (m_flags & e_autoSleepFlag) == e_autoSleepFlag;
  553. }
  554. inline b2Fixture* b2Body::GetFixtureList()
  555. {
  556. return m_fixtureList;
  557. }
  558. inline const b2Fixture* b2Body::GetFixtureList() const
  559. {
  560. return m_fixtureList;
  561. }
  562. inline b2JointEdge* b2Body::GetJointList()
  563. {
  564. return m_jointList;
  565. }
  566. inline const b2JointEdge* b2Body::GetJointList() const
  567. {
  568. return m_jointList;
  569. }
  570. inline b2ContactEdge* b2Body::GetContactList()
  571. {
  572. return m_contactList;
  573. }
  574. inline const b2ContactEdge* b2Body::GetContactList() const
  575. {
  576. return m_contactList;
  577. }
  578. inline b2Body* b2Body::GetNext()
  579. {
  580. return m_next;
  581. }
  582. inline const b2Body* b2Body::GetNext() const
  583. {
  584. return m_next;
  585. }
  586. inline b2BodyUserData& b2Body::GetUserData()
  587. {
  588. return m_userData;
  589. }
  590. inline const b2BodyUserData& b2Body::GetUserData() const
  591. {
  592. return m_userData;
  593. }
  594. inline void b2Body::ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake)
  595. {
  596. if (m_type != b2_dynamicBody)
  597. {
  598. return;
  599. }
  600. if (wake && (m_flags & e_awakeFlag) == 0)
  601. {
  602. SetAwake(true);
  603. }
  604. // Don't accumulate a force if the body is sleeping.
  605. if (m_flags & e_awakeFlag)
  606. {
  607. m_force += force;
  608. m_torque += b2Cross(point - m_sweep.c, force);
  609. }
  610. }
  611. inline void b2Body::ApplyForceToCenter(const b2Vec2& force, bool wake)
  612. {
  613. if (m_type != b2_dynamicBody)
  614. {
  615. return;
  616. }
  617. if (wake && (m_flags & e_awakeFlag) == 0)
  618. {
  619. SetAwake(true);
  620. }
  621. // Don't accumulate a force if the body is sleeping
  622. if (m_flags & e_awakeFlag)
  623. {
  624. m_force += force;
  625. }
  626. }
  627. inline void b2Body::ApplyTorque(float torque, bool wake)
  628. {
  629. if (m_type != b2_dynamicBody)
  630. {
  631. return;
  632. }
  633. if (wake && (m_flags & e_awakeFlag) == 0)
  634. {
  635. SetAwake(true);
  636. }
  637. // Don't accumulate a force if the body is sleeping
  638. if (m_flags & e_awakeFlag)
  639. {
  640. m_torque += torque;
  641. }
  642. }
  643. inline void b2Body::ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake)
  644. {
  645. if (m_type != b2_dynamicBody)
  646. {
  647. return;
  648. }
  649. if (wake && (m_flags & e_awakeFlag) == 0)
  650. {
  651. SetAwake(true);
  652. }
  653. // Don't accumulate velocity if the body is sleeping
  654. if (m_flags & e_awakeFlag)
  655. {
  656. m_linearVelocity += m_invMass * impulse;
  657. m_angularVelocity += m_invI * b2Cross(point - m_sweep.c, impulse);
  658. }
  659. }
  660. inline void b2Body::ApplyLinearImpulseToCenter(const b2Vec2& impulse, bool wake)
  661. {
  662. if (m_type != b2_dynamicBody)
  663. {
  664. return;
  665. }
  666. if (wake && (m_flags & e_awakeFlag) == 0)
  667. {
  668. SetAwake(true);
  669. }
  670. // Don't accumulate velocity if the body is sleeping
  671. if (m_flags & e_awakeFlag)
  672. {
  673. m_linearVelocity += m_invMass * impulse;
  674. }
  675. }
  676. inline void b2Body::ApplyAngularImpulse(float impulse, bool wake)
  677. {
  678. if (m_type != b2_dynamicBody)
  679. {
  680. return;
  681. }
  682. if (wake && (m_flags & e_awakeFlag) == 0)
  683. {
  684. SetAwake(true);
  685. }
  686. // Don't accumulate velocity if the body is sleeping
  687. if (m_flags & e_awakeFlag)
  688. {
  689. m_angularVelocity += m_invI * impulse;
  690. }
  691. }
  692. inline void b2Body::SynchronizeTransform()
  693. {
  694. m_xf.q.Set(m_sweep.a);
  695. m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
  696. }
  697. inline void b2Body::Advance(float alpha)
  698. {
  699. // Advance to the new safe time. This doesn't sync the broad-phase.
  700. m_sweep.Advance(alpha);
  701. m_sweep.c = m_sweep.c0;
  702. m_sweep.a = m_sweep.a0;
  703. m_xf.q.Set(m_sweep.a);
  704. m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
  705. }
  706. inline b2World* b2Body::GetWorld()
  707. {
  708. return m_world;
  709. }
  710. inline const b2World* b2Body::GetWorld() const
  711. {
  712. return m_world;
  713. }
  714. #endif