Calculating the Angle Between Two vectors Using Dot Product

Posted by P. Avery on Stack Overflow See other posts from Stack Overflow or by P. Avery
Published on 2012-03-20T22:24:34Z Indexed on 2012/03/20 23:29 UTC
Read the original article Hit count: 166

Filed under:
|
|
|

I'm trying to calculate the angle between two vectors so that I can rotate a character in the direction of an object in 3D space. I have two vectors( character & object), loc_look, and modelPos respectively. For simplicity's sake I am only trying to rotate along the up axis...yaw. loc_look = D3DXVECTOR3 (0, 0, 1), modelPos = D3DXVECTOR3 (0, 0, 15);

I have written this code which seems to be the correct calculations. My problem arises, seemingly, because the rotation I apply to the character's look vector(loc_look) exceeds the value of the object's position (modelPos). Here is my code:

BOOL CEntity::TARGET()   
{   
    if(graphics.m_model->m_enemy)   
    {   
        D3DXVECTOR3 modelPos = graphics.m_model->position;   
        D3DXVec3Normalize(&modelPos, &modelPos);   

        //D3DXVec3Normalize(&loc_look, &loc_look);   
        float dot = D3DXVec3Dot(&loc_look, &modelPos);   
        float yaw = acos(dot);   
        BOOL neg = (loc_look.x > modelPos.x) ? true : false;   
        switch ( neg )   
        {   
        case false:   
            Yaw(yaw);   
            return true;   
        case true:   
            Yaw(-yaw);   
            return true;   
        }          
    }   
    else  
        return false;   
}

I rotate the character's orientation matrix with the following code:

void CEntity::CalculateOrientationMatrix(D3DXMATRIX *orientationMatrix)   

{    

D3DXMatrixRotationAxis(&rotY, &loc_up, loc_yaw);   

D3DXVec3TransformCoord(&loc_look, &loc_look, &rotY);   

D3DXVec3TransformCoord(&loc_right, &loc_right, &rotY);   


D3DXMatrixRotationAxis(&rotX, &loc_right, loc_pitch);   

D3DXVec3TransformCoord(&loc_look, &loc_look, &rotX);   

D3DXVec3TransformCoord(&loc_up, &loc_up, &rotX);   


D3DXMatrixRotationAxis(&rotZ, &loc_look, loc_roll);    

D3DXVec3TransformCoord(&loc_up, &loc_up, &rotZ);   

D3DXVec3TransformCoord(&loc_right, &loc_right, &rotZ);   

*orientationMatrix *= rotX * rotY * rotZ;   


orientationMatrix->_41 = loc_position.x;   

orientationMatrix->_42 = loc_position.y;   

orientationMatrix->_43 = loc_position.z;   


//D3DXVec3Normalize(&loc_look, &loc_look);   

SetYawPitchRoll(0,0,0); // Reset Yaw, Pitch, & Roll Amounts   

}

Also to note, the modelPos.x increases by 0.1 each iteration so the character will face the object as it moves along the x-axis... Now, when I run program, in the first iteration everything is fine(I haven't rotated the character yet). On the second iteration, the loc_look.x value is greater than the modelPos.x value(I rotated the character too much using the angle specified with the dot product calculations in the TARGET function). Therefore on the second iteration my code will rotate the character left to adjust for the difference in the vectors' x values...

How can I tighten up the measurements so that I do not rotate my character's look vector by too great a value?

© Stack Overflow or respective owner

Related posts about c++

Related posts about winapi