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

Why do these two CFrame calculations result in different result?

Asked by 2 years ago
Edited 2 years ago

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

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
2 years ago
Edited 2 years ago

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
0
I see, is there any way to fix this? So that I can rotate in both axis without it being weird? ChicaLower 25 — 2y
0
(In a loop like the bottom code)^ ChicaLower 25 — 2y
0
I have edited the answer sleazel 1287 — 2y
0
I am guessing tweenservice saves the starting cframe, and multiplies by an increasing number, avoiding the problem? ChicaLower 25 — 2y
Ad

Answer this question