- 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.
- JavaScript從入門到精通(微視頻精編版)
- 精通JavaScript+jQuery:100%動態(tài)網(wǎng)頁設(shè)計密碼
- Java 9 Concurrency Cookbook(Second Edition)
- Java面向?qū)ο筌浖_發(fā)
- Java EE框架整合開發(fā)入門到實戰(zhàn):Spring+Spring MVC+MyBatis(微課版)
- Mastering Python High Performance
- 名師講壇:Java微服務(wù)架構(gòu)實戰(zhàn)(SpringBoot+SpringCloud+Docker+RabbitMQ)
- PhpStorm Cookbook
- C# 8.0核心技術(shù)指南(原書第8版)
- Internet of Things with ESP8266
- iPhone應(yīng)用開發(fā)從入門到精通
- 鴻蒙OS應(yīng)用編程實戰(zhàn)
- 算法圖解
- JavaScript編程精解(原書第2版)
- Java EE實用教程