# Clumsy quaternions

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?

``````        //Fix rotation
Quaternion fr = new Quaternion();
fr.set(0.601501f, 0.37174806f, 0.0f, -0.70710677f);

Quaternion rot90 = new Quaternion();
rot90.fromAngleAxis(FastMath.HALF_PI, Vector3f.UNIT_X);

vector = rot90.mult(vector);
``````

fr values were determined a while ago by finding the delta between the vector and another vector pointing the way I wanted.

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.

Poo…

So the vector I have is this:

``````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.”

primity way I use:

2 quaternions each with lookat one of the vectors.

Now you have two rotations from world coordinate system.

Substract? one from the other, and you have an rotation between both vectors, in respect tot he originally given up vector.

Note that substract is not a mathematical substract here, it was something else I think it was a devide dor something with the inverse Alsways kinda suck at this stuff myself

Depends on what you want from “subtract”… but if you want one quaternion relative to the other then you usually multiply by the inverse of the base.

OP, what are you using this quaternion for? Maybe that will help.

Basically I want all the points to be rotated a bit so that the one vector I put up there (the -1, t, 0 one) is pointing straight “up” so that that point is in effect the “North Pole”.

So, if I translate correctly… you have a point on the sphere and you want to rotate the sphere such that the specific point is “up”?

Yup. Sorry I wasn’t clear. Running on very little sleep.

some pseudo code:

Given:
pointOnSphere
desiredUp (presumably Vector3f.UNIT_Y)

currentUp = pointOnSphere.normalize()
if( desiredUp == currentUp ) {
done
}

// 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.

1 Like

Cool. Thanks!