#converting quaternion to euler angles

81 messages · Page 1 of 1 (latest)

rapid swallow
#

I am trying to convert a quaternion to a 2 dimensional rotation vector. i have successfully converted the 2d rotation vector to quaternions but going backwards is giving me a huge headache. im wondering if anyone knew how exactly to string together the converting back properly.

dapper kettleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

rapid swallow
#

I expect quaternionToEulerRot to give me -70.f, 125.f

#

converting quaternion to euler angles

jovial verge
#

your y and z (and their signs) are flipped in line 40-41

#

additionally, on line 57 the x/z and w/y should also be switched

#

line 40/41:

(cx * sy * cz) - (sx * cy * sz), // quat.y
(sx * cy * cz) + (cx * sy * sz)  // quat.z

line 57:

float sinp = 2.f * (quat.x * quat.z - quat.w * quat.y);
#

btw, (after the 40/41 fix) your w and x are swapped compared to wikipedia and likewise for z and y
this is why they need to be switched for line 57 (the other ones are all addition)

#

@rapid swallow

rapid swallow
#

i think my target has roll and pitch flipped or something

#

or something flipped not sure, but i just know that eulerRotToQuaternion is "correct"*

jovial verge
#

oh ok, then i guess you just need to switch quat.y and quat.z in your quaternionToEulerRot and flip the signs for both

rapid swallow
#

wym "flip the signs for both"?

#

like, the members of the final result?

jovial verge
#

change (sx * cy * cz) - (cx * sy * sz) to +

#

and same for z

rapid swallow
#

But, as I said above, eulerRotToQuaternion gives me correct numbers

#

dont really know why but it does lol

jovial verge
#

huh...

rapid swallow
#

the relationship is really close as you can see

#

swap the members and take their complement or something...but thats cheeky

jovial verge
#

wait you mean the r1 numbers? or the q2?

rapid swallow
#

r1 is what im starting with

#

in my console i expect to be able to convert r1 to a quat then back to a vec2 and end up with the same values that i started with

jovial verge
#

(the second one fixes sign of first number, and the first one fixes the 55/110 to 70/125)

rapid swallow
#

one moment...

#

i think its working now

#

tysm!!

jovial verge
#

oh um....

#

it seems that rot.x has to be within (-90, 90)

#

probably because of the asin

rapid swallow
rapid swallow
#

its for a player

#

if they look straight up, they are at -89.99 degrees

#

straight down, 89.99

#

should i be concerned with that?

jovial verge
#

uh gimme 1 sec

rapid swallow
jovial verge
#

gah i hate floating points

#

would it be possible to switch out the float for doubles?

#

sometimes it just likes to break on me and i don't exactly know why

rapid swallow
#

I mean, my target wants floats but its the same dif

jovial verge
#

are you getting floats as inputs as well?

rapid swallow
#

yes

jovial verge
#

mm ok

rapid swallow
#

the quat is just gonna be like that vec4 thing with 4 floats

jovial verge
#

it would probably be better if you used doubles for the processing stuffs, to minimize error

rapid swallow
#

yes, I see

jovial verge
#

i'm getting some pretty large errors here lmao :\ (>0.05)

rapid swallow
#

just by virtue of floating point inaccuracies?

#

i should prob know this lol but do the errors "build up" over time?

jovial verge
#

i think the errors happen more toward the extremes

rapid swallow
#

like, would i benefit if i were to do everything in terms of doubles then cast the final result to a float?

jovial verge
#

probably... but i can check

rapid swallow
#

I can't change the actual inputs and outputs to doubles because im working with someone elses api

jovial verge
#

understandable
a lot of graphics stuff is done in float becuse i think even modern gpus sometimes don't support double lol

rapid swallow
#

yea i see

#

but i totally can use doubles as an intermediary step

jovial verge
#

i can't tell if it's better or the same lmao

#

i'm getting 89.9984, -166.899 -> 90, -166.854

#

89.9802, -169.836 with float (the first one was double as intermediary)

rapid swallow
#

i guess only way to tell is if you actually test a double input and output then compare that

jovial verge
#

seems like a negligible difference to me

rapid swallow
#

i suppose so

#

but in practice i should keep in mind how lots of repeated arithmetic might have noticeable precision errors with floats

jovial verge
#

here is a random "fuzz" thing i hacked together (it is not very good code)

Vec2 r2;
    for (float i = -89.9f; i < 90.0f; i += 0.1f)
    {
        for (float j = -179.9f; j < 180.0f; j += 0.1f)
        {
            r2.x = i;
            r2.y = j;
            auto q2 = eulerRotToQuaternion(r2);
            auto r1 = quaternionToEulerRot(q2);
            if (std::abs(r1.x - r2.x) >= 0.1 || std::abs(r1.y - r2.y) >= 0.1)
            {
                std::cout << i << " " << j << std::endl <<
                    r1.x << " " << r2.x << "\t" << r1.y << " " << r2.y << std::endl;
            }
        }
    }
#

i unfortunately have to leave, but you can play around with the things and see if the errors are acceptable

#

or whatever, idk

rapid swallow
#

thats ok thx again for ur help

#

prob wouldnt have figured this out otherwise lol

jovial verge
#

@rapid swallow Has your question been resolved? If so, run !solved :)

rapid swallow
#

oh yes

#

soryyy

#

!solved