I have problems with pointing a tube towards an object, i.e. a sphere.
Sample Code goes here:
Sphere s1 = new Sphere("test", new Vector3f(-1, -1, -1), 30, 30, 0.2f);
Sphere s2 = new Sphere("test2", new Vector3f(1, 1, 1), 30, 30, 0.2f);
Tube t = new Tube("test3", 0.1f, 0, new Vector3f(2, 2, 2).length());
I would expect the ends of the tube to be exactly in the middle of the two spheres. fromAngleAxis() leads to the same result. The application I have in mind is a 3d molecule viewer.
3D Rotations and quaternions make my hair standup, so this was a good exercise in rotation for me too. Following code uses the angle-axis method, where' we're aligning vector u (tube's direction) with some vector v (the vector between the two sphere's).
Sphere s1 = new Sphere("test", new Vector3f(-1, -1, -1), 30, 30, 0.2f);
Sphere s2 = new Sphere("test2", new Vector3f(1, 1, 1), 30, 30, 0.2f);
Tube t = new Tube("test3", 0.1f, 0, new Vector3f(2, 2, 2).length());
//Find the vector between the two sphere's
Vector3f v = s2.getCenter().subtract(s1.getCenter());
v.normalizeLocal();
//Note that the tube is is upright, if you use it's local
//rotation it's direction will be <0,0,1> and the finished
//rotation won't look correct
Vector3f u = new Vector3f(0,1,0);
//Do the cross product of both vectors to find the axis of rotation
Vector3f axis = u.cross(v).normalizeLocal();
//Find the rotation angle
float angle = FastMath.acos(u.dot(v));
//Setup the quaternion
Quaternion q = new Quaternion();
q.fromAngleAxis(angle, axis);
t.setLocalRotation(q);
Hey Starnick, I don't require this type of info now, but I read your post and the math after a bit actually made sense. I do appreciate your post and hopefully if I run into rotations etc… in the future I will be able to look back at this for some guidance. Thanks.
Btw, the axis vector seems to be the normal of the plane being rotated on or a vector for where the rotation is hinged on?
Hey Starnick, I don't require this type of info now, but I read your post and the math after a bit actually made sense. I do appreciate your post and hopefully if I run into rotations etc... in the future I will be able to look back at this for some guidance. Thanks.
Btw, the axis vector seems to be the normal of the plane being rotated on or a vector for where the rotation is hinged on?