Axmol 3.0.0-89d6d9a
Loading...
Searching...
No Matches
Rigidbody2D Class Reference

A Rigidbody2D affected by physics2d. More...

#include <Rigidbody2D.h>

Public Member Functions

virtual Collider2D * addCollider (Collider2D *collider, bool addMassAndMoment=true)
 Add a collider to body.
void removeCollider (Collider2D *collider, bool reduceMassAndMoment=true)
 Remove a collider from body.
void removeCollider (int tag, bool reduceMassAndMoment=true)
 Remove a collider from body.
void removeAllColliders (bool reduceMassAndMoment=true)
 Remove all colliders.
const Vector< Collider2D * > & getColliders () const
 Get the body colliders.
Collider2D * getFirstCollider () const
 Get the first collider of the body colliders.
Collider2D * getCollider (int tag) const
 get the collider of the body.
virtual void applyForce (const Vec2 &force, const Vec2 &offset=Vec2::zero)
 Applies a continuous force to body.
virtual void resetForces ()
 reset all the force applied to body.
virtual void applyImpulse (const Vec2 &impulse, const Vec2 &offset=Vec2::zero)
 Applies a immediate force to body.
virtual void applyTorque (float torque)
 Applies a torque force to body.
virtual void setVelocity (const Vec2 &velocity)
 Set the velocity of a body.
virtual Vec2 getVelocity ()
 Get the velocity of a body.
virtual void setAngularVelocity (float velocity)
 Set the angular velocity in degrees of a body.
virtual Vec2 getVelocityAtLocalPoint (const Vec2 &point)
 Get the angular velocity of a body at a local point.
virtual Vec2 getVelocityAtWorldPoint (const Vec2 &point)
 get the angular velocity of a body at a world point
virtual float getAngularVelocity ()
 get the angular velocity in degrees of a body
virtual void setVelocityLimit (float limit)
 set the max of velocity
virtual float getVelocityLimit ()
 get the max of velocity
virtual void setAngularVelocityLimit (float limit)
 set the max of angular velocity in degrees
virtual float getAngularVelocityLimit ()
 get the max of angular velocity in degrees
void removeFromWorld ()
 remove the body from the world it added to
PhysicsWorld2D * getWorld () const
 get the world body added to.
Node * getNode () const
 get the node the body set to.
void setEventEnabled (ContactEventBits events, bool enabled)
 Enable or disable one or more event types for this collider.
uint64_t getCategoryBits () const
 Return bitmask of first collider.
void setCategoryBits (uint64_t categoryBits)
 A mask that defines which categories this physics body belongs to.
int getCollisionMaskBits () const
 Return bitmask of first collider.
void setCollisionMaskBits (int maskBits)
 A mask that defines which categories of physics bodies can collide with this physics body.
void setGroup (int groupIndex)
 Set the group of body.
int getGroup () const
 Return group of first collider.
Vec2 getPosition () const
 get the body position.
float getRotation ()
 get the body rotation in degrees.
void setPositionOffset (const Vec2 &position)
 set body position offset, it's the position witch relative to node
const Vec2 & getPositionOffset () const
 get body position offset.
void setRotationOffset (float rotation)
 set body rotation offset in degrees, it's the rotation witch relative to node
float getRotationOffset () const
 set the body rotation offset in degrees
void setDynamic ()
 Sets the body type to dynamic (affected by forces, gravity, collisions).
bool isDynamic () const
 Checks if the body type is dynamic.
void setKinematic ()
 Sets the body type to kinematic (not affected by forces, moved by script/animation).
bool isKinematic () const
 Checks if the body type is kinematic.
bool getBodyType () const
 Get the body type.
void setBodyType (BodyType bodyType)
 Set body type.
float getMass () const
 Get the body mass.
void setMass (float mass)
 Set the body mass Override the body's mass properties.
float getMoment () const
 Get the body moment of rotation inertia.
void setMoment (float moment)
 Set the body moment of rotation inertia.
float getLinearDamping () const
 get linear damping.
void setLinearDamping (float damping)
 Set linear damping.
float getAngularDamping () const
 Get angular damping.
void setAngularDamping (float damping)
 Set angular damping.
bool isAttached () const
 Whether the body is attached to physics world.
