cglm icon indicating copy to clipboard operation
cglm copied to clipboard

How do I generate a quaternion from euler angles?

Open badasahog opened this issue 1 year ago • 14 comments

I want to get a quaternion based on roll, pitch, and yaw, but as far as I can tell there is no function for this.

The only functions I could find were based on a unit vector and an angle (glm_quatv, glm_quat)

glm provides this functionality in the form of the glm::quat(glm::vec3) constructor

badasahog avatar May 30 '23 20:05 badasahog

Hi @badasahog,

Sorry for the delay,

It could be nice to have this feature in cglm too. I had tried to update Euler with https://github.com/recp/cglm/pull/128 but couldn't finished that PR yet,

We can have this before that,

Any PR would be awesome otherwise I added to TODOs. 🙂

recp avatar Jun 02 '23 11:06 recp

Yes It would be amazing I also need a quat structure for my project.

Nonesence999 avatar Jun 10 '23 17:06 Nonesence999

Sorry for answering very late, but for anyone else who needs help in the future, you could try making a function that rotates the quaternion by each axis. I guess you could also try and push it to the repository even though it might not be optimal

// example for rotating the quaternion by the y axis by an angle:

// y axis vec3s axis = {0, 1, 0};

// creates a quaternion with the angle and axis versors rotation = glms_quatv(angle, axis);

// when multiplying this rotation with an angular position quaternion, it will apply the rotation to the angular position angular_position = glms_quat_mul(rotation, angular_position);

telephone001 avatar Nov 28 '23 16:11 telephone001

@telephone001 many thanks, hope we add this asap 🚀

recp avatar Nov 28 '23 17:11 recp

do you think glm_quate_xyz or glm_quat_euler_xyz would be a better name for this new function?

telephone001 avatar Dec 05 '23 14:12 telephone001

To convert Euler to Quat, I would use glm_euler_[x|y|z]_quat which is similar pattern used in cglm e.g. glm_mat4_quat() convert mat4 to quat.

glm_SRC_DST()

recp avatar Dec 06 '23 07:12 recp

Alright thanks for the response. I'll try and get this done soon

telephone001 avatar Dec 06 '23 14:12 telephone001

@telephone001 thanks 🤗

recp avatar Dec 06 '23 19:12 recp

@recp Finished making it and all my tests pass on my computer, but it fails using meson webassembly for some reason. I think I did everything I needed though (exported it in call/quat, made struct versions of it, declared and entered tests) Do I need to add anything in the wasmfiles?

telephone001 avatar Dec 08 '23 20:12 telephone001

Thanks for the implementation a PR would be nice to see,

it fails using meson webassembly for some reason.

https://github.com/recp/cglm/actions seems fine, may I ask what are the errors? Also can you pull latest commits and try again? there was an issue about https://github.com/recp/cglm/pull/360#issuecomment-1838122934 but fixed later.

recp avatar Dec 08 '23 22:12 recp

I created a pr and most tests failed. The error is just that all tests fail on certain builds.

telephone001 avatar Dec 08 '23 22:12 telephone001

The 'expected' quaternion is overwriting itself during multiplication.

Another thing to check is whether glm_euler_*_quat matches, or roughly matches, glm_euler_by_order + glm_mat4_quat. GLM_EULER_XYZ looks like X*Y*Z (local/intrinsic) instead of Z*Y*X (global/extrinsic).

gottfriedleibniz avatar Dec 09 '23 01:12 gottfriedleibniz

Oh I see now. Thank you for the reply. I'll try and get this fixed up. I'll also add glm_euler_by_order + glm_mat4_quat in tests Would it be preferred to use the glm_euler_seq ord or the xyz combo in the name? Because euler.h has both Also, I'm thinking of moving these functions to euler.h would that be better than quat.h? If we do settle on the xyz combo in the name, would it be allowed for me to make a macro function to create the xyz combination tests for me?

telephone001 avatar Dec 09 '23 01:12 telephone001

The 'expected' quaternion is overwriting itself during multiplication.

@gottfriedleibniz many thanks for the catch!

I'll also check all places e.g. dest[0] to fix similar situations

Also, I'm thinking of moving these functions to euler.h would that be better than quat.h?

Yes since SRC->DST is Euler -> Quat I think they must be in euler.h, In the future we can add reverse quat_to_euler in quat.h.

If we do settle on the xyz combo in the name, would it be allowed for me to make a macro function to create the xyz combination tests for me?

To simplify creating tests, any macro, utility functions can be used 👍

recp avatar Dec 09 '23 09:12 recp