Skip to main content

RigidBody

RigidBodies are formed by Constraints, Points and UI Elements.

Types

type Collision: {
axis: Vector2,
depth: number,
edge: Constraint,
vertex: Point,
}

type RigidBodyStructure: {
-- { Point1: Point, Point2: Point, support: boolean }
{ any }
...
}

Functions

RigidBody.new()

This method is used to initialize a new RigidBody.

  • parameters - frame: GuiObject, collidable: boolean, anchored: boolean, engine: Engine
  • returns - RigidBody

RigidBody:CreateProjection()

NOTE:

This method is automatically handled by the engine!

This method projects the RigidBody on an axis. Used for collision detection.

  • parameters - axis: Vector2, min, max
  • returns - min: number, max: number

RigidBody:DetectCollision()

NOTE:

This method is automatically handled by the engine!

This method detects collision between two RigidBodies.

  • parameters - body: RigidBody
  • returns - { colliding: boolean, collision: Collision }

RigidBody:ApplyForce()

This method is used to apply an external force on the rigid body. The time parameter can be passed in the function to apply the force for a certain time period.

  • parameters - force: Vector2, time: number | nil
  • returns - nil

RigidBody:Update()

NOTE:

This method is automatically handled by the engine!

This method updates the positions of the RigidBody's points and constraints.

  • parameters - dt: number
  • returns - nil

RigidBody:Render()

NOTE:

This method is automatically handled by the engine!

This method updates the positions and appearance of the RigidBody on screen.

  • parameters - none
  • returns - nil

RigidBody:Clone()

This method is used to clone and already existing RigidBody. By default the clone won't inherit the original RigidBody's states, lifespan or filtered RigidBodies. In order to copy those too, pass in the deepCopy parameter as true.

  • parameters - deepCopy: boolean | nil
  • returns - clone: RigidBody

RigidBody:Destroy()

Destroys the rigid body's GuiObject, destroys all its constraints (edges) which subsequently destroys its vertices (points), destroys and disconnects all its connections, and the rigid body is no longer a part of the engine. The body's UI element is destroyed if keepFrame is false or nil.

Destroying the GuiObject of the rigid body via Instance:Destroy() triggers RigidBody:Destroy(). Destroying a vertex of the rigid body via Point:Destroy() triggers RigidBody:Destroy(). Destroying an edge of the rigid body via Constraint:Destroy() triggers RigidBody:Destroy().

  • parameters - keepFrame: boolean | nil
  • returns - nil

RigidBody:Rotate()

This method is used to rotate the RigidBody's UI element. After rotation the positions of its points and constraints are automatically updated.

  • parameters - newRotation: number
  • returns - oldRotation: number, newRotation: number

RigidBody:SetPosition()

This method is used to set a new position of the RigidBody's UI element. After updating the position, the positions of its points and constraints are automatically updated.

  • parameters - PositionX: number, PositionX: number
  • returns - oldPosition: UDim2, newPosition: UDim2

RigidBody:SetSize()

NOTE:

This method is restricted for Custom RigidBodies.

This method is used to set a new size of the RigidBody's UI element. After updating the size, the positions of its points and constraints are automatically updated.

  • parameters - SizeX: number, SizeY: number
  • returns - oldSize: UDim2, newSize: UDim2

RigidBody:SetScale()

NOTE:

This method can only be used for custom rigid bodies!

This method is used to scale a custom rigid body's size. Similar to how UDim/UDim2.Scale works. The scale for any custom rigid body by default is 1.

  • parameters - scale: number
  • returns - nil

RigidBody:Anchor()

This method is used to anchor the RigidBody. Its position will no longer change. It will still be able to collide with other RigidBodies. It will act the same as an anchored BasePart. Rotate(), SetPosition() and SetSize() can still be called.

  • parameters - none
  • returns - nil

RigidBody:Unanchor()

This method is used to unachor and anchored RigidBody.

  • parameters - none
  • returns - nil

RigidBody:CanCollide()

This method is used to determine whether the RigidBody will collide with other RigidBodies. If set to false, collision detection and response will be skipped. Similar to how BasePart.CanCollide functions.

  • parameters - collidable: boolean
  • returns - nil

Engine:CanRotate()

This method is uded to determine whether the RigidBody can be rotated or not. Setting this to false won't allow the RigidBody to change its rotation while still being able to move and collide. Extremely useful for creating platformer games, top-down games, character controllers etc.

  • parameters - canRotate: boolean
  • returns - nil

RigidBody:SetLifeSpan()