CollisionDetectionMode getCollisionDetectionMode () const
 Gets the current collision detection mode.
void setCollisionDetectionMode (CollisionDetectionMode mode)
 Sets the collision detection mode.
void setEnabled (bool enable) override
 Set the enable value.
bool isRotationEnabled () const
 Whether the body can rotation.
void setRotationEnabled (bool enabled)
 Set the body is allow rotation or not.
bool isGravityEnabled () const
 Whether this physics body is affected by the physics world's gravitational force.
void setGravityEnabled (bool enabled)
 Set the body is affected by the physics world's gravitational force or not.
void setGravityScale (float scale)
 Sets the gravity scale factor.
float getGravityScale () const
 Gets the current gravity scale factor.
void setTag (unsigned int tag)
 Set this body's tag.
unsigned int getTag () const
 Get this body's tag.
Vec2 world2Local (const Vec2 &point)
 Convert the world point to local.
Vec2 local2World (const Vec2 &point)
 Convert the local point to world.
float getAngleRadians ()
 Gets angle in randius.
b2BodyId internalHandle () const
 Get the rigid body of box2d.

Static Public Member Functions

static Rigidbody2D * create ()
 Create a rigibody.
static Rigidbody2D * createCircle (float radius, const PhysicsMaterial2D &material=PHYSICS_MATERIAL_2D_DEFAULT, const Vec2 &offset=Vec2::zero)
 Create a body contains a circle.
static Rigidbody2D * createBox (const Vec2 &size, const PhysicsMaterial2D &material=PHYSICS_MATERIAL_2D_DEFAULT, const Vec2 &offset=Vec2::zero)
 Create a body contains a box collider.
static Rigidbody2D * createPolygon (std::span< const Vec2 > points, const PhysicsMaterial2D &material=PHYSICS_MATERIAL_2D_DEFAULT, const Vec2 &offset=Vec2::zero)
 Create a body contains a polygon collider.
static Rigidbody2D * createEdgeSegment (const Vec2 &a, const Vec2 &b, const PhysicsMaterial2D &material=PHYSICS_MATERIAL_2D_DEFAULT)
 Create a body contains a EdgeSegment collider.
static Rigidbody2D * createEdgeBox (const Vec2 &size, const PhysicsMaterial2D &material=PHYSICS_MATERIAL_2D_DEFAULT, const Vec2 &offset=Vec2::zero)
 Create a body contains a EdgeBox collider.
static Rigidbody2D * createEdgePolygon (std::span< const Vec2 > points, const PhysicsMaterial2D &material=PHYSICS_MATERIAL_2D_DEFAULT)
 Create a body contains a EdgePolygon collider.
static Rigidbody2D * createEdgeChain (std::span< const Vec2 > points, const PhysicsMaterial2D &material=PHYSICS_MATERIAL_2D_DEFAULT)
 Create a body contains a EdgeChain collider.

Detailed Description

A Rigidbody2D affected by physics2d.

It can attach one or more colliders. By default, Box2D automatically computes mass and moment of inertia from the collider shapes and the specified density (default material PHYSICS_MATERIAL_2D_DEFAULT, density = 0.1f), using the formula: mass = density * area.

You can also set a body static/dynamic via setDynamic().

Note: Box2D does not allow changing mass or inertia dynamically once created.

Member Function Documentation

◆ create()

Rigidbody2D * create ( )
static

Create a rigibody.

The mass and moment automaticall computed by box2d internal

Returns
An autoreleased Rigidbody2D object pointer.

◆ createCircle()

Rigidbody2D * createCircle ( float radius,
const PhysicsMaterial2D & material = PHYSICS_MATERIAL_2D_DEFAULT,
const Vec2 & offset = Vec2::zero )
static

Create a body contains a circle.

Parameters
radiusA float number, it is the circle's radius.
materialA PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
offsetA Vec2 object, it is the offset from the body's center of gravity in body local coordinates.
Returns
An autoreleased Rigidbody2D object pointer.

◆ createBox()

Rigidbody2D * createBox ( const Vec2 & size,
const PhysicsMaterial2D & material = PHYSICS_MATERIAL_2D_DEFAULT,
const Vec2 & offset = Vec2::zero )
static

Create a body contains a box collider.

