I’m having… issues… understanding quaternions a bit.
What I’ve started with is a vector that is not pointing up like how I want it in the end. I needed this off kilter vector since it was easier to create my mesh that way. Now that my class is working awesomely the I’d like to rotate the vectors that go into the mesh so that north pole on the shape is facing up.
The solution I’ve come up with… sorta sucks I think. It works… But is there a more elegant solution?
You mean you looked at a quaternion you found by trial and error? Or something else.
Usually when I see users calling quaternion.set() I already know they are way lost. If you got the values from another quaternion then it’s technically not wrong… but there was probably a way to find the quaternion using math.
We haven’t been provided enough information to help with that, though.
float t = (float)((1.0 + Math.sqrt(5.0)) / 2.0);
Vector3f vec1 = new Vector3f(-1, t, 0);
Which is pulled from a blog post about icosahedron coordinates (there’s a total of 12 vectors that all have different combinations 1, -1, 0, and t as their values).
But no. It wasn’t through trial and error. I found a post in here (sorry… can’t find it) that showed a way to get the quaternion between 2 vectors. So I thought “Hey. May as well write down what it spit out and use that.”
// Get a vector orthogonal to the two up vectors
lookVector = desiredUp.cross(currentUp)
// Get a quaternion that represent the local space of the point on the sphere
// It will be looking down the lookVector we just created and will be as if we
// are standing on the sphere. The lookVector make sure that we don’t have
// any extra roll with respect to the desired up vector
Quaternion localSpace = new Quaternion().lookAt(lookVector, currentUp)
Quaternion rotateToUp = localSpace.inverse()
…something like that.
Note that it’s more complicated if your desired up vector is not UNIT_Y. You’d have to build an additional quaternion for that local up space (same way as above, same lookVector even) and then do a change of basis to get relative rotation.
Edit: also note that the if statement is required because you will get strange results from the cross product if they are the same vector.
Edit2: also, because it happens often, it’s also possible I have the cross product backwards. Not sure it matters in this case, though.
Is it such a bad thing to use static values for a quaternion if it’ll be the exact same every time you load the game? I agree that math is better just curious if there’s another reason why I shouldn’t just go “Oh. It’s these values… lemme just hard code those values so no processing time is used however small.”
Static values are fine. I use them in camera setups all the time.
…but usually when we see quaternion.set() here it’s because a user thinks they know what the values mean and are usually wrong. In your case that wasn’t true as you extracted them from a known quatermion basically. Still, beware.