add rotated method to vector3

This commit is contained in:
Lera Elvoé 2025-02-13 23:47:16 +03:00
parent c9ac220089
commit de9ee09074
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc

View File

@ -164,6 +164,22 @@ function Vector3:cross(with)
}
end
function Vector3:rotated(axis, angle)
if not is_weak_vector3(axis) then
error("Vector3: axis must be a Vector3-like table. Returning Vector3()")
return Vector3()
end
axis = Vector3(axis):normalized()
local cosa = math.cos(angle)
local sina = math.sin(angle)
-- __mul is only defined for the left operand (table), numbers don't get metatables.
-- as such, the ordering of operations here is specific
local v = (self * cosa) + (axis * ((1 - cosa) * self:dot(axis))) + (axis:cross(self) * sina)
return v
end
---- CONSTANTS
Vector3.UP = Vector3(0, 1, 0)