Parameters
sizeSize contains this box's width and height.
materialA PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
offsetA Vec2 object, it is the offset from the body's center of gravity in body local coordinates.
Returns
An autoreleased Rigidbody2D object pointer.

◆ createPolygon()

Rigidbody2D * createPolygon ( std::span< const Vec2 > points,
const PhysicsMaterial2D & material = PHYSICS_MATERIAL_2D_DEFAULT,
const Vec2 & offset = Vec2::zero )
static

Create a body contains a polygon collider.

Parameters
pointsPoints is an array of Vec2 structs defining a convex hull with a clockwise winding.
materialA PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
offsetA Vec2 object, it is the offset from the body's center of gravity in body local coordinates.
Returns
An autoreleased Rigidbody2D object pointer.

◆ createEdgeSegment()

Rigidbody2D * createEdgeSegment ( const Vec2 & a,
const Vec2 & b,
const PhysicsMaterial2D & material = PHYSICS_MATERIAL_2D_DEFAULT )
static

Create a body contains a EdgeSegment collider.

Parameters
aIt's the edge's begin position.
bIt's the edge's end position.
materialA PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
Returns
An autoreleased Rigidbody2D object pointer.

◆ createEdgeBox()

Rigidbody2D * createEdgeBox ( const Vec2 & size,
const PhysicsMaterial2D & material = PHYSICS_MATERIAL_2D_DEFAULT,
const Vec2 & offset = Vec2::zero )
static

Create a body contains a EdgeBox collider.

Parameters
sizeThe size contains this box's width and height.
materialA PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
offsetA Vec2 object, it is the offset from the body's center of gravity in body local coordinates.
Returns
An autoreleased Rigidbody2D object pointer.

◆ createEdgePolygon()

Rigidbody2D * createEdgePolygon ( std::span< const Vec2 > points,
const PhysicsMaterial2D & material = PHYSICS_MATERIAL_2D_DEFAULT )
static

Create a body contains a EdgePolygon collider.

Parameters
pointsPoints is an array of Vec2 structs defining a convex hull with a clockwise winding.
materialA PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
Returns
An autoreleased Rigidbody2D object pointer.

◆ createEdgeChain()

Rigidbody2D * createEdgeChain ( std::span< const Vec2 > points,
const PhysicsMaterial2D & material = PHYSICS_MATERIAL_2D_DEFAULT )
static

Create a body contains a EdgeChain collider.

Parameters
pointsA Vec2 object pointer, it contains an array of points.
materialA PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
Returns
An autoreleased Rigidbody2D object pointer.

◆ addCollider()

virtual Collider2D * addCollider ( Collider2D * collider,
bool addMassAndMoment = true )
virtual

Add a collider to body.

Parameters
colliderThe collider to be added.
addMassAndMomentIf this is true, the collider's mass and moment will be added to body. The default is true.
Returns
This collider's pointer if added success or nullptr if failed.

◆ removeCollider() [1/2]

void removeCollider ( Collider2D * collider,
bool reduceMassAndMoment = true )

Remove a collider from body.

Parameters
colliderthe collider to be removed.
reduceMassAndMomentIf this is true, the body mass and moment will be reduced by collider. The default is true.

◆ removeCollider() [2/2]

void removeCollider ( int tag,
bool reduceMassAndMoment = true )

Remove a collider from body.

Parameters
tagThe tag of the collider to be removed.
reduceMassAndMomentIf this is true, the body mass and moment will be reduced by collider. The default is true.

◆ removeAllColliders()

void removeAllColliders ( bool reduceMassAndMoment = true)

Remove all colliders.

Parameters
reduceMassAndMomentIf this is true, the body mass and moment will be reduced by collider. The default is true.

◆ getColliders()

const Vector< Collider2D * > & getColliders ( ) const
inline

Get the body colliders.

Returns
A Vector<Collider2D*> object contains Collider2D pointer.

◆ getFirstCollider()

Collider2D * getFirstCollider ( ) const
inline

Get the first collider of the body colliders.

Returns
The first collider in this body.

◆ getCollider()

Collider2D * getCollider ( int tag) const

get the collider of the body.

Parameters
tagAn integer number that identifies a Collider2D object.
Returns
A Collider2D object pointer or nullptr if no colliders were found.

◆ applyForce()

