title: 3D libgdx rotation
body: <p>I think this question is not a duplicate, or if it is, I couldn't understand how to make this work.</p>
  
  <p>(sorry for my great drawing capabilities...)
  <img src="https://i.stack.imgur.com/KOaSo.png" alt="Rotation Schema"></p>
  
  <p>I have a ball, radius 0.5 (so, perimeter is 3.1415926).
  In each frame, I have it in <code>oldPosition</code> and I want to place it in <code>position</code>, so, move in the blue arrow direction, represented by the v vector in the drawing. This is easy.</p>
  
  <p>The trick is that I want the ball to be rotated in the target position. The rotation axis should be perpendicular to the direction (green dashed line), and rotation should be performed in relation to the object center (rotation will be 2x the length of the translation).</p>
  
  <p>My libgdx code at the moment looks like:</p>
  
  <pre><code>// During object initialization I have a Matrix4 with identify
  Matrix4 rotation = new Matrix4();
  
  
  // Now, this code during render()
  Vector3 movement = position.sub(oldPosition);
  float length = movement.len();
  
  // drawing this on paper, it seems a correct way to compute orthogonal vector
  rotation.rotateRad(new Vector3(-movement.z, 0f, movement.x), 2*length);
  
  iSphere.transform = rotation.cpy().trn(position);
  </code></pre>
  
  <p>So, basically, I start with the identify Matrix4. In each frame I apply a rotation to it, apply that rotation in the sphere, and translate it.</p>
  
  <p>This seems to work at first (?), but after a lot of rotations, ball starts rotating in the wrong direction.</p>
  
  <p>I tried an implementation with Quaternions but did not have luck as well. It should be something simple that I am forgetting.</p>