Rigidbody2D Component

The Rigidbody2D class is a component in Tsar engine used for simulating an object's position and rotation in 2D space based on its mass, drag and gravity.


Class Declaration

class Rigidbody2D : public Component

This class inherits from the Component class and adds functionality related to body properties required for physics simulation.


Public Methods


float GetRotationalInertia()

Get angular inertia of body relative to Z axis.


glm::vec2 GetPointInWorld(const glm::vec2& pointLocal)

Get position of a point in world relative to body origin.


glm::vec2 GetPointLocal(const glm::vec2& pointInWorld)
Get position of a point relative to body origin from world coordinates/position.


glm::vec2 GetVectorLocal(const glm::vec2& worldVector)
Get vector/direction relative to body from world vector.


glm::vec2 GetVectorWorld(const glm::vec2& localVector)
Get world vector/direction from local body vector.


glm::vec2 GetPosition()
Get world position of body.


void SetPosition(const glm::vec2& posInWorld)
Set world position of body. This acts as teleport and is fairly expensive


float GetRotation()
Get rotation of body along Z axis, in degrees


float GetRotationRad()
Get rotation of body along Z axis, in radians


void SetRotation(float rotationDegrees)
Set rotation of body along Z axis, in degrees. This acts as teleport and is fairly expensive


glm::vec2 GetCenterOfMassLocal()
Get center of mass relative to origin/local offset.


glm::vec2 GetCenterOfMassWorld()
Get center of mass in world coordinates.




void ApplyLinearImpulse(const glm::vec2& impulse, const glm::vec2& point, bool wake = true)

Apply impulse, this changes velocity of body immediately.
This should be used for one-shot impulses. If you need a steady force, use a force instead, which will work better with the sub-stepping solver.
The impulse is ignored if the body is not awake.

  • impulse - direction in world space where impulse is applied. Horizontal(X) and vertical(Y) forces are in Newton-seconds or kg*m/s. If we apply impulse of {0.0, 2.0f} to body with mass 1kg it will start moving upward with addidtional velocity of 2m/s.
  • point - point in world where the force/impulse is to be applied, if not at center of body/mass this will produce angular velocity.
  • wake - should the body be awaken, default is true.


void ApplyLinearImpulseToCenter(const glm::vec2& impulse, bool wake = true)
Apply impulse at center of mass, this immediately changes linear velocity of body.
This should be used for one-shot impulses. If you need a steady force, use a force instead, which will work better with the sub-stepping solver.
The impulse is ignored if the body is not awake.

  • impulse - direction in world space where impulse is applied. Horizontal(X) and vertical(Y) forces are in Newton-seconds or kg*m/s. If we apply impulse of {2.0, 2.0f} to body with mass 1kg it will start moving upward and right with addidtional velocity of 2m/s horizontaly and verticaly, or diagonally 2√2m/s.
  • wake - should the body be awaken, default is true.


void ApplyForce(const glm::vec2& force, const glm::vec2& point, bool wake = true)
Apply force at world point, this changes linear velocity of body over time.
The force is ignored if the body is not awake.

  • force - The world force vector, usually in newtons (N).
  • point - The world position of the point of application, If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity.
  • wake - Option to wake up the body.


void ApplyForceToCenter(const glm::vec2& force, bool wake = true)
Apply force at body center of mass, this changes linear velocity of body over time.
The force is ignored if the body is not awake.

  • force - The world force vector, usually in newtons (N).
  • wake - Option to wake up the body.


void ApplyTorque(float torque, bool wake = true)
Apply torque at body center of mass, this changes angular velocity of body over time without affecting linear velocity.
The torque is ignored if the body is not awake.

  • torque - Torque along Z-axis, usually in N*m.
  • wake - Option to wake up the body.


void ApplyAngularImpulse(float impulse, bool wake = true)
Apply torque at body center of mass, this changes angular velocity of body immediately without affecting linear velocity.
This should be used for one-shot impulses. If you need a steady force, use a force instead, which will work better with the sub-stepping solver.
The torque is ignored if the body is not awake.

  • impulse - the angular impulse, usually in units of kgmm/s
  • wake - Option to wake up the body.

Properties

The Rigidbody2D class exposes properties (that can also be accessed via getter and setter functions). This allows users to manipulate all properties of rigid body in a more intuitive manner:


RigidBody2DType Type : static, dynamic, kinematic

  • Get: GetBodyType()
  • Set: SetBodyType(RigidBody2DType type) Note that this is to be used when creating a body during runtime (once, right after creation), changing the body type during runtime is bad design choice(doesn't work).

bool IsFixedRotation
Is rotation along Z axis changed by simulation?

  • Get: GetFixedRotation()
  • Set: SetFixedRotation(bool fixedRotation)

glm::vec2 Velocity
Linear velocity(speed) of body in m/s.

  • Get: GetLinearVelocity()
  • Set: SetLinearVelocity(const glm::vec2& velocity)

float LinearDamping
How much the body resist moving, normally in range [0; 1]. 0 meaning no resistance(floaty like in space), 1 meaning rapid slow down(like in honey or glue).

  • Get: GetLinearDamping()
  • Set: SetLinearDamping(float linearDamping)

float Mass
Mass of body, in kilograms by default.

  • Get: GetMass()
  • Set: SetMass(float mass) Should be positive number, avoid close to zero or negative!

float AngularVelocity
Speed at which the body is rotating in radians per second or half rotations per second(1rad=180deg).

  • Get: GetAngularVelocity()
  • Set: SetAngularVelocity()

float AngularDamping
Resistance of body to rotation, normally in range [0; 1]. 0 meaning no resistance, 1 meaning quick rotation slow down over time.

  • Get: GetAngularDamping()
  • Set: SetAngularDamping()

float GravityScale
Multiplier for affection by gravity, default is 1.0. 0.0 means body is not affected by gravity, 2.0 means two times stronger gravity.

  • Get: GetGravityScale()
  • Set: SetGravityScale()

float SleepThreshold
Velocity below which the body is put to sleep to improve performance.

  • Get: GetSleepThreshold()
  • Set: SetSleepThreashold()

bool Bullet
Bullet is used for more precise calculations, mainly to avoid tunneling at high speeds. Use for fast moving objects as its slightly more expensive.

  • Get: IsBullet()
  • Set: SetBullet()

bool Sleep
Enable/Disable sleeping. Sleeping improves performance when body is not moving.

  • Get: IsSleepingEnabled()
  • Set: EnableSleep()

bool Awake
Is body being simulated/awake?.

  • Get: IsAwake()
  • Set: SetIsAwake()

bool SensorEvents
Sensor events are triggered when overlap happens. Sensor shapes are like hollow detectors as they don't have collisions. Enter and Exit are up to you to implement, see events in scripting

  • Get: IsSensorEventsEnabled()
  • Set: EnableSensorEvents()

bool CollisionEvents
Collision events are triggered when two shapes begin touching. Enter and Exit callbacks are up to you to implement, see events in scripting

  • Get: IsCollisionEventsEnabled()
  • Set: EnableCollisionEvents()

bool HitEvents
Hit events are triggered when collision happens with speed/velocity higher than HitEventThreshold. Hit events require collision events to be enabled. Enter and Exit are up to you to implement, see Scripting-Events

  • Get: IsSensorEventsEnabled()
  • Set: EnableSensorEvents()