From bd90620bdfc880b65f0d2cc6065d2fed0a97ba58 Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Sun, 5 May 2019 16:31:25 +1000 Subject: [PATCH] Fixed quat to yaw, pitch and roll functions and rotation arc not matching up to actual rotation --- src/common/linmath.c | 25 +++++++++++++++++-------- todo.txt | 7 ++++--- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/common/linmath.c b/src/common/linmath.c index 47661f4..ad057df 100755 --- a/src/common/linmath.c +++ b/src/common/linmath.c @@ -802,23 +802,32 @@ void quat_get_right(vec3* res, const quat* q) float quat_get_pitch(const quat* q) { float check = 2.0f * (-q->y * q->z + q->w * q->x); - if(check < -0.995f || check > 0.995f) - return 0.f; + if(check < -0.995f) + return -90.f; + else if(check > 0.995) + return 90.f; else - return TO_DEGREES(atan2f(2.f * (q->x * q->z + q->w * q->y), 1.f - 2.f * (q->x * q->x + q->y * q->y))); - //return TO_DEGREES(atan2f(2.f * (q->y * q->z + q->w * q->x), q->w * q->w - q->x * q->x - q->y * q->y + q->z * q->z)); + return TO_DEGREES(asinf(check)); } float quat_get_yaw(const quat* q) { - float result = (float)asin(-2 * (q->x * q->z - q->w * q->y)); - return TO_DEGREES(result); + float check = 2.0f * (-q->y * q->z + q->w * q->x); + if(check > 0.995f || check < -0.995f) + return 0.f; + else + return TO_DEGREES(atan2f(2.0f * (q->x * q->z + q->w * q->y), 1.0f - 2.0f * (q->x * q->x + q->y * q->y))); } float quat_get_roll(const quat* q) { - float result = atan2(2 * (q->x * q->y + q->w * q->z), q->w * q->w + q->x * q->x - q->y * q->y - q->z * q->z); - return TO_DEGREES(result); + float check = 2.0f * (-q->y * q->z + q->w * q->x); + if(check < -0.955f) + return TO_DEGREES(-atan2f(2.0f * (q->x * q->z - q->w * q->y), 1.0f - 2.0f * (q->y * q->y + q->z * q->z))); + else if(check > 0.995f) + return TO_DEGREES(atan2f(2.0f * (q->x * q->z - q->w * q->y), 1.0f - 2.0f * (q->y * q->y + q->z * q->z))); + else + return TO_DEGREES(atan2f(2.0f * (q->x * q->y + q->w * q->z), 1.0f - 2.0f * (q->x * q->x + q->z * q->z))); } void quat_mul_mat4(quat* res, quat* val, mat4* mat) diff --git a/todo.txt b/todo.txt index 4682c0a..d1ac7d3 100644 --- a/todo.txt +++ b/todo.txt @@ -98,9 +98,8 @@ Bugs: - Investigate memory usage increase when window is minimized - Fix hang on fullscreen toggle - Fix axis lines not aligning with grid lines - - Fix rotation arc starting and ending degree calculations - - Fix broken spot light degree/radian conversions - + - Fix arc angles resetting when rotating + Done: * Input * Shaders @@ -285,4 +284,6 @@ Done: * Highlight if we are about to select an entity or perform the tool action like translate when mouse is hovered and an entity can be selected at that location * Display the projected position if we perform the action for example display what the new location would be right next to the tool mesh * Tooltips for current action or hovered entity + * Fix broken spot light degree/radian conversions + * Fix rotation arc starting and ending degree calculations