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

Why do these two CFrame calculations result in different result?

Hello!

local x = 70

script.Parent.CFrame *= CFrame.Angles(math.rad(x),math.rad(x),0)

print(script.Parent.Rotation)

Result: 82.9000015258789, 18.75, -68.83000183105469


local x = 1

for i = 1, 70 do
	script.Parent.CFrame *= CFrame.Angles(math.rad(x),math.rad(x),0)
end

print(script.Parent.Rotation)

Result: 102.52999877929688, 43.900001525878906, -54.16999816894531

Why do these two result in different results, when they're doing the same calculation pretty much?

1 answer

✓ Accepted answer

Applying rotation is not as simple as applying position.

Order in which you apply rotation on each axis matters.

Simple explanation: Imagine You want to rotate Your body 90 degree on X and Y axis.

First scenario - you apply x axis first.

  1. You lay on your back. (90 degree x axis)
  2. You roll right (90 degree y axis) You end up lying on your side facing a wall.

Second scenario - you apply y axis first.

  1. You spin right (90 degree y axis)
  2. You lay on your back (90 degree x axis) You end up lying on your back facing the ceiling.

In your first example, you rotate in x then y axis. (CFrame.Angles is equivalent to fromEulerAnglesXYZ). In second example you apply both axis each step, so the end result will be a mixture of x-y and y-x order rotation.

EDIT: Solution:

Use tween:

TS = game:GetService("TweenService")
STYLE = TweenInfo.new(
 1, --put TOTAL duration here in seconds
 Enum.EasingStyle.Linear)

local tween = TS:Create(script.Parent,STYLE,{CFrame = script.Parent.CFrame * CFrame.Angles(math.rad(x),math.rad(x),0)})
tween:Play()
tween.Completed:Wait() --optional, will pause the script until the tween is finished

4 comments

0

I see, is there any way to fix this? So that I can rotate in both axis without it being weird?

0

(In a loop like the bottom code)^

0

I have edited the answer

0

I am guessing tweenservice saves the starting cframe, and multiplies by an increasing number, avoiding the problem?

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