Skip to content

Useful snippets of Lua code for the Roblox platform.

Notifications You must be signed in to change notification settings

DeltaRager/RbxCookbook

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RbxCookbook

Feel free to submit a pull request with your own contributions!

Snippets

AngleBetween

AngleBetween.lua

-- Find angle between two vectors:

local function AngleBetween(vectorA, vectorB)
	return math.acos(math.clamp(vectorA.Unit:Dot(vectorB.Unit), -1, 1))
end

GammaColorTransition

GammaColorTransition.lua

-- Allows for linear interpolation between two colors using a specified Gamma value.

-- Utility function to apply a power to Color3
local function PowColor3(color, pow)
    return Color3.new(math.pow(color.R, pow), math.pow(color.G, pow), math.pow(color.B, pow))
end

-- Interpolate between 'ColorA' and 'ColorB' by 'Frac' percentage with an optional 'Gamma' value. 
-- Typical gamma values range from 1.8 to 2.2. The default value is 2.0.
local function LerpColor(colorA, colorB, frac, gamma)
    gamma = (gamma or 2.0)
    local ca = PowColor3(colorA, gamma)
    local cb = PowColor3(colorB, gamma)
    return PowColor3(ca:Lerp(cb, frac), 1 / gamma)
end

LinearInterpolation

LinearInterpolation.lua

-- Linear Interpolation (AKA Lerp)
-- Interpolate between 'a' and 'b' by 'x' percentage

local function Lerp(a, b, x)
	return a + ((b - a) * x)
end

Map

Map.lua

-- Remap 'n' from the old range (oldMin, oldMax) to the new range (min, max)

local function Map(n, oldMin, oldMax, min, max)
	return (min + ((max - min) * ((n - oldMin) / (oldMax - oldMin))))
end

ModelCFramer

ModelCFramer.lua

-- SetPrimaryPartCFrame but avoids float errors via caching

-- ExampleSetterFunction = ModelCFramer(workspace.Model)
-- ExampleSetterFunction(CFrame.new(0, 5, 0))


local function ModelCFramer(model)
	local primary = model.PrimaryPart
	local primaryCf = primary.CFrame
	local cache = {}
	for _,child in ipairs(model:GetDescendants()) do
		if (child:IsA("BasePart") and child ~= primary) then
			cache[child] = primaryCf:ToObjectSpace(child.CFrame)
		end
	end
	return function(desiredCf)
		primary.CFrame = desiredCf
		for part,offset in pairs(cache) do
			part.CFrame = desiredCf * offset
		end
	end
end

Reflection

Reflection.lua

-- Finding the reflection vector for an incident vector
-- Arguments made for raycast [Origin, RaycastResult.Position, RaycastResult.Normal]

local function Reflection(origin : Vector3,hitpos : Vector3,normal : Vector3)
    return ((hitpos - origin) - 2*((hitpos - origin):Dot(normal) * normal))
end

RoundNumbers

RoundNumbers.lua

-- Round 'x' to the nearest 'mult'

local function Round(x, mult)
	return math.round(x / mult) * mult
end

About

Useful snippets of Lua code for the Roblox platform.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Lua 61.4%
  • Python 38.6%