94 lines
2.5 KiB
Lua
94 lines
2.5 KiB
Lua
|
--require("math.quat")
|
||
|
--require("math.vec3")
|
||
|
|
||
|
Transform = { translation = Vec3.ZERO, rotation = Quat.IDENTITY, scale = Vec3.ONE }
|
||
|
Transform.__index = Transform
|
||
|
Transform.__name = "Transform"
|
||
|
|
||
|
function Transform:new(translation, rotation, scale)
|
||
|
local t = {}
|
||
|
setmetatable(t, Transform)
|
||
|
|
||
|
t.translation = translation
|
||
|
t.rotation = rotation
|
||
|
t.scale = scale
|
||
|
|
||
|
return t
|
||
|
end
|
||
|
|
||
|
function Transform:copy()
|
||
|
return Transform:new(self.translation:copy(), self.rotation:copy(), self.scale:copy())
|
||
|
end
|
||
|
|
||
|
--- Creates a new Transform with the translation at the vec3
|
||
|
--- @param pos Vec3
|
||
|
function Transform:from_vec3(pos)
|
||
|
local t = Transform:copy() -- copy of default transform
|
||
|
t.translation = pos
|
||
|
return t
|
||
|
end
|
||
|
|
||
|
function Transform:from_xyz(x, y, z)
|
||
|
Transform:from_vec3(Vec3:new(x, y, z))
|
||
|
end
|
||
|
|
||
|
--- Calculates the forward vector of the Transform.
|
||
|
--- @return Vec3
|
||
|
function Transform:forward()
|
||
|
return (self.rotation * Vec3.NEG_Z):normalize()
|
||
|
end
|
||
|
|
||
|
--- Calculates the left vector of the Transform.
|
||
|
--- @return Vec3
|
||
|
function Transform:left()
|
||
|
return (self.rotation * Vec3.X):normalize()
|
||
|
end
|
||
|
|
||
|
--- Calculates the up vector of the Transform.
|
||
|
--- @return Vec3
|
||
|
function Transform:up()
|
||
|
return (self.rotation * Vec3.Y):normalize()
|
||
|
end
|
||
|
|
||
|
--- Rotates `self` using a Quaternion
|
||
|
--- @param quat Quat
|
||
|
function Transform:rotate(quat)
|
||
|
self.rotation = (quat * self.rotation):normalize()
|
||
|
end
|
||
|
|
||
|
--- Rotates `self` around the x-axis
|
||
|
--- @param rad number
|
||
|
function Transform:rotate_x(rad)
|
||
|
self:rotate(Quat:from_rotation_x(rad))
|
||
|
end
|
||
|
|
||
|
--- Rotates `self` around the y-axis
|
||
|
--- @param rad number
|
||
|
function Transform:rotate_y(rad)
|
||
|
self:rotate(Quat:from_rotation_y(rad))
|
||
|
end
|
||
|
|
||
|
--- Rotates `self` around the z-axis
|
||
|
--- @param rad number
|
||
|
function Transform:rotate_z(rad)
|
||
|
self:rotate(Quat:from_rotation_z(rad))
|
||
|
end
|
||
|
|
||
|
--- Calculates the linear iterpolation between `self` and `rhs` based on the `alpha`.
|
||
|
--- When `alpha` is `0`, the result will be equal to `self`. When `s` is `1`, the result
|
||
|
--- will be equal to `rhs`
|
||
|
--- @param rhs Transform
|
||
|
--- @param alpha number
|
||
|
--- @return Transform
|
||
|
function Transform:lerp(rhs, alpha)
|
||
|
local res = self:copy()
|
||
|
res.translation = self.translation:lerp(rhs.translation, alpha)
|
||
|
res.rotation = self.rotation:lerp(rhs.rotation, alpha)
|
||
|
res.scale = self.scale:lerp(rhs.scale, alpha)
|
||
|
return res
|
||
|
end
|
||
|
|
||
|
function Transform:__tostring()
|
||
|
return "Transform(pos=" .. tostring(self.translation) .. ", rot="
|
||
|
.. tostring(self.rotation) .. ", scale=" .. tostring(self.scale) .. ")"
|
||
|
end
|