diff --git a/data/scripts/types/vector3.lua b/data/scripts/types/vector3.lua index 6be968a..79c852a 100644 --- a/data/scripts/types/vector3.lua +++ b/data/scripts/types/vector3.lua @@ -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)