Skip to content

Instantly share code, notes, and snippets.

@jaipack17
Last active June 6, 2022 13:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaipack17/653c3f5c0a11a6fe0b151aced5ca451a to your computer and use it in GitHub Desktop.
Save jaipack17/653c3f5c0a11a6fe0b151aced5ca451a to your computer and use it in GitHub Desktop.
Rotation Pivots for Guis on Roblox
-- Pivot.lua by jaipack17
-- Last updated: 02/04/2022
-- License: MIT
-- Easily create rotation pivots for GuiObjects on Roblox.
-- The API is similar to the Pivot API available for BaseParts.
-- Pivot.SetAngleMode(angleMode: string) -> Set angle mode for the pivot. Either DEGREES or RADIANS. By default set to DEGREES.
-- Pivot.ApplyIgnoreGuiInsetOffset(apply: boolean) -> Apply Offset of Vector2.new(0, 36) to the center position of the GuiObject.
-- Set this to true if your ScreenGui is ignoring GuiInset.
-- Pivot.new(object: GuiObject, pivotOffset: Vector2, keepPositionUnchanged: boolean, keepRotationUnchanged: boolean) -> Creates a pivot for the GuiObject given a pivot offset relative to the GuiObject's center
-- Pivot:Rotate(angle: number) -> Rotate the GuiObject around its pivot given an angle.
-- Pivot:GetPivotOffset() Vector2 -> Returns the pivot offset for the GuiObject
-- Pivot:GetPivot() Vector2 -> Returns the position of the pivot point on screen
-- Pivot:SetPivotOffset(pivotOffset: Vector2) -> Set a new pivot offset for the GuiObject.
-- Pivot:PivotTo(pivotPosition: Vector2) -> Move the pivot to somewhere else. This moves the GuiObject along with the pivot.
-- Pivot:CenterTo(centerPoisition: Vector2) -> Move the GuiObject to somewhere else. This moves the pivot along with the GuiObject.
local Pivot = {}
Pivot.__index = Pivot
local Settings = {
_angleMode = "DEGREES",
_applyIgnoreGuiInsetOffset = false
}
local function Offset(self)
return (if Settings._applyIgnoreGuiInsetOffset then Vector2.new(0, 36) else Vector2.new())
end
local function GetCenter(self)
return self.Object.AbsolutePosition + self.Object.AbsoluteSize/2 + Offset(self)
end
function Pivot.new(object: GuiObject, pivotOffset: Vector2, keepPositionUnchanged: boolean?, keepRotationUnchanged: boolean?)
local self = setmetatable({
Object = object,
PivotOffset = pivotOffset,
_applyIgnoreGuiInsetOffset = false,
_angleMode = "DEGREES",
_objectCenter = nil,
_pivotPosition = nil,
_overwritePosition = not keepPositionUnchanged,
_overwriteRotation = not keepRotationUnchanged
}, Pivot)
self._objectCenter = GetCenter(self)
self._pivotPosition = self._objectCenter + self.PivotOffset
return self
end
function Pivot.SetAngleMode(mode: string)
mode = mode:upper()
if mode == "DEGREES" or mode == "RADIANS" then
Settings._angleMode = mode
end
end
function Pivot.ApplyIgnoreGuiInsetOffset(apply: boolean)
Settings._applyIgnoreGuiInsetOffset = apply
end
function Pivot:Rotate(angle: number)
if Settings._angleMode == "DEGREES" then
angle = math.rad(angle)
end
local difference = self._objectCenter - self._pivotPosition
local theta = angle + math.atan2(difference.Y, difference.X)
local pos = self._pivotPosition + Vector2.new(math.cos(theta), math.sin(theta)) * difference.Magnitude
local offset = Vector2.new(.5, .5) - self.Object.AnchorPoint
offset *= self.Object.AbsoluteSize
pos -= offset
if self._overwritePosition then
self.Object.Position = UDim2.fromOffset(pos.X, pos.Y)
end
if self._overwriteRotation then
self.Object.Rotation = math.deg(angle)
end
end
function Pivot:GetPivot()
return self._pivotPosition
end
function Pivot:GetPivotOffset()
return self.PivotOffset
end
function Pivot:SetPivotOffset(pivotOffset: Vector2)
self.PivotOffset = pivotOffset
self._objectCenter = GetCenter(self)
self._pivotPosition = self._objectCenter + self.PivotOffset
end
function Pivot:CenterTo(centerPosition: Vector2)
self._objectCenter = centerPosition
self._pivotPosition = self._objectCenter + self.PivotOffset
end
function Pivot:PivotTo(pivotPosition: Vector2)
self._pivotPosition = pivotPosition
self._objectCenter = self._pivotPosition - self.PivotOffset
end
return Pivot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment