Box2D  3.0.0
A Real-Time-Oriented 2-D Physics Engine
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
box2d::Body Class Reference

A physical entity that exists within a World. More...

#include <Body.hpp>

Public Types

using Fixtures = std::forward_list< Fixture * >
 
using Joints = std::unordered_set< Joint * >
 
using Contacts = std::list< Contact * >
 

Public Member Functions

FixtureCreateFixture (std::shared_ptr< const Shape > shape, const FixtureDef &def=GetDefaultFixtureDef(), bool resetMassData=true)
 Creates a fixture and attaches it to this body. More...
 
bool DestroyFixture (Fixture *fixture, bool resetMassData=true)
 Destroys a fixture. More...
 
void SetTransform (const Length2D position, Angle angle)
 Sets the position of the body's origin and rotation. More...
 
Transformation GetTransformation () const noexcept
 Gets the body transform for the body's origin. More...
 
Length2D GetLocation () const noexcept
 Gets the world body origin location. More...
 
const SweepGetSweep () const noexcept
 
Angle GetAngle () const noexcept
 Get the angle in radians. More...
 
Length2D GetWorldCenter () const noexcept
 Get the world position of the center of mass. More...
 
Length2D GetLocalCenter () const noexcept
 Gets the local position of the center of mass. More...
 
Velocity GetVelocity () const noexcept
 
void SetVelocity (const Velocity &v) noexcept
 Sets the body's velocity (linear and angular velocity). More...
 
void SetAcceleration (const LinearAcceleration2D linear, const AngularAcceleration angular) noexcept
 Sets the linear and rotational accelerations on this body. More...
 
LinearAcceleration2D GetLinearAcceleration () const noexcept
 
AngularAcceleration GetAngularAcceleration () const noexcept
 
InvMass GetInvMass () const noexcept
 Gets the inverse total mass of the body. More...
 
InvRotInertia GetInvRotInertia () const noexcept
 Gets the inverse rotational inertia of the body. More...
 
void SetMassData (const MassData &data)
 Set the mass properties to override the mass properties of the fixtures. More...
 
void ResetMassData ()
 Resets the mass data properties. More...
 
RealNum GetLinearDamping () const noexcept
 Get the linear damping of the body. More...
 
void SetLinearDamping (RealNum linearDamping) noexcept
 Set the linear damping of the body. More...
 
RealNum GetAngularDamping () const noexcept
 Get the angular damping of the body. More...
 
void SetAngularDamping (RealNum angularDamping) noexcept
 Set the angular damping of the body. More...
 
void SetType (BodyType type)
 Set the type of this body. This may alter the mass and velocity. More...
 
BodyType GetType () const noexcept
 Get the type of this body. More...
 
bool IsSpeedable () const noexcept
 Is "speedable". More...
 
bool IsAccelerable () const noexcept
 Is accelerable. More...
 
void SetBullet (bool flag) noexcept
 Should this body be treated like a bullet for continuous collision detection? More...
 
bool IsImpenetrable () const noexcept
 Is this body treated like a bullet for continuous collision detection? More...
 
void SetSleepingAllowed (bool flag) noexcept
 You can disable sleeping on this body. If you disable sleeping, the body will be woken. More...
 
bool IsSleepingAllowed () const noexcept
 Is this body allowed to sleep. More...
 
void SetAwake () noexcept
 Sets this body to awake if it's a "speedable" body. More...
 
void UnsetAwake () noexcept
 Sets this body to asleep if sleeping is allowed. More...
 
bool IsAwake () const noexcept
 Gets the awake/asleep state of this body. More...
 
Time GetUnderActiveTime () const noexcept
 Gets this body's under-active time value. More...
 
void SetUnderActiveTime (Time value) noexcept
 Sets the "under-active" time to the given value. More...
 
void SetEnabled (bool flag)
 Set the enabled state of the body. A disabled body is not simulated and cannot be collided with or woken up. If you pass a flag of true, all fixtures will be added to the broad-phase. If you pass a flag of false, all fixtures will be removed from the broad-phase and all contacts will be destroyed. Fixtures and joints are otherwise unaffected. You may continue to create/destroy fixtures and joints on disabled bodies. Fixtures on a disabled body are implicitly disabled and will not participate in collisions, ray-casts, or queries. Joints connected to a disabled body are implicitly disabled. A disabled body is still owned by a World object and remains in the body list. More...
 
bool IsEnabled () const noexcept
 Get the enabled/disabled state of the body. More...
 
void SetFixedRotation (bool flag)
 Set this body to have fixed rotation. This causes the mass to be reset. More...
 