virtual void applyForce ( const Vec2 & force,
const Vec2 & offset = Vec2::zero )
virtual

Applies a continuous force to body.

Parameters
forceThe force is applies to this body.
offsetA Vec2 object, it is the offset from the body's center of gravity in world coordinates.

◆ applyImpulse()

virtual void applyImpulse ( const Vec2 & impulse,
const Vec2 & offset = Vec2::zero )
virtual

Applies a immediate force to body.

Parameters
impulseThe impulse is applies to this body.
offsetA Vec2 object, it is the offset from the body's center of gravity in world coordinates.

◆ applyTorque()

virtual void applyTorque ( float torque)
virtual

Applies a torque force to body.

Parameters
torqueThe torque is applies to this body.

◆ setVelocity()

virtual void setVelocity ( const Vec2 & velocity)
virtual

Set the velocity of a body.

Parameters
velocityThe velocity is set to this body.

◆ setAngularVelocity()

virtual void setAngularVelocity ( float velocity)
virtual

Set the angular velocity in degrees of a body.

Parameters
velocityThe angular velocity is set to this body.

◆ setEventEnabled()

void setEventEnabled ( ContactEventBits events,
bool enabled )

Enable or disable one or more event types for this collider.

This method allows toggling specific event bits. You can enable or disable a single event or multiple events at once by combining flags.

Parameters
eventsThe event bits to modify.
enabledTrue to enable the specified events, false to disable them.

◆ getCategoryBits()

uint64_t getCategoryBits ( ) const

Return bitmask of first collider.

Returns
If there is no collider in body, return default value.(0xFFFFFFFF)

◆ setCategoryBits()

void setCategoryBits ( uint64_t categoryBits)

A mask that defines which categories this physics body belongs to.

Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions.

Parameters
categoryBitsAn integer number, the default value is UINT64_MAX (all bits set).

◆ getCollisionMaskBits()

int getCollisionMaskBits ( ) const

Return bitmask of first collider.

Returns
If there is no collider in body, return default value.(UINT64_MAX)

◆ setCollisionMaskBits()

void setCollisionMaskBits ( int maskBits)

A mask that defines which categories of physics bodies can collide with this physics body.

When two physics bodies contact each other, a collision may occur. This body's collision mask is compared to the other body's category mask by performing a logical AND operation. If the result is a non-zero value, then this body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might use this to avoid collision calculations that would make negligible changes to a body's velocity.

Parameters
bitmaskAn integer number, the default value is UINT64_MAX (all bits set).

◆ setGroup()

void setGroup ( int groupIndex)

Set the group of body.

Collision groups let you specify an integral group index. You can have all fixtures with the same group index always collide (positive index) or never collide (negative index). It have high priority than bit masks.

◆ getGroup()

int getGroup ( ) const

Return group of first collider.

Returns
If there is no collider in body, return default value.(0)

◆ setMass()

void setMass ( float mass)

Set the body mass Override the body's mass properties.

Normally this is computed automatically using the shape geometry and density. This information is lost if a shape is added or removed or if the body type changes.

◆ setLinearDamping()

void setLinearDamping ( float damping)
inline

Set linear damping.

it is used to simulate fluid or air friction forces on the body.

Parameters
dampingThe value is 0.0f to 1.0f.

◆ setAngularDamping()

void setAngularDamping ( float damping)
inline

Set angular damping.

It is used to simulate fluid or air friction forces on the body.

Parameters
dampingThe value is 0.0f to 1.0f.

◆ setEnabled()

void setEnabled ( bool enable)
override

Set the enable value.

If the body it isn't enabled, it will not has simulation by world.

◆ setGravityScale()

void setGravityScale ( float scale)

Sets the gravity scale factor.

Parameters
scaleMultiplier for global gravity (1.0 = normal gravity, 0 disables gravity).

◆ getGravityScale()

float getGravityScale ( ) const
inline

Gets the current gravity scale factor.

Returns
The multiplier applied to global gravity.

◆ setTag()

void setTag ( unsigned int tag)
inline

Set this body's tag.

Parameters
tagAn integer number that identifies a shape object.

◆ getTag()

unsigned int getTag ( ) const
inline

Get this body's tag.

Returns
An integer number.

The documentation for this class was generated from the following file:
  • Rigidbody2D.h