Box2D 2.4.1
A 2D physics engine for games
Loading...
Searching...
No Matches
b2_body.h
1// MIT License
2
3// Copyright (c) 2019 Erin Catto
4
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#ifndef B2_BODY_H
24#define B2_BODY_H
25
26#include "b2_api.h"
27#include "b2_math.h"
28#include "b2_shape.h"
29
30class b2Fixture;
31class b2Joint;
32class b2Contact;
33class b2Controller;
34class b2World;
35struct b2FixtureDef;
36struct b2JointEdge;
37struct b2ContactEdge;
38
43enum b2BodyType
44{
45 b2_staticBody = 0,
46 b2_kinematicBody,
47 b2_dynamicBody
48};
49
52struct B2_API b2BodyDef
53{
56 {
57 position.Set(0.0f, 0.0f);
58 angle = 0.0f;
59 linearVelocity.Set(0.0f, 0.0f);
60 angularVelocity = 0.0f;
61 linearDamping = 0.0f;
62 angularDamping = 0.0f;
63 allowSleep = true;
64 awake = true;
65 fixedRotation = false;
66 bullet = false;
67 type = b2_staticBody;
68 enabled = true;
69 gravityScale = 1.0f;
70 }
71
74 b2BodyType type;
75
79
81 float angle;
82
85
88
94
100
104
106 bool awake;
107
110
115 bool bullet;
116
119
122
125};
126
128class B2_API b2Body
129{
130public:
139
147 b2Fixture* CreateFixture(const b2Shape* shape, float density);
148
156 void DestroyFixture(b2Fixture* fixture);
157
163 void SetTransform(const b2Vec2& position, float angle);
164
167 const b2Transform& GetTransform() const;
168
171 const b2Vec2& GetPosition() const;
172
175 float GetAngle() const;
176
178 const b2Vec2& GetWorldCenter() const;
179
181 const b2Vec2& GetLocalCenter() const;
182
185 void SetLinearVelocity(const b2Vec2& v);
186
189 const b2Vec2& GetLinearVelocity() const;
190
193 void SetAngularVelocity(float omega);
194
197 float GetAngularVelocity() const;
198
205 void ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake);
206
210 void ApplyForceToCenter(const b2Vec2& force, bool wake);
211
216 void ApplyTorque(float torque, bool wake);
217
224 void ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake);
225
229 void ApplyLinearImpulseToCenter(const b2Vec2& impulse, bool wake);
230
234 void ApplyAngularImpulse(float impulse, bool wake);
235
238 float GetMass() const;
239
242 float GetInertia() const;
243
246 b2MassData GetMassData() const;
247
253 void SetMassData(const b2MassData* data);
254
259
263 b2Vec2 GetWorldPoint(const b2Vec2& localPoint) const;
264
268 b2Vec2 GetWorldVector(const b2Vec2& localVector) const;
269
273 b2Vec2 GetLocalPoint(const b2Vec2& worldPoint) const;
274
278 b2Vec2 GetLocalVector(const b2Vec2& worldVector) const;
279
283 b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const;
284
288 b2Vec2 GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const;
289
291 float GetLinearDamping() const;
292
294 void SetLinearDamping(float linearDamping);
295
297 float GetAngularDamping() const;
298
300 void SetAngularDamping(float angularDamping);
301
303 float GetGravityScale() const;
304
306 void SetGravityScale(float scale);
307
309 void SetType(b2BodyType type);
310
312 b2BodyType GetType() const;
313
315 void SetBullet(bool flag);
316
318 bool IsBullet() const;
319
322 void SetSleepingAllowed(bool flag);
323
325 bool IsSleepingAllowed() const;
326
330 void SetAwake(bool flag);
331
334 bool IsAwake() const;
335
348 void SetEnabled(bool flag);
349
351 bool IsEnabled() const;
352
355 void SetFixedRotation(bool flag);
356
358 bool IsFixedRotation() const;
359
361 b2Fixture* GetFixtureList();
362 const b2Fixture* GetFixtureList() const;
363
365 b2JointEdge* GetJointList();
366 const b2JointEdge* GetJointList() const;
367
371 b2ContactEdge* GetContactList();
372 const b2ContactEdge* GetContactList() const;
373
375 b2Body* GetNext();
376 const b2Body* GetNext() const;
377
379 b2BodyUserData& GetUserData();
380 const b2BodyUserData& GetUserData() const;
381
383 b2World* GetWorld();
384 const b2World* GetWorld() const;
385
387 void Dump();
388
389private:
390
391 friend class b2World;
392 friend class b2Island;
393 friend class b2ContactManager;
394 friend class b2ContactSolver;
395 friend class b2Contact;
396
397 friend class b2DistanceJoint;
398 friend class b2FrictionJoint;
399 friend class b2GearJoint;
400 friend class b2MotorJoint;
401 friend class b2MouseJoint;
402 friend class b2PrismaticJoint;
403 friend class b2PulleyJoint;
404 friend class b2RevoluteJoint;
405 friend class b2WeldJoint;
406 friend class b2WheelJoint;
407
408 // m_flags
409 enum
410 {
411 e_islandFlag = 0x0001,
412 e_awakeFlag = 0x0002,
413 e_autoSleepFlag = 0x0004,
414 e_bulletFlag = 0x0008,
415 e_fixedRotationFlag = 0x0010,
416 e_enabledFlag = 0x0020,
417 e_toiFlag = 0x0040
418 };
419
420 b2Body(const b2BodyDef* bd, b2World* world);
421 ~b2Body();
422
423 void SynchronizeFixtures();
424 void SynchronizeTransform();
425
426 // This is used to prevent connected bodies from colliding.
427 // It may lie, depending on the collideConnected flag.
428 bool ShouldCollide(const b2Body* other) const;
429
430 void Advance(float t);
431
432 b2BodyType m_type;
433
434 uint16 m_flags;
435
436 int32 m_islandIndex;
437
438 b2Transform m_xf; // the body origin transform
439 b2Sweep m_sweep; // the swept motion for CCD
440
441 b2Vec2 m_linearVelocity;
442 float m_angularVelocity;
443
444 b2Vec2 m_force;
445 float m_torque;
446
447 b2World* m_world;
448 b2Body* m_prev;
449 b2Body* m_next;
450
451 b2Fixture* m_fixtureList;
452 int32 m_fixtureCount;
453
454 b2JointEdge* m_jointList;
455 b2ContactEdge* m_contactList;
456
457 float m_mass, m_invMass;
458
459 // Rotational inertia about the center of mass.
460 float m_I, m_invI;
461
462 float m_linearDamping;
463 float m_angularDamping;
464 float m_gravityScale;
465
466 float m_sleepTime;
467
468 b2BodyUserData m_userData;
469};
470
471inline b2BodyType b2Body::GetType() const
472{
473 return m_type;
474}
475
477{
478 return m_xf;
479}
480
481inline const b2Vec2& b2Body::GetPosition() const
482{
483 return m_xf.p;
484}
485
486inline float b2Body::GetAngle() const
487{
488 return m_sweep.a;
489}
490
491inline const b2Vec2& b2Body::GetWorldCenter() const
492{
493 return m_sweep.c;
494}
495
496inline const b2Vec2& b2Body::GetLocalCenter() const
497{
498 return m_sweep.localCenter;
499}
500
502{
503 if (m_type == b2_staticBody)
504 {
505 return;
506 }
507
508 if (b2Dot(v,v) > 0.0f)
509 {
510 SetAwake(true);
511 }
512
513 m_linearVelocity = v;
514}
515
517{
518 return m_linearVelocity;
519}
520
521inline void b2Body::SetAngularVelocity(float w)
522{
523 if (m_type == b2_staticBody)
524 {
525 return;
526 }
527
528 if (w * w > 0.0f)
529 {
530 SetAwake(true);
531 }
532
533 m_angularVelocity = w;
534}
535
536inline float b2Body::GetAngularVelocity() const
537{
538 return m_angularVelocity;
539}
540
541inline float b2Body::GetMass() const
542{
543 return m_mass;
544}
545
546inline float b2Body::GetInertia() const
547{
548 return m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
549}
550
552{
553 b2MassData data;
554 data.mass = m_mass;
555 data.I = m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
556 data.center = m_sweep.localCenter;
557 return data;
558}
559
560inline b2Vec2 b2Body::GetWorldPoint(const b2Vec2& localPoint) const
561{
562 return b2Mul(m_xf, localPoint);
563}
564
565inline b2Vec2 b2Body::GetWorldVector(const b2Vec2& localVector) const
566{
567 return b2Mul(m_xf.q, localVector);
568}
569
570inline b2Vec2 b2Body::GetLocalPoint(const b2Vec2& worldPoint) const
571{
572 return b2MulT(m_xf, worldPoint);
573}
574
575inline b2Vec2 b2Body::GetLocalVector(const b2Vec2& worldVector) const
576{
577 return b2MulT(m_xf.q, worldVector);
578}
579
581{
582 return m_linearVelocity + b2Cross(m_angularVelocity, worldPoint - m_sweep.c);
583}
584
586{
588}
589
590inline float b2Body::GetLinearDamping() const
591{
592 return m_linearDamping;
593}
594
595inline void b2Body::SetLinearDamping(float linearDamping)
596{
597 m_linearDamping = linearDamping;
598}
599
600inline float b2Body::GetAngularDamping() const
601{
602 return m_angularDamping;
603}
604
605inline void b2Body::SetAngularDamping(float angularDamping)
606{
607 m_angularDamping = angularDamping;
608}
609
610inline float b2Body::GetGravityScale() const
611{
612 return m_gravityScale;
613}
614
615inline void b2Body::SetGravityScale(float scale)
616{
617 m_gravityScale = scale;
618}
619
620inline void b2Body::SetBullet(bool flag)
621{
622 if (flag)
623 {
624 m_flags |= e_bulletFlag;
625 }
626 else
627 {
628 m_flags &= ~e_bulletFlag;
629 }
630}
631
632inline bool b2Body::IsBullet() const
633{
634 return (m_flags & e_bulletFlag) == e_bulletFlag;
635}
636
637inline void b2Body::SetAwake(bool flag)
638{
639 if (m_type == b2_staticBody)
640 {
641 return;
642 }
643
644 if (flag)
645 {
646 m_flags |= e_awakeFlag;
647 m_sleepTime = 0.0f;
648 }
649 else
650 {
651 m_flags &= ~e_awakeFlag;
652 m_sleepTime = 0.0f;
653 m_linearVelocity.SetZero();
654 m_angularVelocity = 0.0f;
655 m_force.SetZero();
656 m_torque = 0.0f;
657 }
658}
659
660inline bool b2Body::IsAwake() const
661{
662 return (m_flags & e_awakeFlag) == e_awakeFlag;
663}
664
665inline bool b2Body::IsEnabled() const
666{
667 return (m_flags & e_enabledFlag) == e_enabledFlag;
668}
669
670inline bool b2Body::IsFixedRotation() const
671{
672 return (m_flags & e_fixedRotationFlag) == e_fixedRotationFlag;
673}
674
675inline void b2Body::SetSleepingAllowed(bool flag)
676{
677 if (flag)
678 {
679 m_flags |= e_autoSleepFlag;
680 }
681 else
682 {
683 m_flags &= ~e_autoSleepFlag;
684 SetAwake(true);
685 }
686}
687
688inline bool b2Body::IsSleepingAllowed() const
689{
690 return (m_flags & e_autoSleepFlag) == e_autoSleepFlag;
691}
692
694{
695 return m_fixtureList;
696}
697
698inline const b2Fixture* b2Body::GetFixtureList() const
699{
700 return m_fixtureList;
701}
702
704{
705 return m_jointList;
706}
707
708inline const b2JointEdge* b2Body::GetJointList() const
709{
710 return m_jointList;
711}
712
714{
715 return m_contactList;
716}
717
718inline const b2ContactEdge* b2Body::GetContactList() const
719{
720 return m_contactList;
721}
722
724{
725 return m_next;
726}
727
728inline const b2Body* b2Body::GetNext() const
729{
730 return m_next;
731}
732
734{
735 return m_userData;
736}
737
738inline const b2BodyUserData& b2Body::GetUserData() const
739{
740 return m_userData;
741}
742
743inline void b2Body::ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake)
744{
745 if (m_type != b2_dynamicBody)
746 {
747 return;
748 }
749
750 if (wake && (m_flags & e_awakeFlag) == 0)
751 {
752 SetAwake(true);
753 }
754
755 // Don't accumulate a force if the body is sleeping.
756 if (m_flags & e_awakeFlag)
757 {
758 m_force += force;
759 m_torque += b2Cross(point - m_sweep.c, force);
760 }
761}
762
763inline void b2Body::ApplyForceToCenter(const b2Vec2& force, bool wake)
764{
765 if (m_type != b2_dynamicBody)
766 {
767 return;
768 }
769
770 if (wake && (m_flags & e_awakeFlag) == 0)
771 {
772 SetAwake(true);
773 }
774
775 // Don't accumulate a force if the body is sleeping
776 if (m_flags & e_awakeFlag)
777 {
778 m_force += force;
779 }
780}
781
782inline void b2Body::ApplyTorque(float torque, bool wake)
783{
784 if (m_type != b2_dynamicBody)
785 {
786 return;
787 }
788
789 if (wake && (m_flags & e_awakeFlag) == 0)
790 {
791 SetAwake(true);
792 }
793
794 // Don't accumulate a force if the body is sleeping
795 if (m_flags & e_awakeFlag)
796 {
797 m_torque += torque;
798 }
799}
800
801inline void b2Body::ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake)
802{
803 if (m_type != b2_dynamicBody)
804 {
805 return;
806 }
807
808 if (wake && (m_flags & e_awakeFlag) == 0)
809 {
810 SetAwake(true);
811 }
812
813 // Don't accumulate velocity if the body is sleeping
814 if (m_flags & e_awakeFlag)
815 {
816 m_linearVelocity += m_invMass * impulse;
817 m_angularVelocity += m_invI * b2Cross(point - m_sweep.c, impulse);
818 }
819}
820
821inline void b2Body::ApplyLinearImpulseToCenter(const b2Vec2& impulse, bool wake)
822{
823 if (m_type != b2_dynamicBody)
824 {
825 return;
826 }
827
828 if (wake && (m_flags & e_awakeFlag) == 0)
829 {
830 SetAwake(true);
831 }
832
833 // Don't accumulate velocity if the body is sleeping
834 if (m_flags & e_awakeFlag)
835 {
836 m_linearVelocity += m_invMass * impulse;
837 }
838}
839
840inline void b2Body::ApplyAngularImpulse(float impulse, bool wake)
841{
842 if (m_type != b2_dynamicBody)
843 {
844 return;
845 }
846
847 if (wake && (m_flags & e_awakeFlag) == 0)
848 {
849 SetAwake(true);
850 }
851
852 // Don't accumulate velocity if the body is sleeping
853 if (m_flags & e_awakeFlag)
854 {
855 m_angularVelocity += m_invI * impulse;
856 }
857}
858
859inline void b2Body::SynchronizeTransform()
860{
861 m_xf.q.Set(m_sweep.a);
862 m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
863}
864
865inline void b2Body::Advance(float alpha)
866{
867 // Advance to the new safe time. This doesn't sync the broad-phase.
868 m_sweep.Advance(alpha);
869 m_sweep.c = m_sweep.c0;
870 m_sweep.a = m_sweep.a0;
871 m_xf.q.Set(m_sweep.a);
872 m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
873}
874
876{
877 return m_world;
878}
879
880inline const b2World* b2Body::GetWorld() const
881{
882 return m_world;
883}
884
885#endif
A rigid body. These are created via b2World::CreateBody.
Definition b2_body.h:129
bool IsEnabled() const
Get the active state of the body.
Definition b2_body.h:665
void SetEnabled(bool flag)
bool IsFixedRotation() const
Does this body have fixed rotation?
Definition b2_body.h:670
b2Vec2 GetLinearVelocityFromLocalPoint(const b2Vec2 &localPoint) const
Definition b2_body.h:585
b2Vec2 GetLocalPoint(const b2Vec2 &worldPoint) const
Definition b2_body.h:570
b2Fixture * CreateFixture(const b2Shape *shape, float density)
void ResetMassData()
b2BodyUserData & GetUserData()
Get the user data pointer that was provided in the body definition.
Definition b2_body.h:733
b2ContactEdge * GetContactList()
Definition b2_body.h:713
float GetAngularDamping() const
Get the angular damping of the body.
Definition b2_body.h:600
void ApplyAngularImpulse(float impulse, bool wake)
Definition b2_body.h:840
void SetAngularVelocity(float omega)
Definition b2_body.h:521
void SetSleepingAllowed(bool flag)
Definition b2_body.h:675
void SetBullet(bool flag)
Should this body be treated like a bullet for continuous collision detection?
Definition b2_body.h:620
void SetType(b2BodyType type)
Set the type of this body. This may alter the mass and velocity.
const b2Vec2 & GetWorldCenter() const
Get the world position of the center of mass.
Definition b2_body.h:491
b2JointEdge * GetJointList()
Get the list of all joints attached to this body.
Definition b2_body.h:703
void SetMassData(const b2MassData *data)
b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2 &worldPoint) const
Definition b2_body.h:580
const b2Vec2 & GetLocalCenter() const
Get the local position of the center of mass.
Definition b2_body.h:496
b2Fixture * GetFixtureList()
Get the list of all fixtures attached to this body.
Definition b2_body.h:693
bool IsAwake() const
Definition b2_body.h:660
float GetLinearDamping() const
Get the linear damping of the body.
Definition b2_body.h:590
b2Vec2 GetWorldPoint(const b2Vec2 &localPoint) const
Definition b2_body.h:560
float GetAngularVelocity() const
Definition b2_body.h:536
const b2Vec2 & GetPosition() const
Definition b2_body.h:481
void ApplyLinearImpulse(const b2Vec2 &impulse, const b2Vec2 &point, bool wake)
Definition b2_body.h:801
void SetAngularDamping(float angularDamping)
Set the angular damping of the body.
Definition b2_body.h:605
void SetLinearVelocity(const b2Vec2 &v)
Definition b2_body.h:501
void DestroyFixture(b2Fixture *fixture)
float GetMass() const
Definition b2_body.h:541
b2MassData GetMassData() const
Definition b2_body.h:551
float GetGravityScale() const
Get the gravity scale of the body.
Definition b2_body.h:610
const b2Vec2 & GetLinearVelocity() const
Definition b2_body.h:516
void SetGravityScale(float scale)
Set the gravity scale of the body.
Definition b2_body.h:615
void ApplyForce(const b2Vec2 &force, const b2Vec2 &point, bool wake)
Definition b2_body.h:743
b2BodyType GetType() const
Get the type of this body.
Definition b2_body.h:471
void SetLinearDamping(float linearDamping)
Set the linear damping of the body.
Definition b2_body.h:595
b2Fixture * CreateFixture(const b2FixtureDef *def)
float GetAngle() const
Definition b2_body.h:486
void ApplyForceToCenter(const b2Vec2 &force, bool wake)
Definition b2_body.h:763
b2World * GetWorld()
Get the parent world of this body.
Definition b2_body.h:875
void SetTransform(const b2Vec2 &position, float angle)
bool IsSleepingAllowed() const
Is this body allowed to sleep.
Definition b2_body.h:688
void SetAwake(bool flag)
Definition b2_body.h:637
void Dump()
Dump this body to a file.
void ApplyTorque(float torque, bool wake)
Definition b2_body.h:782
b2Body * GetNext()
Get the next body in the world's body list.
Definition b2_body.h:723
bool IsBullet() const
Is this body treated like a bullet for continuous collision detection?
Definition b2_body.h:632
b2Vec2 GetWorldVector(const b2Vec2 &localVector) const
Definition b2_body.h:565
float GetInertia() const
Definition b2_body.h:546
b2Vec2 GetLocalVector(const b2Vec2 &worldVector) const
Definition b2_body.h:575
void ApplyLinearImpulseToCenter(const b2Vec2 &impulse, bool wake)
Definition b2_body.h:821
const b2Transform & GetTransform() const
Definition b2_body.h:476
void SetFixedRotation(bool flag)
Definition b2_contact.h:89
Definition b2_contact_manager.h:36
Definition b2_distance_joint.h:77
Definition b2_fixture.h:117
Definition b2_friction_joint.h:61
Definition b2_gear_joint.h:62
Definition b2_joint.h:110
Definition b2_motor_joint.h:65
Definition b2_mouse_joint.h:66
Definition b2_prismatic_joint.h:92
Definition b2_pulley_joint.h:85
Definition b2_revolute_joint.h:95
Definition b2_shape.h:49
Definition b2_weld_joint.h:70
Definition b2_wheel_joint.h:96
Definition b2_world.h:47
Definition b2_body.h:53
bool allowSleep
Definition b2_body.h:103
bool awake
Is this body initially awake or sleeping?
Definition b2_body.h:106
b2Vec2 linearVelocity
The linear velocity of the body's origin in world co-ordinates.
Definition b2_body.h:84
bool fixedRotation
Should this body be prevented from rotating? Useful for characters.
Definition b2_body.h:109
bool enabled
Does this body start out enabled?
Definition b2_body.h:118
b2Vec2 position
Definition b2_body.h:78
bool bullet
Definition b2_body.h:115
b2BodyDef()
This constructor sets the body definition default values.
Definition b2_body.h:55
b2BodyType type
Definition b2_body.h:74
float linearDamping
Definition b2_body.h:93
float angularVelocity
The angular velocity of the body.
Definition b2_body.h:87
float angle
The world angle of the body in radians.
Definition b2_body.h:81
float gravityScale
Scale the gravity applied to this body.
Definition b2_body.h:124
b2BodyUserData userData
Use this to store application specific body data.
Definition b2_body.h:121
float angularDamping
Definition b2_body.h:99
You can define this to inject whatever data you want in b2Body.
Definition b2_settings.h:59
Definition b2_contact.h:78
Definition b2_fixture.h:62
Definition b2_joint.h:63
This holds the mass data computed for a shape.
Definition b2_shape.h:34
b2Vec2 center
The position of the shape's centroid relative to the shape's origin.
Definition b2_shape.h:39
float mass
The mass of the shape, usually in kilograms.
Definition b2_shape.h:36
float I
The rotational inertia of the shape about the local origin.
Definition b2_shape.h:42
void Set(float angle)
Set using an angle in radians.
Definition b2_math.h:300
Definition b2_math.h:369
b2Vec2 c
center world positions
Definition b2_math.h:385
void Advance(float alpha)
Definition b2_math.h:699
b2Vec2 localCenter
local center of mass position
Definition b2_math.h:384
float a
world angles
Definition b2_math.h:386
Definition b2_math.h:339
A 2D column vector.
Definition b2_math.h:42
void SetZero()
Set this vector to all zeros.
Definition b2_math.h:50