Skip to main content

Operations on RigidBodies

RigidBodies have many methods that can help create better simulations. Be sure to check out the RigidBody API.


The Engine:Create("RigidBody", propertyTable) method returns a rigidbody on creation which can be used to perform different actions upon.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Nature2D = require(ReplicatedStorage.Nature2D)

local engine = Nature2D.init(screenGuiInstance)

local newBody = engine:Create("RigidBody", {
Object = UIElement,
Collidable = true,
Anchored = false -- unanchored collidable rigid body.
})

Anchoring and Unanchoring RigidBodies

In order to anchor or unanchor rigid bodies, use the RigidBody:Anchor() or RigidBody:Unanchor() methods, or pass in Anchored property as true when creating a rigid body.


local newBody = engine:Create("RigidBody", {
Object = UIElement,
Collidable = true,
Anchored = true -- anchored collidable rigid body.
})

Using methods:

newBody:Anchor()
newBody:Unanchor()

Rotating, Changing Position and Sizes of the RigidBodies

In order to rotate, change positions and sizes of the RigidBody's UI element, use the RigidBody:Rotate(), RigidBody:SetPosition() and RigidBody:SetSize() methods.

local newBody = engine:Create("RigidBody", { 
Object = UIElement,
Collidable = true,
Anchored = true -- anchored collidable rigid body.
})
newBody:Rotate(45) -- rotate by 45 degrees
newBody:SetPosition(Vector2.new(100, 100))
newBody:SetSize(Vector2.new(100, 150))

You can create cool simulations like this one!

ezgif com-gif-maker (17)

Events

You can use events to perform tasks when something happens. There are 2 events for RigidBodies at present: RigidBody.Touched and RigidBody.CanvasEdgeTouched.

RigidBody.Touched is fired when the RigidBody collides with another RigidBody.

newBody.Touched:Connect(function(otherRigidBodyID) -- id of the rigid body it touched
local other = engine:GetBodyById(otherRigidBodyID)
if other then
other:Destroy() -- destroy the rigid body that touched newBody
end
end)

RigidBody.CanvasEdgeTouched is fired when the RigidBody touches any of the canvas' boundaries.

newBody.CanvasEdgeTouched:Connect(function()
newBody:Destroy() -- destroy newBody if it touches the canvas' boundaries
end)

Fetch Methods

RigidBody:GetFrame() returns the UI element associated with the RigidBody

newBody:GetFrame().BackgroundColor3 = Color3.new(1, 0, 0)

RigidBody:GetId() returns the unique ID for the rigidbody.

RigidBody:GetVertices() & RigidBody:GetConstraints() return a table of 4 points and constraints associated with the RigidBody.

RigidBody:GetTouchingRigidBodies() - returns a table of RigidBodies in collision with the current.

Other

RigidBody:KeepInCanvas() takes in 1 parameter which is a boolean, if false, it allows the rigid body to go past the canvas' boundaries.

RigidBody:CanCollide() takes in 1 parameter which is a boolean, if false, it won't collide with any other rigid body.

RigidBody:SetLifeSpan() takes in 1 parameter, which is time in seconds. The rigidbody is destroyed after the set amount of time is passed.

Also check out other methods in the RigidBody API!