bool IsFixedRotation () const noexcept
 Does this body have fixed rotation? More...
 
const FixturesGetFixtures () const noexcept
 Gets the container of all fixtures attached to this body. More...
 
const JointsGetJoints () const noexcept
 Gets the container of all joints attached to this body. More...
 
const ContactsGetContacts () const noexcept
 Gets the container of all contacts attached to this body. More...
 
void * GetUserData () const noexcept
 Get the user data pointer that was provided in the body definition. More...
 
void SetUserData (void *data) noexcept
 Set the user data. Use this to store your application specific data. More...
 
WorldGetWorld () noexcept
 Gets the parent world of this body. More...
 
const WorldGetWorld () const noexcept
 Gets the parent world of this body. More...
 
bool IsMassDataDirty () const noexcept
 

Static Public Member Functions

static bool IsValid (const Shape &shape)
 

Static Public Attributes

static constexpr auto InvalidIslandIndex = static_cast<body_count_t>(-1)
 

Friends

class BodyAtty
 

Detailed Description

A physical entity that exists within a World.

A rigid body entity created or destroyed through a World instance.

Invariant
Only bodies that allow sleeping, can be put to sleep.
Only "speedable" bodies can be awake.
Only "speedable" bodies can have non-zero velocities.
Only "accelerable" bodies can have non-zero accelerations.
Only "accelerable" bodies can have non-zero "under-active" times.
Note
Create these using the World::Create method.
On a 64-bit architecture with 4-byte RealNum, this data structure is at least 192-bytes large.

Member Typedef Documentation

◆ Contacts

using box2d::Body::Contacts = std::list<Contact*>

◆ Fixtures

using box2d::Body::Fixtures = std::forward_list<Fixture*>

◆ Joints

using box2d::Body::Joints = std::unordered_set<Joint*>

Member Function Documentation

◆ CreateFixture()

Fixture * Body::CreateFixture ( std::shared_ptr< const Shape shape,
const FixtureDef def = GetDefaultFixtureDef(),
bool  resetMassData = true 
)

Creates a fixture and attaches it to this body.

Parameters
shapeSharable shape definition. Its vertex radius must be less than the minimum or more than the maximum allowed by the body's world.
defInitial fixture settings. Friction and density must be >= 0. Restitution must be > -infinity and < infinity.
resetMassDataWhether or not to reset the mass data of the body.
Note
This function should not be called if the world is locked.
Warning
This function is locked during callbacks.
Returns
nullptr if the world is locked or a parameter is dissallowed. A pointer to the created fixture otherwise.

◆ DestroyFixture()

bool Body::DestroyFixture ( Fixture fixture,
bool  resetMassData = true 
)

Destroys a fixture.

This removes the fixture from the broad-phase and destroys all contacts associated with this fixture. All fixtures attached to a body are implicitly destroyed when the body is destroyed.

Warning
This function is locked during callbacks.
Note
Make sure to explicitly call ResetMassData after fixtures have been destroyed.
See also
ResetMassData.
Parameters
fixturethe fixture to be removed.
resetMassDataWhether or not to reset the mass data.

◆ GetAngle()

Angle box2d::Body::GetAngle ( ) const
inlinenoexcept

Get the angle in radians.

Returns
the current world rotation angle in radians.

◆ GetAngularAcceleration()

AngularAcceleration box2d::Body::GetAngularAcceleration ( ) const
inlinenoexcept

◆ GetAngularDamping()

RealNum box2d::Body::GetAngularDamping ( ) const
inlinenoexcept

Get the angular damping of the body.

◆ GetContacts()

const Body::Contacts & box2d::Body::GetContacts ( ) const
inlinenoexcept

Gets the container of all contacts attached to this body.

Warning
This list changes during the time step and you may miss some collisions if you don't use ContactListener.

◆ GetFixtures()

const Body::Fixtures & box2d::Body::GetFixtures ( ) const
inlinenoexcept

Gets the container of all fixtures attached to this body.

◆ GetInvMass()

InvMass box2d::Body::GetInvMass ( ) const
inlinenoexcept

Gets the inverse total mass of the body.

This is the cached result of dividing 1 by the body's mass. Often floating division is much slower than multiplication. As such, it's likely faster to multiply values by this inverse value than to redivide them all the time by the mass.

Returns
Value of zero or more representing the body's inverse mass (in 1/kg).
See also
SetMassData.

◆ GetInvRotInertia()

InvRotInertia box2d::Body::GetInvRotInertia ( ) const
inlinenoexcept

Gets the inverse rotational inertia of the body.

