b2Body.h 22 KB

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