This method is used to set the RigidBody's life span. Life span is determined by 'seconds'. After this time in seconds has been passed after the RigidBody is created, the RigidBody is automatically destroyed and removed from the engine.

  • parameters - seconds: number
  • returns - nil

RigidBody:KeepInCanvas()

This method determines if the RigidBody stays inside the engine's canvas at all times. If set to false, the RigidBody will be able to go past the engine's canvas (boundaries).

  • parameters - keepInCanvas: boolean
  • returns - nil

RigidBody:SetFriction()

This method sets a custom frictional damp value just for the RigidBody. The RigidBody no longer abides by the engine's universal friction.

  • parameters - friction: number
  • returns - nil

RigidBody:SetAirFriction()

This method sets a custom air frictional damp value just for the RigidBody. The RigidBody no longer abides by the engine's universal friction.

  • parameters - airfriction: number
  • returns - nil

RigidBody:SetGravity()

This method sets a custom gravitational force just for the RigidBody. The RigidBody no longer abides by the engine's universal gravity.

  • parameters - gravity: Vector2
  • returns - nil

RigidBody:SetMaxForce()

Determines the max force that can be applied to the RigidBody.

  • parameters - maxForce: number
  • returns - nil

RigidBody:SetMass()

This method sets the mass for the RigidBody. By default mass is set to 1.

  • parameters - mass: number
  • returns - nil

RigidBody:SetState()

Used to initialize or update states of a RigidBody

  • parameters - state: string, value: any
  • returns - nil

RigidBody:FilterCollisionsWith()

Used to ignore/filter any collisions with the other RigidBody.

  • parameters - otherRigidBody: RigidBody
  • returns - nil

RigidBody:UnfilterCollisionsWith()

Used to unfilter collisions with the other RigidBody. The two bodies will now collide with each other.

  • parameters - otherRigidBody: RigidBody
  • returns - nil

Fetch Methods

RigidBody:GetFrame()

The RigidBody's UI Element can be fetched using this method.

  • parameters - none
  • returns - UIElement: GuiObject

RigidBody:GetId()

The RigidBody's unique ID can be fetched using this method.

  • parameters - none
  • returns - id: string

RigidBody:GetVertices()

The RigidBody's Points can be fetched using this method.

  • parameters - none
  • returns - points: table

RigidBody:GetConstraints()

The RigidBody's Constraints can be fetched using this method.

  • parameters - none
  • returns - constraints: table

RigidBody:IsInBounds()

Returns true if the RigidBody lies within the boundaries of the canvas, else false.

  • parameters - none
  • returns - isInBounds: boolean

RigidBody:AverageVelocity()

Returns the average of all the velocities of the RigidBody's points.

  • parameters - none
  • returns - velocity: Vector2

RigidBody:GetState()

Used to fetch a state

  • parameters - state: string
  • returns - value: any

RigidBody:GetCenter()

Used to fetch the center position of the RigidBody

  • parameters - none
  • returns - center: Vector2

RigidBody:GetFilteredRigidBodies()

Returns all filtered RigidBodies.

  • parameters - none
  • returns - filtered: table

RigidBody:GetTouchingRigidBodies()

Returns all RigidBodies that are in collision with the current RigidBody.

  • parameters - none
  • returns - bodies: table

Events

RigidBody.Touched

This event is fired the moment a RigidBody collides with another RigidBody. This event returns the unique ID of the other RigidBody and collision information such as the collision axis, depth, edge and collision vertex.


RigidBody.TouchEnded

This event is fired the moment a RigidBody stops colliding with another RigidBody. This event returns the unique ID of the other RigidBody.

  • returns - RigidBodyID: number

RigidBody.CanvasEdgeTouched

This event is fired the moment a RigidBody collides with the engine's canvas' boundary.

  • returns - edgeName: string

Properties

Object GuiObject

The UI element of the RigidBody, if any.

Structure RigidBodyStructure

The point-constraint structure of the RigidBody, if any.

Mass number

The mass of the RigidBody

Collidable boolean

Whether or not the RigidBody will collide with other RigidBodies.

Anchored boolean

Whether or not the RigidBody is anchored.

CanRotate boolean

Whether or not the RigidBody can have angular rotations.

LifeSpan number

The amount of time a RigidBody is part of the engine, if any. After this time is elapsed, the RigidBody is destroyed.

KeepInCanvas boolean

Whether or not the RigidBody will be able to go past its confined bounds.

Gravity Vector2

The gravitational force vector for the RigidBody.

Friction number

The damping value for the RigidBody when it is in collisions with the other RigidBodies.

AirFriction number

The damping value for the RigidBody when it is not in collisions with the other RigidBodies.