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

I don't get how '*' is also counted as adding I thought it was multiplying?

Asked by 6 years ago

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)
0
* is multiplication in all cases, but i've never seen that... DeceptiveCaster 3761 — 6y
0
Interesting... DeceptiveCaster 3761 — 6y
1
CFrames are not the same as numbers. CFrames are matrices, and when multiplying two CFrames together, your resultant CFrame is the two added together. There is a deeper explanation for this, but that's simply the syntax. You can check out http://wiki.roblox.com/index.php?title=CFrame#CFrame_.2A_CFrame and view the operators there to see what is allowed and what happens. WoolHat 53 — 6y
1
Multiplying matrices is not the same as element-wise adding them together, it's a bit more involved than that. Multiplying CFrames is a step more mysterious to developers, since the 4th row of the matrix is hidden. This row is always 0,0,0,1 so you can write the CFrame as a 4x4 and multiply it to see what really happens. For CFrame * Vector3 you need to upgrade the vector to a 4x1: (x,y,z,1) EmilyBendsSpace 1025 — 6y

1 answer

Log in to vote
7
Answered by
ozzyDrive 670 Moderation Voter
6 years ago

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.

Ad

Answer this question