b2Body.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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 <Box2D/Common/b2Math.h>
  21. #include <Box2D/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. /// Manipulating a body's transform may cause non-physical behavior.
  134. /// Note: contacts are updated on the next call to b2World::Step.
  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. const 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. /// @param wake also wake up the body
  169. void ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake);
  170. /// Apply a force to the center of mass. This wakes up the body.
  171. /// @param force the world force vector, usually in Newtons (N).
  172. /// @param wake also wake up the body
  173. void ApplyForceToCenter(const b2Vec2& force, bool wake);
  174. /// Apply a torque. This affects the angular velocity
  175. /// without affecting the linear velocity of the center of mass.
  176. /// This wakes up the body.
  177. /// @param torque about the z-axis (out of the screen), usually in N-m.
  178. /// @param wake also wake up the body
  179. void ApplyTorque(float32 torque, bool wake);
  180. /// Apply an impulse at a point. This immediately modifies the velocity.
  181. /// It also modifies the angular velocity if the point of application
  182. /// is not at the center of mass. This wakes up the body.
  183. /// @param impulse the world impulse vector, usually in N-seconds or kg-m/s.
  184. /// @param point the world position of the point of application.
  185. /// @param wake also wake up the body
  186. void ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake);
  187. /// Apply an angular impulse.
  188. /// @param impulse the angular impulse in units of kg*m*m/s
  189. /// @param wake also wake up the body
  190. void ApplyAngularImpulse(float32 impulse, bool wake);
  191. /// Get the total mass of the body.
  192. /// @return the mass, usually in kilograms (kg).
  193. float32 GetMass() const;
  194. /// Get the rotational inertia of the body about the local origin.
  195. /// @return the rotational inertia, usually in kg-m^2.
  196. float32 GetInertia() const;
  197. /// Get the mass data of the body.
  198. /// @return a struct containing the mass, inertia and center of the body.
  199. void GetMassData(b2MassData* data) const;
  200. /// Set the mass properties to override the mass properties of the fixtures.
  201. /// Note that this changes the center of mass position.
  202. /// Note that creating or destroying fixtures can also alter the mass.
  203. /// This function has no effect if the body isn't dynamic.
  204. /// @param massData the mass properties.
  205. void SetMassData(const b2MassData* data);
  206. /// This resets the mass properties to the sum of the mass properties of the fixtures.
  207. /// This normally does not need to be called unless you called SetMassData to override
  208. /// the mass and you later want to reset the mass.
  209. void ResetMassData();
  210. /// Get the world coordinates of a point given the local coordinates.
  211. /// @param localPoint a point on the body measured relative the the body's origin.
  212. /// @return the same point expressed in world coordinates.
  213. b2Vec2 GetWorldPoint(const b2Vec2& localPoint) const;
  214. /// Get the world coordinates of a vector given the local coordinates.
  215. /// @param localVector a vector fixed in the body.
  216. /// @return the same vector expressed in world coordinates.
  217. b2Vec2 GetWorldVector(const b2Vec2& localVector) const;
  218. /// Gets a local point relative to the body's origin given a world point.
  219. /// @param a point in world coordinates.
  220. /// @return the corresponding local point relative to the body's origin.
  221. b2Vec2 GetLocalPoint(const b2Vec2& worldPoint) const;
  222. /// Gets a local vector given a world vector.
  223. /// @param a vector in world coordinates.
  224. /// @return the corresponding local vector.
  225. b2Vec2 GetLocalVector(const b2Vec2& worldVector) const;
  226. /// Get the world linear velocity of a world point attached to this body.
  227. /// @param a point in world coordinates.
  228. /// @return the world velocity of a point.
  229. b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const;
  230. /// Get the world velocity of a local point.
  231. /// @param a point in local coordinates.
  232. /// @return the world velocity of a point.
  233. b2Vec2 GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const;
  234. /// Get the linear damping of the body.
  235. float32 GetLinearDamping() const;
  236. /// Set the linear damping of the body.
  237. void SetLinearDamping(float32 linearDamping);
  238. /// Get the angular damping of the body.
  239. float32 GetAngularDamping() const;
  240. /// Set the angular damping of the body.
  241. void SetAngularDamping(float32 angularDamping);
  242. /// Get the gravity scale of the body.
  243. float32 GetGravityScale() const;
  244. /// Set the gravity scale of the body.
  245. void SetGravityScale(float32 scale);
  246. /// Set the type of this body. This may alter the mass and velocity.
  247. void SetType(b2BodyType type);
  248. /// Get the type of this body.
  249. b2BodyType GetType() const;
  250. /// Should this body be treated like a bullet for continuous collision detection?
  251. void SetBullet(bool flag);
  252. /// Is this body treated like a bullet for continuous collision detection?
  253. bool IsBullet() const;
  254. /// You can disable sleeping on this body. If you disable sleeping, the
  255. /// body will be woken.
  256. void SetSleepingAllowed(bool flag);
  257. /// Is this body allowed to sleep
  258. bool IsSleepingAllowed() const;
  259. /// Set the sleep state of the body. A sleeping body has very
  260. /// low CPU cost.
  261. /// @param flag set to true to wake the body, false to put it to sleep.
  262. void SetAwake(bool flag);
  263. /// Get the sleeping state of this body.
  264. /// @return true if the body is awake.
  265. bool IsAwake() const;
  266. /// Set the active state of the body. An inactive body is not
  267. /// simulated and cannot be collided with or woken up.
  268. /// If you pass a flag of true, all fixtures will be added to the
  269. /// broad-phase.
  270. /// If you pass a flag of false, all fixtures will be removed from
  271. /// the broad-phase and all contacts will be destroyed.
  272. /// Fixtures and joints are otherwise unaffected. You may continue
  273. /// to create/destroy fixtures and joints on inactive bodies.
  274. /// Fixtures on an inactive body are implicitly inactive and will
  275. /// not participate in collisions, ray-casts, or queries.
  276. /// Joints connected to an inactive body are implicitly inactive.
  277. /// An inactive body is still owned by a b2World object and remains
  278. /// in the body list.
  279. void SetActive(bool flag);
  280. /// Get the active state of the body.
  281. bool IsActive() const;
  282. /// Set this body to have fixed rotation. This causes the mass
  283. /// to be reset.
  284. void SetFixedRotation(bool flag);
  285. /// Does this body have fixed rotation?
  286. bool IsFixedRotation() const;
  287. /// Get the list of all fixtures attached to this body.
  288. b2Fixture* GetFixtureList();
  289. const b2Fixture* GetFixtureList() const;
  290. /// Get the list of all joints attached to this body.
  291. b2JointEdge* GetJointList();
  292. const b2JointEdge* GetJointList() const;
  293. /// Get the list of all contacts attached to this body.
  294. /// @warning this list changes during the time step and you may
  295. /// miss some collisions if you don't use b2ContactListener.
  296. b2ContactEdge* GetContactList();
  297. const b2ContactEdge* GetContactList() const;
  298. /// Get the next body in the world's body list.
  299. b2Body* GetNext();
  300. const b2Body* GetNext() const;
  301. /// Get the user data pointer that was provided in the body definition.
  302. void* GetUserData() const;
  303. /// Set the user data. Use this to store your application specific data.
  304. void SetUserData(void* data);
  305. /// Get the parent world of this body.
  306. b2World* GetWorld();
  307. const b2World* GetWorld() const;
  308. /// Dump this body to a log 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 b2RopeJoint;
  325. friend class b2WeldJoint;
  326. friend class b2WheelJoint;
  327. // m_flags
  328. enum
  329. {
  330. e_islandFlag = 0x0001,
  331. e_awakeFlag = 0x0002,
  332. e_autoSleepFlag = 0x0004,
  333. e_bulletFlag = 0x0008,
  334. e_fixedRotationFlag = 0x0010,
  335. e_activeFlag = 0x0020,
  336. e_toiFlag = 0x0040
  337. };
  338. b2Body(const b2BodyDef* bd, b2World* world);
  339. ~b2Body();
  340. void SynchronizeFixtures();
  341. void SynchronizeTransform();
  342. // This is used to prevent connected bodies from colliding.
  343. // It may lie, depending on the collideConnected flag.
  344. bool ShouldCollide(const b2Body* other) const;
  345. void Advance(float32 t);
  346. b2BodyType m_type;
  347. uint16 m_flags;
  348. int32 m_islandIndex;
  349. b2Transform m_xf; // the body origin transform
  350. b2Sweep m_sweep; // the swept motion for CCD
  351. b2Vec2 m_linearVelocity;
  352. float32 m_angularVelocity;
  353. b2Vec2 m_force;
  354. float32 m_torque;
  355. b2World* m_world;
  356. b2Body* m_prev;
  357. b2Body* m_next;
  358. b2Fixture* m_fixtureList;
  359. int32 m_fixtureCount;
  360. b2JointEdge* m_jointList;
  361. b2ContactEdge* m_contactList;
  362. float32 m_mass, m_invMass;
  363. // Rotational inertia about the center of mass.
  364. float32 m_I, m_invI;
  365. float32 m_linearDamping;
  366. float32 m_angularDamping;
  367. float32 m_gravityScale;
  368. float32 m_sleepTime;
  369. void* m_userData;
  370. };
  371. inline b2BodyType b2Body::GetType() const
  372. {
  373. return m_type;
  374. }
  375. inline const b2Transform& b2Body::GetTransform() const
  376. {
  377. return m_xf;
  378. }
  379. inline const b2Vec2& b2Body::GetPosition() const
  380. {
  381. return m_xf.p;
  382. }
  383. inline float32 b2Body::GetAngle() const
  384. {
  385. return m_sweep.a;
  386. }
  387. inline const b2Vec2& b2Body::GetWorldCenter() const
  388. {
  389. return m_sweep.c;
  390. }
  391. inline const b2Vec2& b2Body::GetLocalCenter() const
  392. {
  393. return m_sweep.localCenter;
  394. }
  395. inline void b2Body::SetLinearVelocity(const b2Vec2& v)
  396. {
  397. if (m_type == b2_staticBody)
  398. {
  399. return;
  400. }
  401. if (b2Dot(v,v) > 0.0f)
  402. {
  403. SetAwake(true);
  404. }
  405. m_linearVelocity = v;
  406. }
  407. inline const b2Vec2& b2Body::GetLinearVelocity() const
  408. {
  409. return m_linearVelocity;
  410. }
  411. inline void b2Body::SetAngularVelocity(float32 w)
  412. {
  413. if (m_type == b2_staticBody)
  414. {
  415. return;
  416. }
  417. if (w * w > 0.0f)
  418. {
  419. SetAwake(true);
  420. }
  421. m_angularVelocity = w;
  422. }
  423. inline float32 b2Body::GetAngularVelocity() const
  424. {
  425. return m_angularVelocity;
  426. }
  427. inline float32 b2Body::GetMass() const
  428. {
  429. return m_mass;
  430. }
  431. inline float32 b2Body::GetInertia() const
  432. {
  433. return m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
  434. }
  435. inline void b2Body::GetMassData(b2MassData* data) const
  436. {
  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. }
  441. inline b2Vec2 b2Body::GetWorldPoint(const b2Vec2& localPoint) const
  442. {
  443. return b2Mul(m_xf, localPoint);
  444. }
  445. inline b2Vec2 b2Body::GetWorldVector(const b2Vec2& localVector) const
  446. {
  447. return b2Mul(m_xf.q, localVector);
  448. }
  449. inline b2Vec2 b2Body::GetLocalPoint(const b2Vec2& worldPoint) const
  450. {
  451. return b2MulT(m_xf, worldPoint);
  452. }
  453. inline b2Vec2 b2Body::GetLocalVector(const b2Vec2& worldVector) const
  454. {
  455. return b2MulT(m_xf.q, worldVector);
  456. }
  457. inline b2Vec2 b2Body::GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const
  458. {
  459. return m_linearVelocity + b2Cross(m_angularVelocity, worldPoint - m_sweep.c);
  460. }
  461. inline b2Vec2 b2Body::GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const
  462. {
  463. return GetLinearVelocityFromWorldPoint(GetWorldPoint(localPoint));
  464. }
  465. inline float32 b2Body::GetLinearDamping() const
  466. {
  467. return m_linearDamping;
  468. }
  469. inline void b2Body::SetLinearDamping(float32 linearDamping)
  470. {
  471. m_linearDamping = linearDamping;
  472. }
  473. inline float32 b2Body::GetAngularDamping() const
  474. {
  475. return m_angularDamping;
  476. }
  477. inline void b2Body::SetAngularDamping(float32 angularDamping)
  478. {
  479. m_angularDamping = angularDamping;
  480. }
  481. inline float32 b2Body::GetGravityScale() const
  482. {
  483. return m_gravityScale;
  484. }
  485. inline void b2Body::SetGravityScale(float32 scale)
  486. {
  487. m_gravityScale = scale;
  488. }
  489. inline void b2Body::SetBullet(bool flag)
  490. {
  491. if (flag)
  492. {
  493. m_flags |= e_bulletFlag;
  494. }
  495. else
  496. {
  497. m_flags &= ~e_bulletFlag;
  498. }
  499. }
  500. inline bool b2Body::IsBullet() const
  501. {
  502. return (m_flags & e_bulletFlag) == e_bulletFlag;
  503. }
  504. inline void b2Body::SetAwake(bool flag)
  505. {
  506. if (flag)
  507. {
  508. if ((m_flags & e_awakeFlag) == 0)
  509. {
  510. m_flags |= e_awakeFlag;
  511. m_sleepTime = 0.0f;
  512. }
  513. }
  514. else
  515. {
  516. m_flags &= ~e_awakeFlag;
  517. m_sleepTime = 0.0f;
  518. m_linearVelocity.SetZero();
  519. m_angularVelocity = 0.0f;
  520. m_force.SetZero();
  521. m_torque = 0.0f;
  522. }
  523. }
  524. inline bool b2Body::IsAwake() const
  525. {
  526. return (m_flags & e_awakeFlag) == e_awakeFlag;
  527. }
  528. inline bool b2Body::IsActive() const
  529. {
  530. return (m_flags & e_activeFlag) == e_activeFlag;
  531. }
  532. inline bool b2Body::IsFixedRotation() const
  533. {
  534. return (m_flags & e_fixedRotationFlag) == e_fixedRotationFlag;
  535. }
  536. inline void b2Body::SetSleepingAllowed(bool flag)
  537. {
  538. if (flag)
  539. {
  540. m_flags |= e_autoSleepFlag;
  541. }
  542. else
  543. {
  544. m_flags &= ~e_autoSleepFlag;
  545. SetAwake(true);
  546. }
  547. }
  548. inline bool b2Body::IsSleepingAllowed() const
  549. {
  550. return (m_flags & e_autoSleepFlag) == e_autoSleepFlag;
  551. }
  552. inline b2Fixture* b2Body::GetFixtureList()
  553. {
  554. return m_fixtureList;
  555. }
  556. inline const b2Fixture* b2Body::GetFixtureList() const
  557. {
  558. return m_fixtureList;
  559. }
  560. inline b2JointEdge* b2Body::GetJointList()
  561. {
  562. return m_jointList;
  563. }
  564. inline const b2JointEdge* b2Body::GetJointList() const
  565. {
  566. return m_jointList;
  567. }
  568. inline b2ContactEdge* b2Body::GetContactList()
  569. {
  570. return m_contactList;
  571. }
  572. inline const b2ContactEdge* b2Body::GetContactList() const
  573. {
  574. return m_contactList;
  575. }
  576. inline b2Body* b2Body::GetNext()
  577. {
  578. return m_next;
  579. }
  580. inline const b2Body* b2Body::GetNext() const
  581. {
  582. return m_next;
  583. }
  584. inline void b2Body::SetUserData(void* data)
  585. {
  586. m_userData = data;
  587. }
  588. inline void* b2Body::GetUserData() const
  589. {
  590. return m_userData;
  591. }
  592. inline void b2Body::ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake)
  593. {
  594. if (m_type != b2_dynamicBody)
  595. {
  596. return;
  597. }
  598. if (wake && (m_flags & e_awakeFlag) == 0)
  599. {
  600. SetAwake(true);
  601. }
  602. // Don't accumulate a force if the body is sleeping.
  603. if (m_flags & e_awakeFlag)
  604. {
  605. m_force += force;
  606. m_torque += b2Cross(point - m_sweep.c, force);
  607. }
  608. }
  609. inline void b2Body::ApplyForceToCenter(const b2Vec2& force, bool wake)
  610. {
  611. if (m_type != b2_dynamicBody)
  612. {
  613. return;
  614. }
  615. if (wake && (m_flags & e_awakeFlag) == 0)
  616. {
  617. SetAwake(true);
  618. }
  619. // Don't accumulate a force if the body is sleeping
  620. if (m_flags & e_awakeFlag)
  621. {
  622. m_force += force;
  623. }
  624. }
  625. inline void b2Body::ApplyTorque(float32 torque, bool wake)
  626. {
  627. if (m_type != b2_dynamicBody)
  628. {
  629. return;
  630. }
  631. if (wake && (m_flags & e_awakeFlag) == 0)
  632. {
  633. SetAwake(true);
  634. }
  635. // Don't accumulate a force if the body is sleeping
  636. if (m_flags & e_awakeFlag)
  637. {
  638. m_torque += torque;
  639. }
  640. }
  641. inline void b2Body::ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake)
  642. {
  643. if (m_type != b2_dynamicBody)
  644. {
  645. return;
  646. }
  647. if (wake && (m_flags & e_awakeFlag) == 0)
  648. {
  649. SetAwake(true);
  650. }
  651. // Don't accumulate velocity if the body is sleeping
  652. if (m_flags & e_awakeFlag)
  653. {
  654. m_linearVelocity += m_invMass * impulse;
  655. m_angularVelocity += m_invI * b2Cross(point - m_sweep.c, impulse);
  656. }
  657. }
  658. inline void b2Body::ApplyAngularImpulse(float32 impulse, bool wake)
  659. {
  660. if (m_type != b2_dynamicBody)
  661. {
  662. return;
  663. }
  664. if (wake && (m_flags & e_awakeFlag) == 0)
  665. {
  666. SetAwake(true);
  667. }
  668. // Don't accumulate velocity if the body is sleeping
  669. if (m_flags & e_awakeFlag)
  670. {
  671. m_angularVelocity += m_invI * impulse;
  672. }
  673. }
  674. inline void b2Body::SynchronizeTransform()
  675. {
  676. m_xf.q.Set(m_sweep.a);
  677. m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
  678. }
  679. inline void b2Body::Advance(float32 alpha)
  680. {
  681. // Advance to the new safe time. This doesn't sync the broad-phase.
  682. m_sweep.Advance(alpha);
  683. m_sweep.c = m_sweep.c0;
  684. m_sweep.a = m_sweep.a0;
  685. m_xf.q.Set(m_sweep.a);
  686. m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
  687. }
  688. inline b2World* b2Body::GetWorld()
  689. {
  690. return m_world;
  691. }
  692. inline const b2World* b2Body::GetWorld() const
  693. {
  694. return m_world;
  695. }
  696. #endif