Question about Quaternion rotation problems

Trying to simplify my original problems, and I think I have it condensed as well as I can. Right now, I have two target points. Between those points, I build a line that matches the length between it’s “target” points. I then am trying to use quaternions to rotate that line appropriately, and translate it into place, effectively placing it perfectly between the two target points.

ofApp.cpp
http://pastebin.com/SyMhMW6i

ofApp.h
http://pastebin.com/3u2DPp9J

Currently, I can’t seem to get rotation around the x axis to work properly. There are other placements of those target points that work fine, when the line remains flush with the x axis, but any z-depth and it’s stuck.

Any tips?

Thank you

Hey,

I think you need to rotate to the normal/direction between your 2 points. The 3D/orientation example has a good example of this. This works for your code…

ofPushMatrix();
ofTranslate((originP.x+destP.x)/2,(originP.y+destP.y)/2,(originP.z+destP.z)/2);

ofVec3f direction = (destP - originP).getNormalized();
float rotationAmount;
ofVec3f rotationAngle;
ofQuaternion rotation;
ofVec3f axis(1, 0, 0);
rotation.makeRotate(axis, direction);
rotation.getRotate(rotationAmount, rotationAngle);
ofRotate(rotationAmount, rotationAngle.x, rotationAngle.y, rotationAngle.z);
connections.draw();

ofPopMatrix();
2 Likes

This is really great and simplified. I’m trying to break down what you’ve provided, I still don’t have a great understanding of the role of normalization and it’s relationship to direction. Also, how do you get by without calculating the angle between the axis?

Thanks a lot though, this really helps me, I’ve been racking my brain over this for a while.

I think I can speak to this a bit.

destP - originP

gives you a vector3 with direction and magnitude. For example, if destP = (3, 5, 2) and originP = (1, 1, 1), then you’ll get a vector3 that is (2, 4, 1), so

magnitude = sqrt(22 + 44 + 1*1) = 4.58258.

And if you were to draw a line between the two points, you would obviously see the direction.

Normalization divides the vector by its own magnitude so you get just the direction. It divides each value in the vector by the magnitude so that the values are mapped between 0 and 1 (in this case the normalized values would be 0.436436, 0.872872, 0.218218). That way, it corresponds much better to the axis, which is also normalized (your axis above is (1, 0, 0) for example).

Since the direction is normalized now, he’s using

rotation.makeRotate(axis, direction);

to directly compare it to ‘axis’ and get the difference between the two, and then

rotation.getRotate(rotationAmount, rotationAngle);

to calculate the angle.

Hope that clears some stuff up!

3 Likes

I appreciate the explanation, much cleaner way of doing things than I was before. Vectors still go over my head, this helps. Thanks!