Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here are my working assumptions:</p> <ul> <li>The coordinate system (x,y,z) is such that positive x is to the right, positive y is down, and z is the remaining direction. In particular, y=0 is the ground plane.</li> <li>An object at (0,0,0) currently facing towards (0,0,1) is being turned to face towards (x,y,z).</li> <li>In order to accomplish this, there will be a rotation about the x-axis followed by one around the y-axis. Finally, there is a rotation about the z-axis in order to have things upright.</li> </ul> <p>(The terminology yaw, pitch, and roll can be confusing, so I'd like to avoid using it, but roughly speaking the correspondence is x=pitch, y=yaw, z=roll.)</p> <p>Here is my attempt to solve your problem given this setup:</p> <pre><code>rotx = Math.atan2( y, z ) roty = Math.atan2( x * Math.cos(rotx), z ) rotz = Math.atan2( Math.cos(rotx), Math.sin(rotx) * Math.sin(roty) ) </code></pre> <p>Hopefully this is correct up to signs. I think the easiest way to fix the signs is by trial and error. Indeed, you appear to have gotten the signs on <code>rotx</code> and <code>roty</code> correct -- including a subtle issue with regards to z -- so you only need to fix the sign on <code>rotz</code>.</p> <p>I expect this to be nontrivial (possibly depending on which octant you're in), but please try a few possibilities before saying it's wrong. Good luck!</p> <hr> <p>Here is the code that finally worked for me.</p> <p>I noticed a "flip" effect that occurred when the object moved from any front quadrant (positive Z) to any back quadrant. In the front quadrants the <strong>front</strong> of the object would always face the point. In the back quadrants the <strong>back</strong> of the object always faces the point.</p> <p>This code corrects the flip effect so the front of the object <strong>always</strong> faces the point. I encountered it through trial-and-error so I don't really know what's happening!</p> <pre><code> rotx = Math.atan2( y, z ); if (z &gt;= 0) { roty = -Math.atan2( x * Math.cos(rotx), z ); }else{ roty = Math.atan2( x * Math.cos(rotx), -z ); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload