add in-place rotate method to v3 and avoid unnecessary conversion in :rotated()

This commit is contained in:
Lera Elvoé 2025-02-23 17:07:56 +03:00
parent 3859c190d1
commit 1e2a162800
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc

View File

@ -225,8 +225,25 @@ function Vector3:rotated(axis, angle)
local sina = math.sin(angle) local sina = math.sin(angle)
-- __mul is only defined for the left operand (table), numbers don't get metatables. -- __mul is only defined for the left operand (table), numbers don't get metatables.
-- as such, the ordering of operations here is specific -- as such, the ordering of operations here is specific
local v = (self * cosa) + (vaxis * ((1 - cosa) * self:dot(vaxis))) + (vaxis:cross(self) * sina) return (self * cosa) + (vaxis * ((1 - cosa) * self:dot(vaxis))) + (vaxis:cross(self) * sina)
return Vector3(v) end
---In-place version of rotated.
---@param axis Vector3
---@param angle number
---@return Vector3
function Vector3:rotate(axis, angle)
local cosa = math.cos(angle)
local sina = math.sin(angle)
local dot = self:dot(axis)
local cross = axis:cross(self)
self.x = self.x * cosa + axis.x * ((1 - cosa) * dot) + cross.x * sina
self.y = self.y * cosa + axis.y * ((1 - cosa) * dot) + cross.y * sina
self.z = self.z * cosa + axis.z * ((1 - cosa) * dot) + cross.z * sina
return self
end end
---Returns a copy of this vector. ---Returns a copy of this vector.