b2Body.h 23 KB

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