Is this the best way to get a surface normal?

Given a cube, is this the best way to get the surface normal?

local function GetNormal(part, normalId)
	return part.CFrame:Inverse():VectorToObjectSpace(Vector3.FromNormalId(normalId))
end

I’m calling VectorToObjectSpace on the inversed CFrame of a given part, and passing the global Vector for the given NormalId.

Is there a better way to do this? This feels clunky.

6 Likes
local function GetNormal(part, normalId)
	return part.CFrame * Vector3.FromNormalId(normalId) - part.Position
end

Should also work and is a little bit cheaper.

24 Likes

What @buildthomas did is the same as part.CFrame:VectorToWorldSpace(Vector3.FromNormalId(normalId)), but like he said, doing it his way is cheaper.

4 Likes

You can just cframe.lookVector cframe.upVector and cframe.rightVector to get the three positive axis normals, and flip them to get the other three.

Its usually going to be cheaper to do some conditional logic than a matrix vec multiply. This is assuming you were worried about performance?

It is less elegant though!

Something like:

function GetNormals(part)
    local x = part.CFrame.upVector
    local y = part.CFrame.rightVector
    local z = part.CFrame.lookVector

    return x, -x, y, -y, z, -z
end
3 Likes