- 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.
- 從零開(kāi)始構(gòu)建企業(yè)級(jí)RAG系統(tǒng)
- HTML5+CSS3王者歸來(lái)
- Node.js+Webpack開(kāi)發(fā)實(shí)戰(zhàn)
- C#高級(jí)編程(第10版) C# 6 & .NET Core 1.0 (.NET開(kāi)發(fā)經(jīng)典名著)
- 數(shù)據(jù)庫(kù)系統(tǒng)教程(第2版)
- C語(yǔ)言程序設(shè)計(jì)(第3版)
- C語(yǔ)言程序設(shè)計(jì)習(xí)題解析與上機(jī)指導(dǎo)(第4版)
- 新一代通用視頻編碼H.266/VVC:原理、標(biāo)準(zhǔn)與實(shí)現(xiàn)
- JMeter 性能測(cè)試實(shí)戰(zhàn)(第2版)
- Gradle for Android
- 微信小程序項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)
- Unity UI Cookbook
- Mastering React
- Flowable流程引擎實(shí)戰(zhàn)
- Beginning C++ Game Programming