Sloppy rotation (solved)

Hello,



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());
      
      Quaternion q = new Quaternion();
      q.fromAngles(FastMath.PI / 4f, 0, -FastMath.PI / 4f);
      // q.fromAngleAxis(FastMath.PI / 4f, new Vector3f(1, 0, -1));
      t.setLocalRotation(q);

      rootNode.attachChild(s1);
      rootNode.attachChild(s2);
      rootNode.attachChild(t);
      cam.setLocation(new Vector3f(0, 0, 5));




The result looks like this:


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.

Hope someone can help me. Many thanks.

I can't really help, because my math skills are pretty much non existent :slight_smile:

I created a grid, to make sure the spheres are correctly placed, maybe it helps other to see whats wrong.

You see it if you first rotate into one direction (45

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);

      rootNode.attachChild(s1);
      rootNode.attachChild(s2);
      rootNode.attachChild(t);
      cam.setLocation(new Vector3f(0, 0, 5));



And the rotation:


Great, that's exactly what I needed. I'm not sure what I've done wrong, though. Never mind.

Thanks a lot!!



Can I mark this topic as solved, somehow?

I guess just edit the your first post's title

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?



Thanks.

msj121 said:

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?

Thanks.


Yeah, something like that. Some reference:

http://en.wikipedia.org/wiki/Axis_angle

Oh okay, it is the normal of the plane, pretty much. Thanks for the link, I appreciate it.