Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculating the Angle Between a 3D Object and a Point
    primarykey
    data
    text
    <p>I have a 3D object in DirectX11 with a position vector and a rotation angle for the direction it's facing (it only rotates around the Y axis).</p> <pre><code>D3DXVECTOR3 m_position; float m_angle; </code></pre> <p>If I then wanted to rotate the object to face something then I'd need to find the angle between the direction it's facing and the direction it needs to face using the dot product on the two normalised vectors.</p> <p>The thing I'm having a problem with is how I find the direction the object is currently facing with just its position and the angle. What I currently have is:</p> <pre><code>D3DXVECTOR3 normDirection, normTarget; D3DXVec3Normalize( &amp;normDirection, ????); D3DXVec3Normalize( &amp;normTarget, &amp;(m_position-target)); // I store the values in degrees because I prefer it float angleToRotate = D3DXToDegree( acos( D3DXVec3Dot( &amp;normDirection, &amp;normTarget))); </code></pre> <p>Does anyone know how I get the vector for the current direction it's facing from the values I have, or do I need to re-write it so I keep track of the object's direction vector?</p> <p>EDIT: Changed 'cos' to 'acos'.</p> <p>SOLUTION (with the assistance of user2802841):</p> <pre><code>// assuming these are member variables D3DXVECTOR3 m_position; D3DXVECTOR3 m_rotation; D3DXVECTOR3 m_direction; // and these are local variables D3DXVECTOR3 target; // passed in as a parameter D3DXVECTOR3 targetNorm; D3DXVECTOR3 upVector; float angleToRotate; // find the amount to rotate by D3DXVec3Normalize( &amp;targetNorm, &amp;(target-m_position)); angleToRotate = D3DXToDegree( acos( D3DXVec3Dot( &amp;targetNorm, &amp;m_direction))); // calculate the up vector between the two vectors D3DXVec3Cross( &amp;upVector, &amp;m_direction, &amp;targetNorm); // switch the angle to negative if the up vector is going down if( upVector.y &lt; 0) angleToRotate *= -1; // add the rotation to the object's overall rotation m_rotation.y += angleToRotate; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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