mooonc0w
(Sauron)
August 2, 2022, 1:41pm
#1
Hello
I have a Part, which I want to rotate around its own Pivot point in the World Y axis.
(like I can rotate it in Studio by a single World axis)
The following code does not produce the desired effect:
part:PivotTo(part:GetPivot()*CFrame.Angles(0, math.rad(angleNew), 0))
Can you recommend me a better approach?
Thank you in advance
You can do this, following the second option.
To rotate by world axis without any local effects, but whilst maintaining the position of the part, you’ll need to extract the Euler angles and simply add the world rotation onto it, then reconstruct a rotational CFrame from those Euler angles, and finally add the position back into it. I use Rx, Ry and Rz to represent the angle you want to turn it through in world space.
local rx, ry, rz = Part.CFrame:ToEulerAnglesYXZ()
Part.CFrame = CFrame:FromEulerAnglesYXZ( rx + Rx, ry + Ry, rz + Rz ) + Par…
1 Like
mooonc0w
(Sauron)
August 2, 2022, 3:13pm
#3
My pivot point is not at the center of the part, but at one of the Part sides, so I need to include somehow the existing Part Pivot…
When I rotate around the Y axis in the studio, in addition to the rotation on Y, there is a change in position in X and Z.
Not sure how to achieve this effect in the code
Forummer
(Forummer)
August 2, 2022, 3:41pm
#4
Part.CFrame = CFrame.new(Part.Position) * CFrame.Angles(0, math.pi, 0)
This would rotate a part 180 degrees around the Y axis according to world space.
mooonc0w
(Sauron)
August 2, 2022, 4:04pm
#5
This does not take into account Part’s pivot point?
Pretty sure it should be something like this, not sure what you tried out via code.
part:PivotTo((CFrame.Angles(0, math.rad(angleNew), 0)*part:GetPivot().Rotation + part:GetPivot().Position)
Then you might need to add an offset CFrame or a dummy CFrame, similar to the tweening door example
This was before Pivot points were added, not sure if there is a simplified version now.
Oh yeah good point, the article in understanding CFrame math operations requires that you need to change two CFrame values at once in order to rotate around a hinge.
hinge.CFrame = hinge.CFrame * CFrame.Angles(0, math.rad(1)*dt*60, 0) -- rotate the hinge
door.CFrame = hinge.CFrame * offset -- apply offset to rotated hinge
Luckily theres a trick I did in another post with a helmet which gets around this issue by creating an CFrame Instance value to change both the hinge CFrame and the CFrame …
2 Likes
Zik_isi
(Zik)
June 11, 2023, 5:26pm
#7
Thanks, that helped alot with my placement system! (I wondered how to replicate the rotation from Lumber Tycoon 2 with only two buttons)
1 Like