- Hands-On C++ Game Animation Programming
- Gabor Szauer
- 218字
- 2021-06-30 14:45:51
The angle between vectors
If two vectors are of unit length, the angle between them is the cosine of their dot product:

If the two vectors are not normalized, the dot product needs to be divided by the product of the length of both vectors:

To find the actual angle, not just the cosine of it, we need to take the inverse of the cosine on both sides, which is the arccosine function:

Implement the angle function in vec3.cpp. Don't forget to add the function declaration to vec3.h:
float angle(const vec3 &l, const vec3 &r) {
float sqMagL = l.x * l.x + l.y * l.y + l.z * l.z;
float sqMagR = r.x * r.x + r.y * r.y + r.z * r.z;
if (sqMagL<VEC3_EPSILON || sqMagR<VEC3_EPSILON) {
return 0.0f;
}
float dot = l.x * r.x + l.y * r.y + l.z * r.z;
float len = sqrtf(sqMagL) * sqrtf(sqMagR);
return acosf(dot / len);
}
Important note:
The acosf function returns angles in radians. To convert radians to degrees, multiply by 57.2958f. To convert degrees to radians, multiply by 0.0174533f.
- Modular Programming with Python
- 在最好的年紀學Python:小學生趣味編程
- TensorFlow Lite移動端深度學習
- Python Game Programming By Example
- Learning SAP Analytics Cloud
- Java Web應用開發(fā)技術與案例教程(第2版)
- C語言程序設計上機指導與習題解答(第2版)
- Troubleshooting Citrix XenApp?
- Unity Character Animation with Mecanim
- Visual Basic程序設計全程指南
- 玩轉(zhuǎn).NET Micro Framework移植:基于STM32F10x處理器
- The Statistics and Calculus with Python Workshop
- Python預測之美:數(shù)據(jù)分析與算法實戰(zhàn)(雙色)
- 走近SDN/NFV
- 程序員的算法趣題2