Physics Component

The physics component controls all of the physics properties of an entity.

BodyType:

The rigid body type is an enum that is used to determine the type of body to be used. These are based on the Box2D b2BodyType. There are three rigid body types to choose from:

  • STATIC
    • Static bodies cannot be moved by other bodies. All movement must be done by the user.
  • KINEMATIC
    • Similar to a static body, that it is not affected by dynamic bodies; however, kinematic bodies can move. Perfect for moving platforms or walls.
  • DYNAMIC
    • Dynamic bodies are movable and will consume all the affects of forces and other bodies.

Use Example:

-- The use of the BodyType enum can be used as follows.
BodyType.Static 
BodyType.Kinematic
BodyType.Dynamic

-- These are normally used when creating the Physics Attributes. 
-- This will set the body type so the physics system knows how it should react to the body.

PhysicsAttributes:

These are the attributes that make up the Physics Component.

Properties:

PropertyTypeFunctionality
eTypeBodyTypeenum for the type of body to use for the PhysicsComp.
densityfloatThe density of the body. Kg/m^2 .
frictionfloatThe coefficient of friction. Usually between values [0 - 1]
restitutionfloatThe coefficient of restitution. Determines the elasticity or bouncyness of a body. Usually between [0 - 1].
restitutionThresholdfloatThe velocity that a body must be >= before the restitution takes affect.
radiusfloatThe radius of the circle collider. ONLY USED IF THE SHAPE IS A CIRCLE.
gravityScalefloatThe scale at which gravity is applied to the body. It is treated as a multiplier. A gravity scale of 1.0 would be normal gravity, where as 0.0 would be no gravity.
positionvec2The [x, y] coordinates of the body in the world.
scalevec2The scale [x, y] of the collider. Used as a multiplied of the radius or box size.
boxSizevec2The size [x == width, y == height] of the collider. ONLY USED IF THE SHAPE IS A BOX.
offsetvec2The offset from the origin of the collider.
bCircleboolFlag used when creating the body and to used the radius upon creation.
bBoxShapeboolFlag used when creating the body and to used the boxSize upon creation.
bFixedRotationboolFlag that prevents the body from rotating.

Use Example:

-- Create a default Physics Attributes object
local physicsAttribs = PhysicsAttributes()

-- Adjust properties to desired values, properties that are unchanged use default
physicsAttribs.eType = BodyType.Dynamic
physicsAttribs.density = 100.0			-- Set the density to 100 kg/m^2
physicsAttribs.friction = 0.2			-- Set the coefficient of friction to 0.2
physicsAttribs.restitution = 0.1		-- Set the coefficient of restitution to 0.2
physicsAttribs.radius = circle.radius 		-- Use the entity's CircleCollider radius
physicsAttribs.gravityScale = 2.0		-- Multiply the force of gravity by 2.0
physicsAttribs.position = transform.position	-- Use the entity's Transform position
physicsAttribs.scale = transform.scale		-- Use the entity's Transform scale
physicsAttribs.bCircle = true			-- Make the Fixture a circle
physicsAttribs.bFixedRotation = false		-- Allow rotation

PhysicsComp:

The actual physics component that can be added to the entity. The PhysicsComp takes in the PhysicsAttributes in order to create the underlying body, fixtures and set thier initial values. The user cannot access the actual physics body; however, has access to functions that will apply the desired changes to the rigid body.

Functions:

linear_impulse

FunctionDescription
linear_impulse(vec2)Applies an impulse at the position of the body. This will immediately modify the velocity of the rigid body.

Example:

-- Get the physics component from the entity
local physics = entity:get_component(Physics)

-- Apply a linear impulse 
physics:linear_impulse(vec2(10000, 100))

angular_impulse

FunctionDescription
angular_impulse(float)Applies an angular impulse on the rigid body. Takes a float value for the impulse.

Example:

-- Get the physics component from the entity
local physics = entity:get_component(Physics)

-- Apply an angular impulse 
physics:angular_impulse(45)

set_linear_velocity

FunctionDescription
set_linear_velocity(vec2)Sets the linear velocity of the center mass of the rigid body.

Example:

-- Get the physics component from the entity
local physics = entity:get_component(Physics)

-- Move the entity to the right with a velocity of 50 
physics:set_linear_velocity(vec2(50, 0))

get_linear_velocity

FunctionDescription
get_linear_velocity()Returns a vec2 of the current linear velocity of the rigid body.

Example:

-- Get the physics component from the entity
local physics = entity:get_component(Physics)

-- Get the linear velocity
local velocity = physics:get_linear_velocity()

set_angular_velocity

FunctionDescription
set_angular_velocity(float)Sets the angular velocity of the rigid body in radians/second.

Example:

-- Get the physics component from the entity
local physics = entity:get_component(Physics)

-- Set the angular velocity
physics:set_angular_velocity(60)

get_angular_velocity

FunctionDescription
get_angular_velocity()Returns a float for the angular velocity of the rigid body in radians/second.

Example:

-- Get the physics component from the entity
local physics = entity:get_component(Physics)

-- Get the angular velocity or Omega
local omega = physics:get_angular_velocity()

set_gravity_scale

FunctionDescription
set_gravity_scale(float)Sets the gravity scale of the rigid body to increase/decrease the forcec of gravity that will be applied.

Example:

-- Get the physics component from the entity
local physics = entity:get_component(Physics)

-- Set the angular velocity
physics:set_gravity_scale(2.0)