简体   繁体   English

使用点积计算两个向量之间的角度

[英]Calculating the Angle Between Two vectors Using Dot Product

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. 我正在尝试计算两个向量之间的角度,以便我可以在3D空间中的对象方向上旋转角色。 I have two vectors( character & object), loc_look, and modelPos respectively. 我有两个向量(字符和对象),loc_look和modelPos。 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); 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). 我的问题似乎就出现了,因为我应用于角色的外观向量(loc_look)的旋转超过了对象位置(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). 另外需要注意的是,modelPos.x每次迭代增加0.1,因此角色在沿x轴移动时将面向对象......现在,当我运行程序时,在第一次迭代中一切都很好(我没有旋转了角色)。 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). 在第二次迭代中,loc_look.x值大于modelPos.x值(我使用TARGET函数中的点积计算指定的角度旋转了太多字符)。 Therefore on the second iteration my code will rotate the character left to adjust for the difference in the vectors' x values... 因此,在第二次迭代中,我的代码将旋转左边的字符以调整矢量的x值的差异...

How can I tighten up the measurements so that I do not rotate my character's look vector by too great a value? 如何收紧测量结果,以便我不会通过太大的值旋转角色的外观矢量?

The dot product is the cosine of the angle between two vectors only if they are unit vectors. 只有当它们是单位向量时,点积才是两个向量之间角度的余弦。 Please see this: 请看这个:

http://en.wikipedia.org/wiki/Dot_product#Geometric_interpretation http://en.wikipedia.org/wiki/Dot_product#Geometric_interpretation

I see you have some commented out line: 我看到你有一些注释掉的行:

 //D3DXVec3Normalize(&loc_look, &loc_look); 

But you do need to normalize both vectors. 但是你需要对两个向量进行标准化。

Think about it. 想一想。 If the vectors are all scaled by a constant factor, the dot product gets bigger, right? 如果向量都按常数因子缩放,则点积变大,对吧? And so the value going into arccos is bigger. 因此,进入arccos的价值更大。 But the angle is the same, so that's obviously wrong. 但角度是一样的,所以这显然是错误的。

The approximation you are talking about is normal for floating point math. 您所谈论的近似值对于浮点数学来说是正常的。 You need to factor in an "epsilon" value so your character does not twitch around after the dot product is close to solved. 你需要考虑一个“epsilon”值,这样你的角色在点积接近解决后不会抽搐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM