Why use math.rad() for CFrame.Angles? and other CFrame questions

I’m trying to make a third person camera script and I suck at CFrames.

Can anyone explain to me why people say to use .rad() instead of degrees?

When I converted from using degrees to using rad, it only made the rotation sensitivity slower.

Second question, does anyone know how much of a performance hit a popper cam would have if you fired a raycast every RenderStepped to see if there is an object between the camera and the character?

Third question, why is it that in CFrame.Angles(var1,var2,var3), changing var1 will rotate the object in the Y axis and not the X axis?

Fourth question, why does the order you multiply a CFrame change its outcome?

For example, in this desired outcome, the camera rotates around the root part:

Camera.CFrame = CFrame.new(rootPart.Position) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0) * offset


If you move the offset to before you multiply the angles it does this:

Camera.CFrame = CFrame.new(rootPart.Position) * offset  * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)

if you multiply the Y angle before the X angle, moving the mouse horizontally will tilt it up or down slightly:

Camera.CFrame = CFrame.new(rootPart.Position) * CFrame.Angles(math.rad(cameraAngleY), 0, 0) * CFrame.Angles(0, math.rad(cameraAngleX), 0)  * offset

Merging the CFrame.Angles into one angles will do the same thing as changing the order of angle multiplication + it will also make the control for the camera inversed in the Y axis (trying to go up will make the camera go down):

Camera.CFrame = CFrame.new(rootPart.Position) * CFrame.Angles(math.rad(cameraAngleY), math.rad(cameraAngleX), 0) * offset

Last question, in the desired outcome, why is it that even though the y and x axis rotate based on mouse delta, the y axis is a bit more sensitive?

6 Likes

CFrame.Angles uses radians instead of degrees to measure angles. For example if you use a value of 360 in degrees in CFrame.Angles, it will think that this 360 is in radians, which means 20626.48 degrees (probably not what you want). You can of course counter this ‘lots of degrees’ effect by reducing that 360 to a smaller number (e.g. reducing the sensitivity), which is how you can still make it work. But it is a good idea to do everything in terms of radians and with numbers that are in that range (0 - 2pi) so that it’s clearer what’s going on for the scripter.

There shouldn’t be a significant performance impact; raycasts are cheap. I do this fine in my games.

Because 3D rotation is a mathematical nightmare. Check out quaternions if you want to find a good proper to this issue (in a Roblox context, sounds like you should avoid the actual math stuff cause it’s quite advanced). I haven’t paid much attention to learning them myself so I can’t tell you much more. The solution of rotating with (0, X, 0) first and (Y, 0, 0) second will do you fine for most stuff though, so don’t bother learning quaternions unless you need them.

You should move the offset after you rotate, unless you have some way of keeping the correct rotation (which probably isn’t feasible in the short time I thought about it).

Either because of the screen height being bigger than width (smaller screen height, less pixels, so if you’re rotating according to a value that isn’t exclusively pixels e.g. what fraction mouse_movement/screen_size then it will have a higher value than width movement of the same number of pixels), or maybe because camera-related stuff like FoV or something causing distortion again due to height < width (idk, I’m no expert on that, that’s probably wrong).

12 Likes

Thank you, it was very informative.
Although for the fourth question, it’s strange how studio, mobile and tablet have almost the same sensitivity, but in an actual PC game, the sensitivity is higher, even though the studio version is roughly the same resolution/size. Plus, resizing the roblox app on a live PC game does not change the sensitivity.

2 Likes

Interesting, are you sure it isn’t due to window size? Mobile studio and tablet are of course way smaller than a full 1920x1080 window so it makes sense that they would have lower sensitivity if that is the case.

1 Like

When you are setting cameraAngleX and cameraAngleY on PC, you’re likely reading from mouse input which is multiplied by it’s sensitivity. While on Xbox and Mobile you are setting it directly from Joystick / Touch input.

There might be a difference in mouse sensitivty in Studio v.s. an online game. Check the settings menu to be certain about that.

2 Likes

Matrix multiplication. In the UK you cover it around the age of 18 or at university if you’re doing a degree that uses maths.

It’s not too difficult to understand the basics if you write your two CFrames out in matrix form and follow the instructions of multiplying out a 4x4 matrix (each row of the first is multiplied by each column of the second to produce a result at their intersection). Now swap them around, and you can get very different terms if there is any rotation involved.

Each CFrame takes the form:


Where the m values are rotation components.

And multiplying two together:

As a quick proof, there are terms such as a11b11+a12b21+a13b31 in this example which do not exist if you swap the two matrices around. Notice how you can’t find the term a11b11+a21b12+a31b13 anywhere, but it would exist if you swapped the order. And welcome to the world of matrices.

There’s more info here if you have a mathematical background: https://developer.roblox.com/en-us/articles/CFrame-Math-Operations

5 Likes

I completely forgot that I set the sensitivity for mobile and pc differently.

Now the mobile version is closer to live PC sensitivity.

I think you are right about the sensitivity being different in Studio and an online game.

I also found that changing your camera sensitivity in the Roblox menu somehow affects the camera rotation speed even though I never made it to account for the Roblox setting.

It appears setting the sensitivity in Studio to around 2x the amount of a live game makes the sensitivity of both almost the same. Which means something about studio is making my sensitivity half the speed of a live game.

It could be that Studio does not detect mouse DPI, but I turned on hardware mouse instead of software mouse in the Studio settings and it didn’t really do anything.

2 Likes

I don’t think it’s due to window size because making a local server’s window in Studio the same size as a live game doesn’t decrease or increase its sensitivity.

3 Likes