I don't get it how does
print(49*40) multiply but things like this game.Players.PlayerAdded:Connect(function(plr) P = script.Parent Click = Instance.new("ClickDetector",P) Click.MouseClick:Connect(function() while true do P.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,0.1,0) -- How does this work I don't get it is it adding or multiplying I heard its adding watching peaspods video but I don't get how this is counted as multiplying wait() end end) end)
That indeed is multiplication, more specifically matrix multiplication. Although keep in mind developers can write their own functionality for the *
operator with metatables, so when looking at someone else's code it may not mean that, even though for the sake of readibility, it should.
CFrames can be thought of as two matrices, the position matrix (x, y, z, a 3x1 matrix) and the rotational matrix (3x3 matrix). When multiplying two matrices together, the resulting position matrix comes like:
local a = CFrame.new( 1, 1, 1 ) local b = CFrame.new( 2, 2, 2 ) --[[ By default, a CFrame's rotational matrix looks like the following: 1, 0, 0, 0, 1, 0, 0, 0, 1 When put into a bunch of variables: m00, m01, m02, m10, m11, m12, m20, m21, m22 Thus when multiplying a by b, we get the position matrix for 'c' like so: x: a.x * m00 + a.y * m01 + a.z * m02 + b.x * m00 + b.y * m01 + b.z * m02, y: a.x * m10 + a.y * m11 + a.z * m12 + b.x * m10 + b.y * m11 + b.z * m12, z: a.x * m20 + a.y * m21 + a.z * m22 + b.x * m20 + b.y * m21 + b.z * m22, ]] local c = CFrame.new( 1 * 1 + 1 * 0 + 1 * 0 + 2 * 1 + 2 * 0 + 2 * 0, 1 * 0 + 1 * 1 + 1 * 0 + 2 * 0 + 2 * 1 + 2 * 0, 1 * 0 + 1 * 0 + 1 * 1 + 2 * 0 + 2 * 0 + 2 * 1 ) local a_by_b = a * b print(a_by_b, "\t", c, "\t", a_by_b == c) -- >> 3, 3, 3, 1, 0, 0, 0, 1, 0, 0, 0, 1 3, 3, 3, 1, 0, 0, 0, 1, 0, 0, 0, 1 true
Where c
is constructed like the a * b
operation would internally construct it.
So matrix multiplication is utilizing the dot product, which itself is multiplication.
The rotational matrix is similar:
local a = CFrame.new( 1, 2, 3, 0.5, 0, 0.5, 0, 1, 0, 0, 0, 1 ) local b = CFrame.new( 1, 2, 3, 1, 0, 0, 0, 1, 0, 0, 0, 1 ) local c = CFrame.new( 1 * 1 + 2 * 0 + 3 * 0 + 1 * 0.5 + 2 * 0 + 3 * 0.5, 1 * 0 + 2 * 1 + 3 * 0 + 1 * 0 + 2 * 1 + 3 * 0, 1 * 0 + 2 * 0 + 3 * 1 + 1 * 0 + 2 * 0 + 3 * 1, 0.5 * 1 + 0 * 0 + 0.5 * 0, 0.5 * 0 + 0 * 1 + 0.5 * 0, 0.5 * 0 + 0 * 0 + 0.5 * 1, 0 * 1 + 1 * 0 + 0 * 0, 0 * 0 + 1 * 1 + 0 * 0, 0 * 0 + 1 * 0 + 0 * 1, 0 * 1 + 0 * 0 + 1 * 0, 0 * 0 + 0 * 1 + 1 * 0, 0 * 0 + 0 * 0 + 1 * 1 ) local a_by_b = a * b print(a_by_b, "\t", c, "\t", a_by_b == c) -- >> 3, 4, 6, 0.5, 0, 0.5, 0, 1, 0, 0, 0, 1 3, 4, 6, 0.5, 0, 0.5, 0, 1, 0, 0, 0, 1 true
EgoMoose has written a bunch of great articles regarding different math topics on the wiki. Here's one related to matrices, including their multiplication.