A Body
is an Object representing a Physical Body in a World
.
Bodies have position, velocity, mass, an optional collider shape, and many other properties that are used in a World
simulation.
Static variables
Constructor
new(?options:Null<BodyOptions>)
Creates a new Body.
Parameters:
options | Optional values to configure the new Body |
---|
Variables
acceleration:Vector2 = new Vector2(0, 0)
A measure of how fast a Body
will change it's velocity.
Can be thought of the sum of all external forces on an object during a step.
active:Bool
Flag to set if the Body is active and will participate in a World's Physics calculations or Collision querys.
collided:Bool
Flag to check if the Body collided with something during the step. Used for debug drawing.
dirty:Bool
Flag to check if the Body has changed its position, rotation, or shape colliders. Used for Collision optimization.
drag:Vector2 = new Vector2(0, 0)
A measure of how fast a Body will move its velocity's x and y components towards 0, when there is no acceleration.
Note: this is calculated separately from a Body's drag_length
, so be careful when applying both.
drag_length:Float
A measure of how fast a Body will move its velocity towards 0 along the velocity's length, when there is no acceleration.
Note: this is calculated separately from a Body's drag
, so be careful when applying both.
elasticity:Float
Value to determine how much of a Body's velocity
should be retained during collisions (or how much should the Body
"bounce", in other words).
gravity_scale:Float
Percentage value that represents how much a World's gravity affects the Body.
read onlyinverse_mass:Float
Cached value of 1 divided by the Body's mass. Used in Internal calculations.
kinematic:Bool
Flag to set how a Body is affected by Collisions.
If set to true, the Body will still Collide and move through the world, but it will not be moved by external collision forces. This is useful for things like moving platforms.
layer_mask:BitMask = new BitMask()
Collision layers that this Body will collide with. Combine with layers
to filter collisions between layers.
Note: a maximum of 32 layers are supported.
layers:BitMask = new BitMask()
Collision layers that this Body belongs to. Combine with layer_mask
to filter collisions between layers.
Note: a maximum of 32 layers are supported.
mass:Float
The Body's mass. Affects how the Body reacts to Collisions and Acceleration Forces. The higher a Body's mass, the more resistant it is to those forces.
If a Body's mass is set to 0
, it becomes static - unmovable by forces and collisions.
max_rotational_velocity:Float
The maximum rotational velocity range that a Body
can have.
If set to 0, the Body has no restrictions on how fast it can rotate.
max_velocity:Vector2 = new Vector2(0, 0)
The maximum values a Body's velocity's x and y components can be. If set to 0, the Body has no restrictions on how fast it can move.
Note: this is calculated separately from a Body's max_velocity_length
, so be careful when applying both.
max_velocity_length:Float
The maximum velocity that a Body
can have along the velocity's length. If set to 0, the Body has no restrictions on how fast it can move.
Note: this is calculated separately from a Body's max_velocity
, so be careful when applying both.
on_move:Null<(Float, Float) ‑> Void>
If set, this method is called whenever the Body's X or Y changes.
torque:Float = 0
A measure of how fast a Body
will change it's rotational velocity.
Can be thought of the sum of all external rotation forces on an object during a step.
transform:Transform = new Transform()
Structure to help perform matrix calculations for the Body's position, rotation, and scale.
Methods
inlineadd_shape(shape:Shape, position:Int = -1):Shape
Adds a Shape
to the Body.
If mass
has not been manually set, It's recommended to call calculate_mass()
after adding/removing a Body's shapes.
Parameters:
shape | |
---|---|
position | The position in the Body's |
Returns:
The added Shape
.
bounds(?aabb:AABB):AABB
If a Body has shapes, it will return an AABB
representing the bounds of the Body's shapes relative to its position. If the Body does not have any shapes, this will return `null'.
Parameters:
aabb | Optional |
---|
Returns:
Null
inlinecalculate_mass():Void
Calculates the Body's mass based on the volume of it's shapes and the density
defined in it's Material
.
This should be called whenever a Body's overall shape is changed, such as in cases of changing the Body's scale - or if an individual shape on the Body has been added, removed, moved, rotated, or scaled.
inlineclear_shapes():Void
Clears all Shapes from the Body, releasing them to their respective pools.
If mass
has not been manually set, It's recommended to call calculate_mass()
after adding/removing a Body's shapes.
inlinecreate_shape(options:ShapeOptions, position:Int = -1):Shape
Adds a new Shape
to the Body based on the ShapeOptions
passed in.
If mass
has not been manually set, It's recommended to call calculate_mass()
after adding/removing a Body's shapes.
Parameters:
options | |
---|---|
position | The position in the Body's |
Returns:
The newly created Shape
.
dispose():Void
Disposes the Body. DO NOT use the Body after disposing it, as it could lead to null reference errors.
get_position(?vec2:Vector2):Vector2
inlineis_dynamic():Bool
Checks if the Body is Dynamic (if it's mass is greater than 0).
Returns:
body.mass > 0
inlineis_static():Bool
Checks if the Body is Static (if it's mass is equal to 0).
Returns:
body.mass == 0
load_options(?options:Null<BodyOptions>):Void
push(x:Float = 0, y:Float = 0, forward:Bool = false, force_type:ForceType = ACCELERATION):Void
Adds forces to a Body.
Options are available to apply the forces relative to the Body's forward, or to configure whether the forces are applied to the Body's acceleration, velocity, or position.
Parameters:
x | |
---|---|
y | |
forward | Set as |
force_type | Determines whether the forces are applied to the Body's acceleration, velocity, or position. |