Hi, I’m just looking for a quick snippet of code. This does not need to be a continuous revolution.
I have a part and an arbitrary position. Say I wanted to rotate the part 90 degrees clockwise around that position using CFrame. What does the code look like for that?
Thank you for well-formatted good effort here!
Unfortunately, this does not actually do what was requested. Your code moves the part to the base position and rotates it.
Perhaps a little clarification will help.
Say you have a satellite orbiting earth with some prior orientation. When it moves around the earth, its orientation relative to the earth and its distance from earth stays the same. This is the behavior I want. Also consider F3X’s rotate tool by LAST.
Interesting question, here is a function which seems to work at any center of rotation, any axis of rotation, and any rotation amount continuous or not:
local part = script.Parent.HumanoidRootPart
local function rotateCFAroundPoint(centerOfRotation : Vector3, normalAxis, angle, currentCF)
local rotationCFrame = CFrame.fromAxisAngle(normalAxis, angle)
local newRotation = (rotationCFrame*currentCF).Rotation
local radiusOfRotation = currentCF.Position-centerOfRotation
local newPositionCF = rotationCFrame*(currentCF-centerOfRotation)+centerOfRotation
return newRotation + newPositionCF.Position
end
local centerOfRotation = Vector3.new(0,0,10)
local rotationAxis = Vector3.yAxis
local center = Instance.new("Part")
center.Position = centerOfRotation
center.Anchored = true
center.Size = Vector3.new(5,5,5)
center.BrickColor = BrickColor.Red()
center.Parent = workspace
local total = 0
while true do
local dt = task.wait()
centerOfRotation = center.Position
local incrementAngle = dt --1 Radians per second rotation
part.CFrame = rotateCFAroundPoint(centerOfRotation, rotationAxis, incrementAngle, part.CFrame)
end
found it from my knowledge that multiplying a rotation CFrame before the parts CFrame generates a orbital motion effect:
From the above code generating orbital motion in the y axis (because CFrame.Angles(x,y,z))
local rotation = CFrame.Angles(0,increment,0)*part.CFrame
I experimented a lot to accomodate a new center of rotation other than the origin (0,0,0) which the above code generated and somehow found this:
local newPositionCF = rotationCFrame*(currentCF-centerOfRotation)+centerOfRotation