This is the cached result of dividing 1 by the body's rotational inertia. Often floating division is much slower than multiplication. As such, it's likely faster to multiply values by this inverse value than to redivide them all the time by the rotational inertia.

Returns
Inverse rotational intertia (in 1/kg-m^2).

◆ GetJoints()

const Body::Joints & box2d::Body::GetJoints ( ) const
inlinenoexcept

Gets the container of all joints attached to this body.

◆ GetLinearAcceleration()

LinearAcceleration2D box2d::Body::GetLinearAcceleration ( ) const
inlinenoexcept

◆ GetLinearDamping()

RealNum box2d::Body::GetLinearDamping ( ) const
inlinenoexcept

Get the linear damping of the body.

◆ GetLocalCenter()

Length2D box2d::Body::GetLocalCenter ( ) const
inlinenoexcept

Gets the local position of the center of mass.

◆ GetLocation()

Length2D box2d::Body::GetLocation ( ) const
inlinenoexcept

Gets the world body origin location.

This is the location of the body's origin relative to its world. The location of the body after stepping the world's physics simulations is dependent on a number of factors:

  1. Location at the last time step.
  2. Forces acting on the body (gravity, applied force, applied impulse).
  3. The mass data of the body.
  4. Damping of the body.
  5. Restitution and friction values of the body's fixtures when they experience collisions.
    Returns
    World location of the body's origin.

◆ GetSweep()

const Sweep & box2d::Body::GetSweep ( ) const
inlinenoexcept

◆ GetTransformation()

Transformation box2d::Body::GetTransformation ( ) const
inlinenoexcept

Gets the body transform for the body's origin.

Returns
the world transform of the body's origin.

◆ GetType()

BodyType box2d::Body::GetType ( ) const
inlinenoexcept

Get the type of this body.

◆ GetUnderActiveTime()

Time box2d::Body::GetUnderActiveTime ( ) const
inlinenoexcept

Gets this body's under-active time value.

Returns
Zero or more time in seconds (of step time) that this body has been "under-active" for.

◆ GetUserData()

void * box2d::Body::GetUserData ( ) const
inlinenoexcept

Get the user data pointer that was provided in the body definition.

◆ GetVelocity()

Velocity box2d::Body::GetVelocity ( ) const
inlinenoexcept

◆ GetWorld() [1/2]

World * box2d::Body::GetWorld ( )
inlinenoexcept

Gets the parent world of this body.

◆ GetWorld() [2/2]

const World * box2d::Body::GetWorld ( ) const
inlinenoexcept

Gets the parent world of this body.

◆ GetWorldCenter()

Length2D box2d::Body::GetWorldCenter ( ) const
inlinenoexcept

Get the world position of the center of mass.

◆ IsAccelerable()

bool box2d::Body::IsAccelerable ( ) const
inlinenoexcept

Is accelerable.

Indicates whether this body is accelerable, ie. whether it is effected by forces. Only Dynamic bodies are accelerable.

Returns
true if the body is accelerable, false otherwise.

◆ IsAwake()

bool box2d::Body::IsAwake ( ) const
inlinenoexcept

Gets the awake/asleep state of this body.

Warning
Being awake may or may not imply being speedable.
Returns
true if the body is awake.

◆ IsEnabled()

bool box2d::Body::IsEnabled ( ) const
inlinenoexcept

Get the enabled/disabled state of the body.

◆ IsFixedRotation()

bool box2d::Body::IsFixedRotation ( ) const
inlinenoexcept

Does this body have fixed rotation?

◆ IsImpenetrable()

bool box2d::Body::IsImpenetrable ( ) const
inlinenoexcept

Is this body treated like a bullet for continuous collision detection?

◆ IsMassDataDirty()

bool box2d::Body::IsMassDataDirty ( ) const
inlinenoexcept

◆ IsSleepingAllowed()

bool box2d::Body::IsSleepingAllowed ( ) const
inlinenoexcept

Is this body allowed to sleep.

◆ IsSpeedable()

bool box2d::Body::IsSpeedable ( ) const
inlinenoexcept

Is "speedable".

Is this body able to have a non-zero speed associated with it. Kinematic and Dynamic bodies are speedable. Static bodies are not.

◆ IsValid()

bool Body::IsValid ( const Shape shape)
static

◆ ResetMassData()

void Body::ResetMassData ( )

Resets the mass data properties.

This resets the mass data to the sum of the mass properties of the fixtures.

Note
This method must be called after calling CreateFixture to update the body mass data properties unless SetMassData is used.
See also
SetMassData.

◆ SetAcceleration()

