- Hands-On C++ Game Animation Programming
- Gabor Szauer
- 377字
- 2021-06-30 14:46:01
Comparison operations
Comparing two quaternions can be done component-wise. Two quaternions can represent the same rotation even if they are not identical on a component level. This happens because a quaternion and its inverse rotate to the same spot but they take different routes:
- Overload the == and != operators in quat.cpp. Add the declaration for these functions to quat.h:
bool operator==(const quat& left, const quat& right) {
return (fabsf(left.x - right.x) <= QUAT_EPSILON &&
fabsf(left.y - right.y) <= QUAT_EPSILON &&
fabsf(left.z - right.z) <= QUAT_EPSILON &&
fabsf(left.w - right.w) <= QUAT_EPSILON);
}
bool operator!=(const quat& a, const quat& b) {
return !(a == b);
}
- To test whether two quaternions represent the same rotation, the absolute difference between the two needs to be tested. Implement the sameOrientation function in quat.cpp. Add the function declaration to quat.h:
bool sameOrientation(const quat&l, const quat&r) {
return (fabsf(l.x - r.x) <= QUAT_EPSILON &&
fabsf(l.y - r.y) <= QUAT_EPSILON &&
fabsf(l.z - r.z) <= QUAT_EPSILON &&
fabsf(l.w - r.w) <= QUAT_EPSILON) ||
(fabsf(l.x + r.x) <= QUAT_EPSILON &&
fabsf(l.y + r.y) <= QUAT_EPSILON &&
fabsf(l.z + r.z) <= QUAT_EPSILON &&
fabsf(l.w + r.w) <= QUAT_EPSILON);
}
Most of the time, you will want to use the equality operator to compare quaternions. The sameOrientation function is not as useful because the rotation that a quaternion takes can be changed if the quaternion is inverted.
In the next section, you will learn how to implement a quaternion dot product.
- 玩轉(zhuǎn)Scratch少兒趣味編程
- LaTeX Cookbook
- Python快樂(lè)編程:人工智能深度學(xué)習(xí)基礎(chǔ)
- Go語(yǔ)言高效編程:原理、可觀測(cè)性與優(yōu)化
- MATLAB圖像處理超級(jí)學(xué)習(xí)手冊(cè)
- MySQL數(shù)據(jù)庫(kù)管理與開(kāi)發(fā)(慕課版)
- Developing SSRS Reports for Dynamics AX
- Deep Learning with R Cookbook
- PHP與MySQL權(quán)威指南
- 游戲設(shè)計(jì)的底層邏輯
- Java 11 and 12:New Features
- Flutter之旅
- 可視化H5頁(yè)面設(shè)計(jì)與制作:Mugeda標(biāo)準(zhǔn)教程
- 絕密原型檔案:看看專(zhuān)業(yè)產(chǎn)品經(jīng)理的原型是什么樣
- Java面試一戰(zhàn)到底(基礎(chǔ)卷)