title: Rotating an object to match a vector
body: <p>I thought that I was doing this correctly for the longest time, but however I believe I may be doing this in a very wrong way.</p>
  
  <p>I have a 3D engine with world terrain, and objects can be placed on the ground, when they get placed they get oriented to the terrain they are on (rotated).  The way I did this was like so:</p>
  
  <pre><code>D3DXVECTOR3 tNormal = GetNormalFromFile( ... );  //Get the terrain's normal
  
  placeItemData.placeMesh.meshRot.z = (acos(tNormal.x) - (D3DX_PI/2));
  placeItemData.placeMesh.meshRot.x = -(acos(tNormal.z) - (D3DX_PI/2)); 
  </code></pre>
  
  <p>I store the rotation as a vector in radians: x,y,z rotation, where Y is up.  So I calculate the rotation of the X and Z axis and rotate accordingly.  The player can manually rotate the object around the Y axis before placing it, and all of this has worked fine, supposedly.</p>
  
  <p>As I continued to improve my item placement code, I realized this was not working for near-vertical planes. For example, trying to place a picture on a wall.  This code seems to work to place items on near horizontal (flat) terrain, but the rotations are way off for a vertical wall.</p>
  
  <p>Am I missing a simple calculation here?  Do I need to calculate in the y-axis value somehow?  I attempted to to calculate the rotation using atan2 functions as well:</p>
  
  <pre><code>placeItemData.placeMesh.meshRot.z = (atan2(finalNormal.x,finalNormal.y) );
  placeItemData.placeMesh.meshRot.x = (atan2(finalNormal.z,finalNormal.y) );
  </code></pre>
  
  <p>But I ran into similar problems.  </p>
  
  <p>Do I need to be using quaternions for these calculations?  Am I able to store the final rotation values in a 3D vector (x,y,z) easily again?</p>
  
  <p>I appreciate any help!</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>Here is an image using this code:</p>
  
  <pre><code>meshRot.z = - atan2(sqrt(finalNormal.y^2 + finalNormal.z^2), finalNormal.x);
  meshRot.x = - atan2(finalNormal.y, finalNormal.z);
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/nYdYC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nYdYC.png" alt="prob1"></a><br>
  <sub>(source: <a href="http://www.crystaldragon.com/TempImages/prob1.png" rel="nofollow noreferrer">crystaldragon.com</a>)</sub>  </p>
  
  <p>As you hover the mouse over the stone, the normal of the triangle is computed (for the picture, the normal of the stone face is (0.637, -0.014, 0.770) ).  Of course the wood block you see in the stone, should be directly laying on top of the stone side (like being hung as a picture).</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>From testing an "wall" positioned in the z-axis in my world, I seem to have the code working for just all cases where y=0.  I am able to place the object on all the vertical walls, but not the horizontal(top) of the wall.</p>
  
  <p>Here is the current code:</p>
  
  <pre><code>placeItemData.placeMesh.meshRot.x =  -(atan2(finalNormal.y, sqrt((finalNormal.x * finalNormal.x ) + (finalNormal.z * finalNormal.z)) ) + (D3DX_PI/2));
  placeItemData.placeMesh.meshRot.z =  -(atan2(finalNormal.z, finalNormal.x ) + (D3DX_PI/2)); 
  </code></pre>
  
  <p>As long as the y-normal is a value of 0, the object will attach to each of the vertical walls, however not the horizontal (since the y-normal is a non negative number obviously).  Hopefully the final step is to figure out the final calculations I need to enter in as a y-value into the equation, but I am still coming up with failed attempts. </p>