void Body::SetAcceleration ( const LinearAcceleration2D  linear,
const AngularAcceleration  angular 
)
noexcept

Sets the linear and rotational accelerations on this body.

Note
This has no effect on non-accelerable bodies.
A non-zero acceleration will also awaken the body.
Parameters
linearLinear acceleration.
angularAngular acceleration.

◆ SetAngularDamping()

void box2d::Body::SetAngularDamping ( RealNum  angularDamping)
inlinenoexcept

Set the angular damping of the body.

◆ SetAwake()

void box2d::Body::SetAwake ( )
inlinenoexcept

Sets this body to awake if it's a "speedable" body.

This sets the sleep state of the body to awake if it's a "speedable" body. It has no effect otherwise.

Postcondition
If this body is a "speedable" body, then this body's IsAwake method returns true.

◆ SetBullet()

void box2d::Body::SetBullet ( bool  flag)
inlinenoexcept

Should this body be treated like a bullet for continuous collision detection?

◆ SetEnabled()

void Body::SetEnabled ( bool  flag)

Set the enabled state of the body. A disabled body is not simulated and cannot be collided with or woken up. If you pass a flag of true, all fixtures will be added to the broad-phase. If you pass a flag of false, all fixtures will be removed from the broad-phase and all contacts will be destroyed. Fixtures and joints are otherwise unaffected. You may continue to create/destroy fixtures and joints on disabled bodies. Fixtures on a disabled body are implicitly disabled and will not participate in collisions, ray-casts, or queries. Joints connected to a disabled body are implicitly disabled. A disabled body is still owned by a World object and remains in the body list.

◆ SetFixedRotation()

void Body::SetFixedRotation ( bool  flag)

Set this body to have fixed rotation. This causes the mass to be reset.

◆ SetLinearDamping()

void box2d::Body::SetLinearDamping ( RealNum  linearDamping)
inlinenoexcept

Set the linear damping of the body.

◆ SetMassData()

void Body::SetMassData ( const MassData data)

Set the mass properties to override the mass properties of the fixtures.

Note
This changes the center of mass position.
Creating or destroying fixtures can also alter the mass.
This function has no effect if the body isn't dynamic.
Parameters
datathe mass properties.

◆ SetSleepingAllowed()

void box2d::Body::SetSleepingAllowed ( bool  flag)
inlinenoexcept

You can disable sleeping on this body. If you disable sleeping, the body will be woken.

◆ SetTransform()

void Body::SetTransform ( const Length2D  position,
Angle  angle 
)

Sets the position of the body's origin and rotation.

This instantly adjusts the body to be at the new position and new orientation.

Warning
Manipulating a body's transform may cause non-physical behavior.
Note
Contacts are updated on the next call to World::Step.
Parameters
positionValid world position of the body's local origin. Behavior is undefined if value is invalid.
angleValid world rotation in radians. Behavior is undefined if value is invalid.

◆ SetType()

void Body::SetType ( BodyType  type)

Set the type of this body. This may alter the mass and velocity.

◆ SetUnderActiveTime()

void box2d::Body::SetUnderActiveTime ( Time  value)
inlinenoexcept

Sets the "under-active" time to the given value.

Sets the "under-active" time to a value of zero or a non-zero value if the body is "accelerable". Otherwise it does nothing.

Warning
Behavior is undefined for negative values.
Note
A non-zero time is only valid for an "accelerable" body.

◆ SetUserData()

void box2d::Body::SetUserData ( void *  data)
inlinenoexcept

Set the user data. Use this to store your application specific data.

◆ SetVelocity()

void Body::SetVelocity ( const Velocity v)
noexcept

Sets the body's velocity (linear and angular velocity).

Note
This method does nothing if this body is not speedable.
A non-zero velocity will awaken this body.
See also
SetAwake.
SetUnderActiveTime.

◆ UnsetAwake()

void box2d::Body::UnsetAwake ( )
inlinenoexcept

Sets this body to asleep if sleeping is allowed.

If this body is allowed to sleep, this: sets the sleep state of the body to asleep, resets this body's under active time, and resets this body's velocity (linear and angular).

Postcondition
This body's IsAwake method returns false.
This body's GetUnderActiveTime method returns zero.
This body's GetVelocity method returns zero linear and zero angular speed.

Friends And Related Function Documentation

◆ BodyAtty

friend class BodyAtty
friend

Member Data Documentation

◆ InvalidIslandIndex

constexpr auto box2d::Body::InvalidIslandIndex = static_cast<body_count_t>(-1)
static

The documentation for this class was generated from the following files: