b2Body.h
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 #ifndef B2_BODY_H
00020 #define B2_BODY_H
00021 
00022 #include <Box2D/Common/b2Math.h>
00023 #include <Box2D/Collision/Shapes/b2Shape.h>
00024 #include <memory>
00025 
00026 class b2Fixture;
00027 class b2Joint;
00028 class b2Contact;
00029 class b2Controller;
00030 class b2World;
00031 struct b2FixtureDef;
00032 struct b2JointEdge;
00033 struct b2ContactEdge;
00034 
00039 enum b2BodyType
00040 {
00041         b2_staticBody = 0,
00042         b2_kinematicBody,
00043         b2_dynamicBody
00044 
00045         // TODO_ERIN
00046         //b2_bulletBody,
00047 };
00048 
00051 struct b2BodyDef
00052 {
00054         b2BodyDef()
00055         {
00056                 userData = NULL;
00057                 position.Set(0.0f, 0.0f);
00058                 angle = 0.0f;
00059                 linearVelocity.Set(0.0f, 0.0f);
00060                 angularVelocity = 0.0f;
00061                 linearDamping = 0.0f;
00062                 angularDamping = 0.0f;
00063                 allowSleep = true;
00064                 awake = true;
00065                 fixedRotation = false;
00066                 bullet = false;
00067                 type = b2_staticBody;
00068                 active = true;
00069                 gravityScale = 1.0f;
00070         }
00071 
00074         b2BodyType type;
00075 
00078         b2Vec2 position;
00079 
00081         float32 angle;
00082 
00084         b2Vec2 linearVelocity;
00085 
00087         float32 angularVelocity;
00088 
00092         float32 linearDamping;
00093 
00097         float32 angularDamping;
00098 
00101         bool allowSleep;
00102 
00104         bool awake;
00105 
00107         bool fixedRotation;
00108 
00113         bool bullet;
00114 
00116         bool active;
00117 
00119         void* userData;
00120 
00122         float32 gravityScale;
00123 };
00124 
00126 class b2Body
00127 {
00128 public:
00136         b2Fixture* CreateFixture(const b2FixtureDef* def);
00137 
00145         b2Fixture* CreateFixture(const b2Shape* shape, float32 density);
00146 
00154         void DestroyFixture(b2Fixture* fixture);
00155 
00161         void SetTransform(const b2Vec2& position, float32 angle);
00162 
00165         const b2Transform& GetTransform() const;
00166 
00169         const b2Vec2& GetPosition() const;
00170 
00173         float32 GetAngle() const;
00174 
00176         const b2Vec2& GetWorldCenter() const;
00177 
00179         const b2Vec2& GetLocalCenter() const;
00180 
00183         void SetLinearVelocity(const b2Vec2& v);
00184 
00187         const b2Vec2& GetLinearVelocity() const;
00188 
00191         void SetAngularVelocity(float32 omega);
00192 
00195         float32 GetAngularVelocity() const;
00196 
00203         void ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake);
00204 
00208         void ApplyForceToCenter(const b2Vec2& force, bool wake);
00209 
00215         void ApplyTorque(float32 torque, bool wake);
00216 
00223         void ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake);
00224 
00228         void ApplyAngularImpulse(float32 impulse, bool wake);
00229 
00232         float32 GetMass() const;
00233 
00236         float32 GetInertia() const;
00237 
00240         void GetMassData(b2MassData* data) const;
00241 
00247         void SetMassData(const b2MassData* data);
00248 
00252         void ResetMassData();
00253 
00257         b2Vec2 GetWorldPoint(const b2Vec2& localPoint) const;
00258 
00262         b2Vec2 GetWorldVector(const b2Vec2& localVector) const;
00263 
00267         b2Vec2 GetLocalPoint(const b2Vec2& worldPoint) const;
00268 
00272         b2Vec2 GetLocalVector(const b2Vec2& worldVector) const;
00273 
00277         b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const;
00278 
00282         b2Vec2 GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const;
00283 
00285         float32 GetLinearDamping() const;
00286 
00288         void SetLinearDamping(float32 linearDamping);
00289 
00291         float32 GetAngularDamping() const;
00292 
00294         void SetAngularDamping(float32 angularDamping);
00295 
00297         float32 GetGravityScale() const;
00298 
00300         void SetGravityScale(float32 scale);
00301 
00303         void SetType(b2BodyType type);
00304 
00306         b2BodyType GetType() const;
00307 
00309         void SetBullet(bool flag);
00310 
00312         bool IsBullet() const;
00313 
00316         void SetSleepingAllowed(bool flag);
00317 
00319         bool IsSleepingAllowed() const;
00320 
00324         void SetAwake(bool flag);
00325 
00328         bool IsAwake() const;
00329 
00343         void SetActive(bool flag);
00344 
00346         bool IsActive() const;
00347 
00350         void SetFixedRotation(bool flag);
00351 
00353         bool IsFixedRotation() const;
00354 
00356         b2Fixture* GetFixtureList();
00357         const b2Fixture* GetFixtureList() const;
00358 
00360         b2JointEdge* GetJointList();
00361         const b2JointEdge* GetJointList() const;
00362 
00366         b2ContactEdge* GetContactList();
00367         const b2ContactEdge* GetContactList() const;
00368 
00370         b2Body* GetNext();
00371         const b2Body* GetNext() const;
00372 
00374         void* GetUserData() const;
00375 
00377         void SetUserData(void* data);
00378 
00380         b2World* GetWorld();
00381         const b2World* GetWorld() const;
00382 
00384         void Dump();
00385 
00386 private:
00387 
00388         friend class b2World;
00389         friend class b2Island;
00390         friend class b2ContactManager;
00391         friend class b2ContactSolver;
00392         friend class b2Contact;
00393         
00394         friend class b2DistanceJoint;
00395         friend class b2FrictionJoint;
00396         friend class b2GearJoint;
00397         friend class b2MotorJoint;
00398         friend class b2MouseJoint;
00399         friend class b2PrismaticJoint;
00400         friend class b2PulleyJoint;
00401         friend class b2RevoluteJoint;
00402         friend class b2RopeJoint;
00403         friend class b2WeldJoint;
00404         friend class b2WheelJoint;
00405 
00406         // m_flags
00407         enum
00408         {
00409                 e_islandFlag            = 0x0001,
00410                 e_awakeFlag                     = 0x0002,
00411                 e_autoSleepFlag         = 0x0004,
00412                 e_bulletFlag            = 0x0008,
00413                 e_fixedRotationFlag     = 0x0010,
00414                 e_activeFlag            = 0x0020,
00415                 e_toiFlag                       = 0x0040
00416         };
00417 
00418         b2Body(const b2BodyDef* bd, b2World* world);
00419         ~b2Body();
00420 
00421         void SynchronizeFixtures();
00422         void SynchronizeTransform();
00423 
00424         // This is used to prevent connected bodies from colliding.
00425         // It may lie, depending on the collideConnected flag.
00426         bool ShouldCollide(const b2Body* other) const;
00427 
00428         void Advance(float32 t);
00429 
00430         b2BodyType m_type;
00431 
00432         uint16 m_flags;
00433 
00434         int32 m_islandIndex;
00435 
00436         b2Transform m_xf;               // the body origin transform
00437         b2Sweep m_sweep;                // the swept motion for CCD
00438 
00439         b2Vec2 m_linearVelocity;
00440         float32 m_angularVelocity;
00441 
00442         b2Vec2 m_force;
00443         float32 m_torque;
00444 
00445         b2World* m_world;
00446         b2Body* m_prev;
00447         b2Body* m_next;
00448 
00449         b2Fixture* m_fixtureList;
00450         int32 m_fixtureCount;
00451 
00452         b2JointEdge* m_jointList;
00453         b2ContactEdge* m_contactList;
00454 
00455         float32 m_mass, m_invMass;
00456 
00457         // Rotational inertia about the center of mass.
00458         float32 m_I, m_invI;
00459 
00460         float32 m_linearDamping;
00461         float32 m_angularDamping;
00462         float32 m_gravityScale;
00463 
00464         float32 m_sleepTime;
00465 
00466         void* m_userData;
00467 };
00468 
00469 inline b2BodyType b2Body::GetType() const
00470 {
00471         return m_type;
00472 }
00473 
00474 inline const b2Transform& b2Body::GetTransform() const
00475 {
00476         return m_xf;
00477 }
00478 
00479 inline const b2Vec2& b2Body::GetPosition() const
00480 {
00481         return m_xf.p;
00482 }
00483 
00484 inline float32 b2Body::GetAngle() const
00485 {
00486         return m_sweep.a;
00487 }
00488 
00489 inline const b2Vec2& b2Body::GetWorldCenter() const
00490 {
00491         return m_sweep.c;
00492 }
00493 
00494 inline const b2Vec2& b2Body::GetLocalCenter() const
00495 {
00496         return m_sweep.localCenter;
00497 }
00498 
00499 inline void b2Body::SetLinearVelocity(const b2Vec2& v)
00500 {
00501         if (m_type == b2_staticBody)
00502         {
00503                 return;
00504         }
00505 
00506         if (b2Dot(v,v) > 0.0f)
00507         {
00508                 SetAwake(true);
00509         }
00510 
00511         m_linearVelocity = v;
00512 }
00513 
00514 inline const b2Vec2& b2Body::GetLinearVelocity() const
00515 {
00516         return m_linearVelocity;
00517 }
00518 
00519 inline void b2Body::SetAngularVelocity(float32 w)
00520 {
00521         if (m_type == b2_staticBody)
00522         {
00523                 return;
00524         }
00525 
00526         if (w * w > 0.0f)
00527         {
00528                 SetAwake(true);
00529         }
00530 
00531         m_angularVelocity = w;
00532 }
00533 
00534 inline float32 b2Body::GetAngularVelocity() const
00535 {
00536         return m_angularVelocity;
00537 }
00538 
00539 inline float32 b2Body::GetMass() const
00540 {
00541         return m_mass;
00542 }
00543 
00544 inline float32 b2Body::GetInertia() const
00545 {
00546         return m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
00547 }
00548 
00549 inline void b2Body::GetMassData(b2MassData* data) const
00550 {
00551         data->mass = m_mass;
00552         data->I = m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
00553         data->center = m_sweep.localCenter;
00554 }
00555 
00556 inline b2Vec2 b2Body::GetWorldPoint(const b2Vec2& localPoint) const
00557 {
00558         return b2Mul(m_xf, localPoint);
00559 }
00560 
00561 inline b2Vec2 b2Body::GetWorldVector(const b2Vec2& localVector) const
00562 {
00563         return b2Mul(m_xf.q, localVector);
00564 }
00565 
00566 inline b2Vec2 b2Body::GetLocalPoint(const b2Vec2& worldPoint) const
00567 {
00568         return b2MulT(m_xf, worldPoint);
00569 }
00570 
00571 inline b2Vec2 b2Body::GetLocalVector(const b2Vec2& worldVector) const
00572 {
00573         return b2MulT(m_xf.q, worldVector);
00574 }
00575 
00576 inline b2Vec2 b2Body::GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const
00577 {
00578         return m_linearVelocity + b2Cross(m_angularVelocity, worldPoint - m_sweep.c);
00579 }
00580 
00581 inline b2Vec2 b2Body::GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const
00582 {
00583         return GetLinearVelocityFromWorldPoint(GetWorldPoint(localPoint));
00584 }
00585 
00586 inline float32 b2Body::GetLinearDamping() const
00587 {
00588         return m_linearDamping;
00589 }
00590 
00591 inline void b2Body::SetLinearDamping(float32 linearDamping)
00592 {
00593         m_linearDamping = linearDamping;
00594 }
00595 
00596 inline float32 b2Body::GetAngularDamping() const
00597 {
00598         return m_angularDamping;
00599 }
00600 
00601 inline void b2Body::SetAngularDamping(float32 angularDamping)
00602 {
00603         m_angularDamping = angularDamping;
00604 }
00605 
00606 inline float32 b2Body::GetGravityScale() const
00607 {
00608         return m_gravityScale;
00609 }
00610 
00611 inline void b2Body::SetGravityScale(float32 scale)
00612 {
00613         m_gravityScale = scale;
00614 }
00615 
00616 inline void b2Body::SetBullet(bool flag)
00617 {
00618         if (flag)
00619         {
00620                 m_flags |= e_bulletFlag;
00621         }
00622         else
00623         {
00624                 m_flags &= ~e_bulletFlag;
00625         }
00626 }
00627 
00628 inline bool b2Body::IsBullet() const
00629 {
00630         return (m_flags & e_bulletFlag) == e_bulletFlag;
00631 }
00632 
00633 inline void b2Body::SetAwake(bool flag)
00634 {
00635         if (flag)
00636         {
00637                 if ((m_flags & e_awakeFlag) == 0)
00638                 {
00639                         m_flags |= e_awakeFlag;
00640                         m_sleepTime = 0.0f;
00641                 }
00642         }
00643         else
00644         {
00645                 m_flags &= ~e_awakeFlag;
00646                 m_sleepTime = 0.0f;
00647                 m_linearVelocity.SetZero();
00648                 m_angularVelocity = 0.0f;
00649                 m_force.SetZero();
00650                 m_torque = 0.0f;
00651         }
00652 }
00653 
00654 inline bool b2Body::IsAwake() const
00655 {
00656         return (m_flags & e_awakeFlag) == e_awakeFlag;
00657 }
00658 
00659 inline bool b2Body::IsActive() const
00660 {
00661         return (m_flags & e_activeFlag) == e_activeFlag;
00662 }
00663 
00664 inline bool b2Body::IsFixedRotation() const
00665 {
00666         return (m_flags & e_fixedRotationFlag) == e_fixedRotationFlag;
00667 }
00668 
00669 inline void b2Body::SetSleepingAllowed(bool flag)
00670 {
00671         if (flag)
00672         {
00673                 m_flags |= e_autoSleepFlag;
00674         }
00675         else
00676         {
00677                 m_flags &= ~e_autoSleepFlag;
00678                 SetAwake(true);
00679         }
00680 }
00681 
00682 inline bool b2Body::IsSleepingAllowed() const
00683 {
00684         return (m_flags & e_autoSleepFlag) == e_autoSleepFlag;
00685 }
00686 
00687 inline b2Fixture* b2Body::GetFixtureList()
00688 {
00689         return m_fixtureList;
00690 }
00691 
00692 inline const b2Fixture* b2Body::GetFixtureList() const
00693 {
00694         return m_fixtureList;
00695 }
00696 
00697 inline b2JointEdge* b2Body::GetJointList()
00698 {
00699         return m_jointList;
00700 }
00701 
00702 inline const b2JointEdge* b2Body::GetJointList() const
00703 {
00704         return m_jointList;
00705 }
00706 
00707 inline b2ContactEdge* b2Body::GetContactList()
00708 {
00709         return m_contactList;
00710 }
00711 
00712 inline const b2ContactEdge* b2Body::GetContactList() const
00713 {
00714         return m_contactList;
00715 }
00716 
00717 inline b2Body* b2Body::GetNext()
00718 {
00719         return m_next;
00720 }
00721 
00722 inline const b2Body* b2Body::GetNext() const
00723 {
00724         return m_next;
00725 }
00726 
00727 inline void b2Body::SetUserData(void* data)
00728 {
00729         m_userData = data;
00730 }
00731 
00732 inline void* b2Body::GetUserData() const
00733 {
00734         return m_userData;
00735 }
00736 
00737 inline void b2Body::ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake)
00738 {
00739         if (m_type != b2_dynamicBody)
00740         {
00741                 return;
00742         }
00743 
00744         if (wake && (m_flags & e_awakeFlag) == 0)
00745         {
00746                 SetAwake(true);
00747         }
00748 
00749         // Don't accumulate a force if the body is sleeping.
00750         if (m_flags & e_awakeFlag)
00751         {
00752                 m_force += force;
00753                 m_torque += b2Cross(point - m_sweep.c, force);
00754         }
00755 }
00756 
00757 inline void b2Body::ApplyForceToCenter(const b2Vec2& force, bool wake)
00758 {
00759         if (m_type != b2_dynamicBody)
00760         {
00761                 return;
00762         }
00763 
00764         if (wake && (m_flags & e_awakeFlag) == 0)
00765         {
00766                 SetAwake(true);
00767         }
00768 
00769         // Don't accumulate a force if the body is sleeping
00770         if (m_flags & e_awakeFlag)
00771         {
00772                 m_force += force;
00773         }
00774 }
00775 
00776 inline void b2Body::ApplyTorque(float32 torque, bool wake)
00777 {
00778         if (m_type != b2_dynamicBody)
00779         {
00780                 return;
00781         }
00782 
00783         if (wake && (m_flags & e_awakeFlag) == 0)
00784         {
00785                 SetAwake(true);
00786         }
00787 
00788         // Don't accumulate a force if the body is sleeping
00789         if (m_flags & e_awakeFlag)
00790         {
00791                 m_torque += torque;
00792         }
00793 }
00794 
00795 inline void b2Body::ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake)
00796 {
00797         if (m_type != b2_dynamicBody)
00798         {
00799                 return;
00800         }
00801 
00802         if (wake && (m_flags & e_awakeFlag) == 0)
00803         {
00804                 SetAwake(true);
00805         }
00806 
00807         // Don't accumulate velocity if the body is sleeping
00808         if (m_flags & e_awakeFlag)
00809         {
00810                 m_linearVelocity += m_invMass * impulse;
00811                 m_angularVelocity += m_invI * b2Cross(point - m_sweep.c, impulse);
00812         }
00813 }
00814 
00815 inline void b2Body::ApplyAngularImpulse(float32 impulse, bool wake)
00816 {
00817         if (m_type != b2_dynamicBody)
00818         {
00819                 return;
00820         }
00821 
00822         if (wake && (m_flags & e_awakeFlag) == 0)
00823         {
00824                 SetAwake(true);
00825         }
00826 
00827         // Don't accumulate velocity if the body is sleeping
00828         if (m_flags & e_awakeFlag)
00829         {
00830                 m_angularVelocity += m_invI * impulse;
00831         }
00832 }
00833 
00834 inline void b2Body::SynchronizeTransform()
00835 {
00836         m_xf.q.Set(m_sweep.a);
00837         m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
00838 }
00839 
00840 inline void b2Body::Advance(float32 alpha)
00841 {
00842         // Advance to the new safe time. This doesn't sync the broad-phase.
00843         m_sweep.Advance(alpha);
00844         m_sweep.c = m_sweep.c0;
00845         m_sweep.a = m_sweep.a0;
00846         m_xf.q.Set(m_sweep.a);
00847         m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
00848 }
00849 
00850 inline b2World* b2Body::GetWorld()
00851 {
00852         return m_world;
00853 }
00854 
00855 inline const b2World* b2Body::GetWorld() const
00856 {
00857         return m_world;
00858 }
00859 
00860 #endif


mvsim
Author(s):
autogenerated on Thu Sep 7 2017 09:27:47