-
Notifications
You must be signed in to change notification settings - Fork 1
🧩 Rigidbody2D
The Rigidbody2D component provides a 2D physics body for a GameObject, enabling interaction with the physics simulation.
It integrates the object into the Box2D world and controls physical properties such as density, friction, restitution, damping, and velocity.
A Rigidbody2D can be defined as dynamic, static, or kinematic, making it suitable for a wide range of use cases: characters, platforms, projectiles, sensors, or static colliders.
Forces, impulses, and torques can be applied at runtime, and the component automatically keeps the associated GameObject’s transform in sync with the physics world.
The Rigidbody2D component integrates a GameObject into the 2D physics simulation. It automatically synchronizes the GameObject’s transform with the physics body, ensuring that position and rotation reflect the physics world. Forces, impulses, and torques applied through the component directly affect motion, velocity, and rotation. Changes to physical properties such as density, friction, restitution, or damping immediately influence how the body interacts with collisions and other Rigidbody2D instances. The body type (dynamic, static, kinematic) determines whether it responds to physics forces, is immovable, or can be controlled manually while still affecting other bodies.
-
float density: Mass density of the body. Default is 10.0. -
float friction: Friction coefficient of the body. Default is 1.0. -
float restitution: Bounciness of the body. Default is 0.0. -
float restitutionThreshold: Minimum velocity required for restitution to take effect. Default is 0.0. -
BodyType bodyType: Type of body (static_body,dynamic_body,kinematic_body, ornone_body). -
float radius: Radius of the body (if applicable). -
b2Body* body: Pointer to the Box2D body. Null if body hasn’t been created yet. -
b2Fixture* fixture: Pointer to the Box2D fixture associated with the body. -
ContactListenerPtr contactListener: Optional contact listener for collision callbacks. -
B2WorldPtr world: Reference to the physics world the body belongs to. -
ShapePtr shape: Shape used to create the body. -
Vector2 shapePos: Local position of the shape relative to the body.
Rigidbody2D()Creates a new Rigidbody2D instance. No body is created until CreateBody is called.
~Rigidbody2D()Destroys the underlying physics body if it exists.
void CreateBody(ShapePtr shape, BodyType bodyType = BodyType::dynamic_body, bool fixedRotation = false, MaskType maskType = MaskType::mask_1)Creates and attaches a physics body to the component using the provided shape and parameters.
Parameters:
-
shape: Shape of the body (rectangle, circle, polygon). -
bodyType: The type of body (dynamic, static, kinematic). -
fixedRotation: Whether the body should ignore rotation. -
maskType: Collision mask category.
void DestroyBody()Destroys the currently attached physics body, if any.
float GetDensity()
void SetDensity(float density)Gets or sets the density of the body. Updating density recalculates the mass.
float GetFriction()
void SetFriction(float friction)Gets or sets the surface friction coefficient.
float GetRestitution()
void SetRestitution(float restitution)Gets or sets the restitution (bounciness).
float GetRestitutionThreshold()
void SetRestitutionThreshold(float restitutionThreshold)Gets or sets the minimum relative speed at which restitution is applied.
float GetInertia()Returns the rotational inertia of the body.
float GetGravityScale()Returns the gravity scaling factor applied to this body.
void ApplyForce(Vector2 force, bool wake = true)Applies a continuous force to the body’s center.
void ApplyLinearForce(Vector2 force, bool wake = true)Applies an instantaneous linear impulse to the body’s center.
void ApplyAngularImpulse(float impulse)Applies an angular impulse, instantly changing angular velocity.
void ApplyTorque(float torque, bool wake = true)Applies continuous torque to the body.
void SetLinearVelocity(Vector2 velocity)
Vector2 GetLinearVelocity()Sets or gets the linear velocity.
void SetVerticalVelocity(float vy)Sets the vertical velocity, keeping horizontal unchanged.
void SetHorizontalVelocity(float vx)Sets the horizontal velocity, keeping vertical unchanged.
void SetAngularVelocity(float va)Sets the angular velocity.
void SetAngularDamping(float ad)Sets the angular damping factor (slows down rotation over time).
void SetLinearDamping(float ld)Sets the linear damping factor (slows down movement over time).
float GetAngle()
void SetAngle(float angle)Gets or sets the body’s rotation angle in radians.
Vector2 GetPosition()Returns the current world position of the body.
void SetPosition(Vector2 position, bool awake = true)
void SetPosition(float x, float y, bool awake = true)Moves the body to the given position.
void SetRotation(float angle, bool awake = true)Rotates the body to the specified angle.
void SetFixedRotation(bool flag)Enables or disables rotation for the body.
BodyType GetBodyType()
void SetBodyType(BodyType bodyType)Gets or sets the type of the body (dynamic, static, kinematic).
ShapeType GetShapeType()Returns the type of the shape used by the body.
void SetTag(String tag)
String GetTag()Assigns or retrieves a string tag for the body, useful for categorization or identification.