Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Euler angles [incomplete] #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions modules/quat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,41 @@ function quat.to_angle_axis(a, identityAxis)
return angle, vec3(x, y, z)
end

--- Convert a quaternion into euler angle components
-- @tparam quat a Quaternion to convert
-- @treturn roll
-- @treturn pitch
-- @treturn yaw
function quat.to_euler_angles_unpack(q)
-- roll (x-axis rotation)
local sinr_cosp = 2 * (q.w * q.x + q.y * q.z)
local cosr_cosp = 1 - 2 * (q.x * q.x + q.y * q.y)
local roll = math.atan2(sinr_cosp, cosr_cosp)

-- pitch (y-axis rotation)
local sinp = 2 * (q.w * q.y - q.z * q.x)
local pitch
if math.abs(sinp) >= 1 then
pitch = math.pi / 2 * ((sinp > 0) and 1 or -1) -- Use 90 degrees if out of range
else
pitch = math.asin(sinp)
end

-- yaw (z-axis rotation)
local siny_cosp = 2 * (q.w * q.z + q.x * q.y)
local cosy_cosp = 1 - 2 * (q.y * q.y + q.z * q.z)
local yaw = math.atan2(siny_cosp, cosy_cosp)

return roll, pitch, yaw
end

--- Convert a quaternion into euler angles
-- @tparam quat a Quaternion to convert
-- @treturn result a {roll, pitch, yaw} table
function quat.to_euler_angles(a)
return {quat.to_euler_angles_unpack(a)}
end

--- Convert a quaternion into a vec3.
-- @tparam quat a Quaternion to convert
-- @treturn vec3 out
Expand Down