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

Why is ToWorldSpace required in basic CFrame rotation?

Asked by 4 years ago

Hello all.

I'm a beginner scripter and have just started touching on CFrames today. I've been using the Roblox Wiki as my guide and so far I've been keeping up with what's going on.

I've just read this page here (Understanding CFrames) and done all the excercises twice. I understand all except one thing; Dynamic CFrame Orientation (as the Wiki puts it), or more specifically, the ToWorldSpace CFrame function when used in simple rotation.

In this example, the Wiki provides you with this code which is supposed to point the front face of the red cube towards the blue cube, and then rotate the red cube so its top face is pointing towards the blue cube:


local redBlock = game.Workspace.RedBlock local blueCube = game.Workspace.BlueCube -- Create a Vector3 for the target position local targetPosition = blueCube.Position -- Point the red block's front surface at 'targetPosition' redBlock.CFrame = CFrame.new(redBlock.Position, targetPosition) -- Rotate red block's CFrame relative to itself so that its top surface (not front) points toward the target local rotatedCFrame = CFrame.Angles(math.rad(-90), 0, 0) redBlock.CFrame = redBlock.CFrame:ToWorldSpace(rotatedCFrame)

Everything here makes sense to me, except for line 12:

redBlock.CFrame = redBlock.CFrame:ToWorldSpace(rotatedCFrame)

My thoughts were that it would be something like this:

redBlock.CFrame = CFrame.new(rotatedCFrame)

I have of course tried this, and as I expected, it didn't work. However, in the original code (which works), I don't see why ToWorldSpace is required if you aren't using relative rotation (because you are rotating the red cube in the last 2 lines, nothing to do with the blue one).

So to summarise, I don't understand why you're using relative rotation for a rotation which relies only on itself. I thought relative rotation was rotating something relative to another thing.

Thank you in advance for you answer/help.

2 answers

Log in to vote
1
Answered by 4 years ago

Okay so, basicly toWorldSpace does the same as:

CFrame * CFrame

It adds a CFrame into CFrame and turn it into one.


Now, reviewing your code:

local rotatedCFrame = CFrame.Angles(math.rad(-90), 0, 0) 
redBlock.CFrame = redBlock.CFrame:ToWorldSpace(rotatedCFrame)

At the first line, you're setting a CFrame angle with a variable. At second line, you're adding that CFrame angle to block's cframe. So, block's cframe will be: Block's position + CFrame angle.

(IMPORTANT: Multiplying CFrames is adding them, it's not really multiplying them.)

In order to get a block rotation relative to another block position, i recommend learning trigonometry.

If this helped, please click the answer button.

0
I get the gist of what you're saying. Thanks for the answer! CaptaiinNoob 52 — 4y
0
No problem. User#27525 1 — 4y
0
It's a bit complicated to explain this kind of thing. User#27525 1 — 4y
Ad
Log in to vote
0
Answered by
Gingerely 245 Moderation Voter
4 years ago









Answer this question