handmade.network » Forums » Help with Quaternions?
ClysmiC
Andrew L. Smith
13 posts

Hobby game developer hoping to go pro some day!

#8326 Help with Quaternions?
2 years, 8 months ago

Hey everyone. I am working on a game where a character walks around the surface of a sphere (think Super Mario Galaxy), so I figured it would be wise to use quaternions for orientation since it is very important that the player and camera orientations work well. I understand the idea behind quaternions, but I don't really understand how to generate them in the first place... previously I had been storing an "up" vector and a "forward" vector to define my character's orientation, and had been doing Euler rotations to construct a rotation matrix. Each time my character moved, the up vector would be recalculated to be the vector from the center of the sphere to the player's position, and the forward vector would be the movement vector projected onto the plane that the player is standing on. I would like to have a similar way of constructing quaternions (where I can define it from and "up" and "forward" vector), but it seems that every resource that I have found online about quaternions explains how to use them for *rotations*, and not for *orientations*. I have found some source code online that supposedly implements the behavior I am looking for, but I don't really understand it and when I tried to implement it I wasn't getting the behavior I wanted.

Maybe I am thinking about quaternions all wrong? I'm not 100% sure what I'm asking, but I've been struggling with this all day to no avail. Given the description of my game, could someone who is experienced using quaternions suggest to me how they would go about using them?
chronokun
Michael Cameron
31 posts
#8330 Help with Quaternions?
2 years, 8 months ago

Normally I would define construct quaternions by chaining together 'rotate around axis/angle' transforms. As for creating a quaternion from basis vectors, it should be basically the same as implementing something like 'lookAt'/'lookRotation' which seems to be based on converting it from a matrix from the various examples I found with google.
ClysmiC
Andrew L. Smith
13 posts

Hobby game developer hoping to go pro some day!

#8348 Help with Quaternions?
2 years, 8 months ago

Thanks for the help. I'll look into that.
timothy.wright
Timothy Wright
77 posts / 1 project
#8350 Help with Quaternions?
2 years, 8 months ago

Do you have the link to the source you are talking about, or maybe just post it.

What part of implementing this gave you trouble? Are you using a math library and don't understand what to do with the code, or are you trying to do it yourself?
ClysmiC
Andrew L. Smith
13 posts

Hobby game developer hoping to go pro some day!

#8362 Help with Quaternions?
2 years, 8 months ago

Gahh, I think I solved the issue I was having. I'm using the GLM math library (but slowly replacing it with my own structs, functions, etc.), and apparently matrix multiplication in GLM is reverse of what I was expecting. To apply a rotation matrix (that I was generating from my quaternion) to the model matrix I was doing
1
model = rotation * model


But I really had to do
1
model = model * rotation


This issue only popped up when I switched to quaternions because I was previously using a built-in GLM rotation function instead of calculating the matrix myself and doing the multiplication. Thanks for the help everyone, I think I got it working now :D
chronokun
Michael Cameron
31 posts
#8364 Help with Quaternions?
2 years, 8 months ago

ClysmiC
Gahh, I think I solved the issue I was having. I'm using the GLM math library (but slowly replacing it with my own structs, functions, etc.), and apparently matrix multiplication in GLM is reverse of what I was expecting.


If you come from using DirectX or something then that's probably down to the difference between row-major and column-major vectors/matrices, it changes the order of things.