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

How can I calculate the surface area of the upward facing side of a part using CFrames?

Asked by 3 years ago
Edited 3 years ago

The part will always be in 90 degree increments in orientation, but what CFrame calculations would I need to do in what order to find the surface area of the side that is facing upward?

1 answer

Log in to vote
0
Answered by
Speedmask 661 Moderation Voter
3 years ago
Edited 3 years ago

I’m assuming your part is rectangular. if so then it should be pretty simple, just a bit tedious to write. this is because the normal of a face will always be RightVector, UpVector, LookVector or their negative counterparts. it works for any orientation (whichever faces the most up).

function SAFromTop(part)
    -- find which side faces most “upwards” or “downwards” by checking the y of each normal
    -- if it faces the opposite direction the surface area is still the same on the opposite side because it’s rectangular, so downwards still counts
    local function compare(axis1, axis2)
        return math.abs(part.CFrame.YVector[axis1]) > math.abs(part.CFrame.YVector[axis2]) and axis1 or axis2
    end

    local axis = compare(“X”, “Y”)
    axis = compare(axis, “Z”)

    if axis == “X” then
        return part.Size.Y * part.Size.Z
    elseif axis == “Y” then
        return part.Size.X * part.Size.Z
    else -- axis == “Z”
        return part.Size.X * part.Size.Y
    end
end

of course things get more complicated when it’s not a square...

0
Took me a minute to understand how this works (somehow) but makes sense to me. Thanks. And yeah it's just a big rectangle for a sandbox tycoon. Keita2282 2 — 3y
0
cool. let me know if you still don’t get it I’m happy to explain further. Speedmask 661 — 3y
Ad

Answer this question