Help with CFrames

Could someone give me a hand with this? How can I do operations with cframe to change the position and or the size of objects?

I already read the documentation and saw the posts in the forum and I still don’t understand how this works

-- Change camera position and size
local camera = workspace.CurrentCamera
camera.CFrame = camera.CFrame * CFrame.Angles(500,500,0)
camera.CFrame = camera.CFrame * CFrame.new(500,500,0)
camera.CFrame = camera.CFrame * CFrame.lookAt(3,3,3)
camera.CFrame = camera.CFrame * math.clamp(3,3,3)

-- Change position and size of part
local part = game.Workspace.Part
part.CFrame = part.CFrame * CFrame.Angles(500,500,0)
part.CFrame = part.CFrame * CFrame.new(500,500,0)
part.CFrame = part.CFrame * CFrame.lookAt(3,3,3)
part.CFrame = part.CFrame * math.clamp(3,3,3)

I would like to learn these that I have put in code, I would really appreciate it very much :frowning:

1 Like

The CFrames can’t change the size of an object.

CFrame is just a matrix of position and rotation. You cant change the size.

A matrix must be multiplied by another matrix.

3 Likes

Size is a Vector3 not a CFrame.

Cameras do not have a Size.

CFrame.lookAt takes in 3 Vectors not 3 numbers. The first Vector is where the CFrame will be Positioned at. The second Position is where it will be looking at and the third Vector represents the upwards direction of the CFrame(optional)

local at = Vector3.new(10,10,0)
local lookAt = Vector3.new()

Camera.CFrame = CFrame.lookAt(at,lookAt) -- looks at center of world.

math.clamp(3,3,3) math.clamp will return 1 number. Not a Vector or a CFrame. In effect you are doing

part.CFrame = part.CFrame * 3
2 Likes

I understand … so should I do something like this to make it work according to operations?
But I don’t understand the math.Clamp what does it do exactly?

local part = game.Workspace.Part
task.wait(10)
part.CFrame = part.CFrame + Vector3.new(10,10,10)
task.wait(10)
part.CFrame = part.CFrame - Vector3.new(10,10,10)
task.wait(10)
part.CFrame = part.CFrame * CFrame.Angles(30,30,30)
task.wait(10)
part.CFrame = part.CFrame * CFrame.new(30,30,30)
1 Like

damn i need to start learning CFrames

4 Likes

CFrame, short for Coordinate Frame, mark a specific point in space, with a position and a rotation. For example,

Part.CFrame = CFrame.new(50, 0, -10)

Gives the same result as

Part.Position = Vector3.new(50, 0, -10)

Only, with CFrames, because they have a rotation as well as a position, you can do stuff like

Part.CFrame = CFrame.new(50, 0, -10) * CFrame.Angles(0, math.rad(180), 0)

Which turns the part around by 180 degrees. It’s equivalent code without CFrames would be

Part.Position = Vector3.new(50, 0, -10)
Part.Orientation = Vector3.new(0, 180, 0)

So, you may ask, “Why do we use them? I could do the exact same thing with Vector3s”, and this is where really useful stuff comes in. For example;

Part1.CFrame = CFrame.lookAt(Part1.Position, Part2.Position)

This makes Part1 “look” towards Part2, which can be really helpful at times.

PS: math.clamp “clamps” the given number to the given bounds

local Number1 = math.clamp(5, -2, 2) --Number1 will be set to 2
local Number2 = math.clamp(-100, -2, 2) --Number2 will be set to -2

PSS: CFrames can’t be used for setting size, you can only use Vector3s for that.

1 Like

Thank you for your comments!
the cframe in this part is multiplying the cframe by the angles?

Would math.Clamp() be useful for cases where I want to leave a position fixed or something like that? How could I use it with the camera?

2 Likes

Yes, it’s basically adding them together.

If you wanted to lock the camera to a specific area, you could use math.clamp. It has a lot of other uses as well, such as stopping a timer from going into the negatives, if that’s a problem you’re facing (though ideally it wouldn’t be happening in the first place).

If you’re wondering,
math.clamp(Number, MinimumItCanBe, MaximumItCanBe)

2 Likes

For example if I want to lock the camera or know when it is in this specific position, could I do something like this?

-- Lock the camera
local camera = workspace.CurrentCamera
camera.CFrame = camera.CFrame * math.clamp(180, 2, 8)

local isCameraLock = camera.CFrame

-- Know if the camera is at a specific point
if isCameraLock  then
  -- do something...
end

Would something like that work?

1 Like

Locking the camera to a specific place can be done by doing

Camera.CameraType = Enum.CameraType.Scriptable

Instead, you can do

Camera.CFrame = CFrame.new(
    math.clamp(Camera.CFrame.X, -50, 50)
    math.clamp(Camera.CFrame.Y, -50, 50)
    math.clamp(Camera.CFrame.Z, -50, 50)
)

Which will lock the camera to a 100x100x100 area. You’ll of course need to run this code multiple times a second for it to work properly, and account for stuff like the camera’s orientation, but this is just a basic example.

2 Likes

CFrame is made up of 12 components,

X, Y, Z, R00, R01, R02, R10, R11, R12, R20, R21, R22

When you build a CFrame and provide values to it, unfilled values (angles or otherwise) will write anyways. So you can build an equivalent CFrame in numerous ways:

print(CFrame.new(0, 0, 0) == CFrame.Angles(0, 0, 0)) -- true

X, Y, Z are the CFrame’s position in 3D space, equivalent to the part’s position.
R00 → R22 are the CFrame rotation in 3D space, equivalent to the part’s orientation (but always use CFrame operations over .Rotation = Rotation).

In the instance that Phoenix provided, IE:

CFrame.new(50, 0, -10) * CFrame.Angles(0, math.rad(180), 0)

A CFrame is built using only position, placed at 50, 0, -10. This CFrame already has its rotational values in nature, because it is a CFrame, even though you didn’t write them or get to Angle calculation yet. They are filled as defaults image to create base rotation.

CFrame.Angles in this instance, also builds its own CFrame, only default filling the positional values. When you multiply two matrixes with equivalent matrice counts, each row will multiply by each column.

A CFrame matrix looks like this, to create each rotation and then the position:
image

As stated, we saw how these default when an unfilled matrice argument is passed, and the rest is just generic multiplication, but it seems more complicated:

All this to say, even though it is effectively adding, it is still multiplication.

3 Likes

Thank you guys so much! I learned a lot, now I will continue practicing with this I still have a lot to learn :smiley:

1 Like