Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Rotating a part around a pivot point?

Asked by 8 years ago

I have tried to make a script where a part get's rotated around a pivot point, but as usual, I'm a skrub who can't get it working.

local testmodel = game.Workspace.TestModel

function rotatemodel(model,center,rotation)
    for i,v in ipairs(model:GetChildren()) do
        v.CFrame=((CFrame.new(center)*rotation)*CFrame.new((v.Position-center)))*(v.CFrame-v.Position)
    end
end

rotatemodel(testmodel,testmodel.PivotPoint.Position,CFrame.Angles(0,math.pi/2,0))

This is why I'm called the most skrubiest person in teh world of roboxia and shcriptin halpers

0
[THIS MAY NOT HELP SOLVING THE SCRIPT!] Don't discourage yourself, you always have worst people around you of whom you don't even know! buoyantair 123 — 8y
1
Self-degradation does not make me pity you. It makes me hate you. I hope this pleases you. User#6546 35 — 8y
0
^ This pleases me a lot. eLunate, your magic just kills others, but to me, it makes me want to kill you! Wait, ignore that :3 TheHospitalDev 1134 — 8y

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

Solution

We'll first need to find the offset of the part to the pivot point. Knowing the part's position and the position of the pivot point, we can do this by taking the inverse of the CFrame returned by the toObjectSpace method.

Now that the offset is known, we can calculate the CFrame by multiplying the pivot CFrame by the desired angle and lastly the offset.

Example:

local pivot = CFrame.new()
local part = game.Workspace.Part
local offset = part.CFrame:toObjectSpace(pivot):inverse() 
local angle = CFrame.Angles(0, math.rad(30), 0)

part.CFrame = pivot*(part.CFrame - part.CFrame.p)*angle*offset

Interpolating Rotation

We will need to use some sort of looping structure in order to interpolate the rotation so that it appears as an animation. I will be using a linear interpolation as it is easiest to implement.

I'll show you two of the ways I like to do this, one dependant on duration and one dependant on speed.

Dependant on Duration:

local pivot = CFrame.new()
local part = game.Workspace.Part
local offset = part.CFrame:toObjectSpace(pivot):inverse() 
local initialRotation = part.CFrame - part.CFrame.p

local firstTick = tick()
local elapsed = 0
local duration = 1
local desiredAngle = 30

while elapsed <= duration do
    local alpha = math.min(1, elapsed/duration)
    local angle = desiredAngle*alpha
    part.CFrame = pivot*initalRotation*angle*offset
    elapsed = tick() - firstTick
end

Dependant on Speed:

local pivot = CFrame.new()
local part = game.Workspace.Part
local offset = part.CFrame:toObjectSpace(pivot):inverse() 
local initialRotation = part.CFrame - part.CFrame.p

local firstTick = tick()
local elapsed = 0
local speed = 16 -- deg/sec
local desiredAngle = 30
local duration = desiredAngle/speed

while elapsed <= duration do
    local alpha = math.min(1, elapsed/duration)
    local angle = desiredAngle*alpha
    part.CFrame = pivot*initalRotation*angle*offset
    elapsed = tick() - firstTick
end
0
It works, but how would I make it so the player can see it? Like an animation? TheHospitalDev 1134 — 8y
0
You would just need to make it a loop that rotates it a certain amount each iteration until you reach your goal angle.  See my edit to see how you might implement that. BlackJPI 2658 — 8y
0
Thanks so much.# TheHospitalDev 1134 — 8y
0
No probem :) BlackJPI 2658 — 8y
Ad

Answer this question