Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Rotating a part around a pivot point?

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

3 comments

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!

1

Self-degradation does not make me pity you. It makes me hate you. I hope this pleases you.

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

1 answer

✓ Accepted answer

###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

4 comments

0

It works, but how would I make it so the player can see it? Like an animation?

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.

0

Thanks so much.#

0

No probem :)

This discussion is preserved as it was. New answers, comments, and votes are closed.