| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Christian Meyer |
|---|
| 13 | co-programmer: Patrick Boenzli : Vector::scale() |
|---|
| 14 | Vector::abs() |
|---|
| 15 | |
|---|
| 16 | Quaternion code borrowed from an Gamasutra article by Nick Bobick and Ken Shoemake |
|---|
| 17 | |
|---|
| 18 | 2005-06-02: Benjamin Grauer: speed up, and new Functionality to Vector (mostly inline now) |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_MATH |
|---|
| 22 | |
|---|
| 23 | #include "quaternion.h" |
|---|
| 24 | #ifdef DEBUG |
|---|
| 25 | #include "debug.h" |
|---|
| 26 | #else |
|---|
| 27 | #include <stdio.h> |
|---|
| 28 | #define PRINT(x) printf |
|---|
| 29 | #endif |
|---|
| 30 | |
|---|
| 31 | ///////////////// |
|---|
| 32 | /* QUATERNIONS */ |
|---|
| 33 | ///////////////// |
|---|
| 34 | /** |
|---|
| 35 | * @brief calculates a lookAt rotation |
|---|
| 36 | * @param dir: the direction you want to look |
|---|
| 37 | * @param up: specify what direction up should be |
|---|
| 38 | * |
|---|
| 39 | * Mathematically this determines the rotation a (0,0,1)-Vector has to undergo to point |
|---|
| 40 | * the same way as dir. If you want to use this with cameras, you'll have to reverse the |
|---|
| 41 | * dir Vector (Vector(0,0,0) - your viewing direction) or you'll point the wrong way. You |
|---|
| 42 | * can use this for meshes as well (then you do not have to reverse the vector), but keep |
|---|
| 43 | * in mind that if you do that, the model's front has to point in +z direction, and left |
|---|
| 44 | * and right should be -x or +x respectively or the mesh wont rotate correctly. |
|---|
| 45 | * |
|---|
| 46 | * @TODO !!! OPTIMIZE THIS !!! |
|---|
| 47 | */ |
|---|
| 48 | Quaternion::Quaternion (const Vector& dir, const Vector& up) |
|---|
| 49 | { |
|---|
| 50 | Vector z = dir.getNormalized(); |
|---|
| 51 | Vector x = up.cross(z).getNormalized(); |
|---|
| 52 | Vector y = z.cross(x); |
|---|
| 53 | |
|---|
| 54 | float m[4][4]; |
|---|
| 55 | m[0][0] = x.x; |
|---|
| 56 | m[0][1] = x.y; |
|---|
| 57 | m[0][2] = x.z; |
|---|
| 58 | m[0][3] = 0.0; |
|---|
| 59 | m[1][0] = y.x; |
|---|
| 60 | m[1][1] = y.y; |
|---|
| 61 | m[1][2] = y.z; |
|---|
| 62 | m[1][3] = 0.0; |
|---|
| 63 | m[2][0] = z.x; |
|---|
| 64 | m[2][1] = z.y; |
|---|
| 65 | m[2][2] = z.z; |
|---|
| 66 | m[2][3] = 0.0; |
|---|
| 67 | m[3][0] = 0.0; |
|---|
| 68 | m[3][1] = 0.0; |
|---|
| 69 | m[3][2] = 0.0; |
|---|
| 70 | m[3][3] = 1.0; |
|---|
| 71 | |
|---|
| 72 | this->from4x4(m); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * @brief calculates a rotation from euler angles |
|---|
| 77 | * @param roll: the roll in radians |
|---|
| 78 | * @param pitch: the pitch in radians |
|---|
| 79 | * @param yaw: the yaw in radians |
|---|
| 80 | */ |
|---|
| 81 | Quaternion::Quaternion (float attitude, float heading, float bank) |
|---|
| 82 | { |
|---|
| 83 | /* |
|---|
| 84 | float cr, cp, cy, sr, sp, sy, cpcy, spsy; |
|---|
| 85 | |
|---|
| 86 | // calculate trig identities |
|---|
| 87 | cr = cos(roll/2); |
|---|
| 88 | cp = cos(pitch/2); |
|---|
| 89 | cy = cos(yaw/2); |
|---|
| 90 | |
|---|
| 91 | sr = sin(roll/2); |
|---|
| 92 | sp = sin(pitch/2); |
|---|
| 93 | sy = sin(yaw/2); |
|---|
| 94 | |
|---|
| 95 | cpcy = cp * cy; |
|---|
| 96 | spsy = sp * sy; |
|---|
| 97 | |
|---|
| 98 | w = cr * cpcy + sr * spsy; |
|---|
| 99 | v.x = sr * cpcy - cr * spsy; |
|---|
| 100 | v.y = cr * sp * cy + sr * cp * sy; |
|---|
| 101 | v.z = cr * cp * sy - sr * sp * cy; |
|---|
| 102 | */ |
|---|
| 103 | float c1, c2, c3, s1, s2, s3; |
|---|
| 104 | |
|---|
| 105 | c1 = cos(heading / 2); |
|---|
| 106 | c2 = cos(attitude / 2); |
|---|
| 107 | c3 = cos(bank / 2); |
|---|
| 108 | |
|---|
| 109 | s1 = sin(heading / 2); |
|---|
| 110 | s2 = sin(attitude / 2); |
|---|
| 111 | s3 = sin(bank / 2); |
|---|
| 112 | |
|---|
| 113 | w = c1 * c2 * c3 - s1 * s2 * s3; |
|---|
| 114 | v.x = s1 * s2 * c3 +c1 * c2 * s3; |
|---|
| 115 | v.y = s1 * c2 * c3 + c1 * s2 * s3; |
|---|
| 116 | v.z = c1 * s2 * c3 - s1 * c2 * s3; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * @brief convert the Quaternion to a 4x4 rotational glMatrix |
|---|
| 122 | * @param m: a buffer to store the Matrix in |
|---|
| 123 | */ |
|---|
| 124 | void Quaternion::matrix (float m[4][4]) const |
|---|
| 125 | { |
|---|
| 126 | float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2; |
|---|
| 127 | |
|---|
| 128 | // calculate coefficients |
|---|
| 129 | x2 = v.x + v.x; |
|---|
| 130 | y2 = v.y + v.y; |
|---|
| 131 | z2 = v.z + v.z; |
|---|
| 132 | xx = v.x * x2; xy = v.x * y2; xz = v.x * z2; |
|---|
| 133 | yy = v.y * y2; yz = v.y * z2; zz = v.z * z2; |
|---|
| 134 | wx = w * x2; wy = w * y2; wz = w * z2; |
|---|
| 135 | |
|---|
| 136 | m[0][0] = 1.0 - (yy + zz); m[1][0] = xy - wz; |
|---|
| 137 | m[2][0] = xz + wy; m[3][0] = 0.0; |
|---|
| 138 | |
|---|
| 139 | m[0][1] = xy + wz; m[1][1] = 1.0 - (xx + zz); |
|---|
| 140 | m[2][1] = yz - wx; m[3][1] = 0.0; |
|---|
| 141 | |
|---|
| 142 | m[0][2] = xz - wy; m[1][2] = yz + wx; |
|---|
| 143 | m[2][2] = 1.0 - (xx + yy); m[3][2] = 0.0; |
|---|
| 144 | |
|---|
| 145 | m[0][3] = 0; m[1][3] = 0; |
|---|
| 146 | m[2][3] = 0; m[3][3] = 1; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | /** |
|---|
| 152 | * @brief Slerps this QUaternion performs a smooth move. |
|---|
| 153 | * @param toQuat to this Quaternion |
|---|
| 154 | * @param t \% inth the the direction[0..1] |
|---|
| 155 | */ |
|---|
| 156 | void Quaternion::slerpTo(const Quaternion& toQuat, float t) |
|---|
| 157 | { |
|---|
| 158 | float tol[4]; |
|---|
| 159 | double omega, cosom, sinom, scale0, scale1; |
|---|
| 160 | // float DELTA = 0.2; |
|---|
| 161 | |
|---|
| 162 | cosom = this->v.x * toQuat.v.x + this->v.y * toQuat.v.y + this->v.z * toQuat.v.z + this->w * toQuat.w; |
|---|
| 163 | |
|---|
| 164 | if( cosom < 0.0 ) |
|---|
| 165 | { |
|---|
| 166 | cosom = -cosom; |
|---|
| 167 | tol[0] = -toQuat.v.x; |
|---|
| 168 | tol[1] = -toQuat.v.y; |
|---|
| 169 | tol[2] = -toQuat.v.z; |
|---|
| 170 | tol[3] = -toQuat.w; |
|---|
| 171 | } |
|---|
| 172 | else |
|---|
| 173 | { |
|---|
| 174 | tol[0] = toQuat.v.x; |
|---|
| 175 | tol[1] = toQuat.v.y; |
|---|
| 176 | tol[2] = toQuat.v.z; |
|---|
| 177 | tol[3] = toQuat.w; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | omega = acos(cosom); |
|---|
| 181 | sinom = sin(omega); |
|---|
| 182 | scale0 = sin((1.0 - t) * omega) / sinom; |
|---|
| 183 | scale1 = sin(t * omega) / sinom; |
|---|
| 184 | this->v = Vector(scale0 * this->v.x + scale1 * tol[0], |
|---|
| 185 | scale0 * this->v.y + scale1 * tol[1], |
|---|
| 186 | scale0 * this->v.z + scale1 * tol[2]); |
|---|
| 187 | this->w = scale0 * this->w + scale1 * tol[3]; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | /** |
|---|
| 192 | * @brief performs a smooth move. |
|---|
| 193 | * @param from where |
|---|
| 194 | * @param to where |
|---|
| 195 | * @param t the time this transformation should take value [0..1] |
|---|
| 196 | * @returns the Result of the smooth move |
|---|
| 197 | */ |
|---|
| 198 | Quaternion Quaternion::quatSlerp(const Quaternion& from, const Quaternion& to, float t) |
|---|
| 199 | { |
|---|
| 200 | float tol[4]; |
|---|
| 201 | double omega, cosom, sinom, scale0, scale1; |
|---|
| 202 | // float DELTA = 0.2; |
|---|
| 203 | |
|---|
| 204 | cosom = from.v.x * to.v.x + from.v.y * to.v.y + from.v.z * to.v.z + from.w * to.w; |
|---|
| 205 | |
|---|
| 206 | if( cosom < 0.0 ) |
|---|
| 207 | { |
|---|
| 208 | cosom = -cosom; |
|---|
| 209 | tol[0] = -to.v.x; |
|---|
| 210 | tol[1] = -to.v.y; |
|---|
| 211 | tol[2] = -to.v.z; |
|---|
| 212 | tol[3] = -to.w; |
|---|
| 213 | } |
|---|
| 214 | else |
|---|
| 215 | { |
|---|
| 216 | tol[0] = to.v.x; |
|---|
| 217 | tol[1] = to.v.y; |
|---|
| 218 | tol[2] = to.v.z; |
|---|
| 219 | tol[3] = to.w; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | omega = acos(cosom); |
|---|
| 223 | sinom = sin(omega); |
|---|
| 224 | scale0 = sin((1.0 - t) * omega) / sinom; |
|---|
| 225 | scale1 = sin(t * omega) / sinom; |
|---|
| 226 | return Quaternion(Vector(scale0 * from.v.x + scale1 * tol[0], |
|---|
| 227 | scale0 * from.v.y + scale1 * tol[1], |
|---|
| 228 | scale0 * from.v.z + scale1 * tol[2]), |
|---|
| 229 | scale0 * from.w + scale1 * tol[3]); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | /** |
|---|
| 233 | * @returns the Heading |
|---|
| 234 | */ |
|---|
| 235 | float Quaternion::getHeading() const |
|---|
| 236 | { |
|---|
| 237 | float pole = this->v.x*this->v.y + this->v.z*this->w; |
|---|
| 238 | if (fabsf(pole) != 0.5) |
|---|
| 239 | return atan2(2.0* (v.y*w - v.x*v.z), 1 - 2.0*(v.y*v.y - v.z*v.z)); |
|---|
| 240 | else if (pole == .5) // North Pole |
|---|
| 241 | return 2.0 * atan2(v.x, w); |
|---|
| 242 | else // South Pole |
|---|
| 243 | return -2.0 * atan2(v.x, w); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | /** |
|---|
| 247 | * @returns the Heading-Quaternion |
|---|
| 248 | */ |
|---|
| 249 | Quaternion Quaternion::getHeadingQuat() const |
|---|
| 250 | { |
|---|
| 251 | return Quaternion(this->getHeading(), Vector(0,1,0)); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | /** |
|---|
| 255 | * @returns the Attitude |
|---|
| 256 | */ |
|---|
| 257 | float Quaternion::getAttitude() const |
|---|
| 258 | { |
|---|
| 259 | return asin(2.0 * (v.x*v.y + v.z*w)); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | /** |
|---|
| 263 | * @returns the Attitude-Quaternion |
|---|
| 264 | */ |
|---|
| 265 | Quaternion Quaternion::getAttitudeQuat() const |
|---|
| 266 | { |
|---|
| 267 | return Quaternion(this->getAttitude(), Vector(0,0,1)); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | /** |
|---|
| 272 | * @returns the Bank |
|---|
| 273 | */ |
|---|
| 274 | float Quaternion::getBank() const |
|---|
| 275 | { |
|---|
| 276 | if (fabsf(this->v.x*this->v.y + this->v.z*this->w) != 0.5) |
|---|
| 277 | return atan2(2.0*(v.x*w-v.y*v.z) , 1 - 2.0*(v.x*v.x - v.z*v.z)); |
|---|
| 278 | else |
|---|
| 279 | return 0.0f; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | /** |
|---|
| 283 | * @returns the Bank-Quaternion |
|---|
| 284 | */ |
|---|
| 285 | Quaternion Quaternion::getBankQuat() const |
|---|
| 286 | { |
|---|
| 287 | return Quaternion(this->getBank(), Vector(1,0,0)); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | /** |
|---|
| 291 | * @returns Bank, Attitude, Heading |
|---|
| 292 | */ |
|---|
| 293 | Vector Quaternion::getRotation() const |
|---|
| 294 | { |
|---|
| 295 | return Vector(getAttitude(), getHeading(), getBank()); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | /** |
|---|
| 300 | * @brief convert a rotational 4x4 glMatrix into a Quaternion |
|---|
| 301 | * @param m: a 4x4 matrix in glMatrix order |
|---|
| 302 | */ |
|---|
| 303 | void Quaternion::from4x4(float m[4][4]) |
|---|
| 304 | { |
|---|
| 305 | |
|---|
| 306 | float tr, s, q[4]; |
|---|
| 307 | int i, j, k; |
|---|
| 308 | |
|---|
| 309 | static int nxt[3] = {1, 2, 0}; |
|---|
| 310 | |
|---|
| 311 | tr = m[0][0] + m[1][1] + m[2][2]; |
|---|
| 312 | |
|---|
| 313 | // check the diagonal |
|---|
| 314 | if (tr > 0.0) |
|---|
| 315 | { |
|---|
| 316 | s = sqrt (tr + 1.0); |
|---|
| 317 | w = s / 2.0; |
|---|
| 318 | s = 0.5 / s; |
|---|
| 319 | v.x = (m[1][2] - m[2][1]) * s; |
|---|
| 320 | v.y = (m[2][0] - m[0][2]) * s; |
|---|
| 321 | v.z = (m[0][1] - m[1][0]) * s; |
|---|
| 322 | } |
|---|
| 323 | else |
|---|
| 324 | { |
|---|
| 325 | // diagonal is negative |
|---|
| 326 | i = 0; |
|---|
| 327 | if (m[1][1] > m[0][0]) i = 1; |
|---|
| 328 | if (m[2][2] > m[i][i]) i = 2; |
|---|
| 329 | j = nxt[i]; |
|---|
| 330 | k = nxt[j]; |
|---|
| 331 | |
|---|
| 332 | s = sqrt ((m[i][i] - (m[j][j] + m[k][k])) + 1.0); |
|---|
| 333 | |
|---|
| 334 | q[i] = s * 0.5; |
|---|
| 335 | |
|---|
| 336 | if (s != 0.0) s = 0.5 / s; |
|---|
| 337 | |
|---|
| 338 | q[3] = (m[j][k] - m[k][j]) * s; |
|---|
| 339 | q[j] = (m[i][j] + m[j][i]) * s; |
|---|
| 340 | q[k] = (m[i][k] + m[k][i]) * s; |
|---|
| 341 | |
|---|
| 342 | v.x = q[0]; |
|---|
| 343 | v.y = q[1]; |
|---|
| 344 | v.z = q[2]; |
|---|
| 345 | w = q[3]; |
|---|
| 346 | } |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | /** |
|---|
| 351 | * applies a quaternion from a 3x3 rotation matrix. |
|---|
| 352 | * @param mat The 3x3 source rotation matrix. |
|---|
| 353 | * @return The equivalent 4 float quaternion. |
|---|
| 354 | */ |
|---|
| 355 | void Quaternion::from3x3(float mat[3][3]) |
|---|
| 356 | { |
|---|
| 357 | int NXT[] = {1, 2, 0}; |
|---|
| 358 | float q[4]; |
|---|
| 359 | |
|---|
| 360 | // check the diagonal |
|---|
| 361 | float tr = mat[0][0] + mat[1][1] + mat[2][2]; |
|---|
| 362 | if( tr > 0.0f) |
|---|
| 363 | { |
|---|
| 364 | float s = (float)sqrtf(tr + 1.0f); |
|---|
| 365 | this->w = s * 0.5f; |
|---|
| 366 | s = 0.5f / s; |
|---|
| 367 | this->v.x = (mat[1][2] - mat[2][1]) * s; |
|---|
| 368 | this->v.y = (mat[2][0] - mat[0][2]) * s; |
|---|
| 369 | this->v.z = (mat[0][1] - mat[1][0]) * s; |
|---|
| 370 | } |
|---|
| 371 | else |
|---|
| 372 | { |
|---|
| 373 | // diagonal is negative |
|---|
| 374 | // get biggest diagonal element |
|---|
| 375 | int i = 0; |
|---|
| 376 | if (mat[1][1] > mat[0][0]) i = 1; |
|---|
| 377 | if (mat[2][2] > mat[i][i]) i = 2; |
|---|
| 378 | //setup index sequence |
|---|
| 379 | int j = NXT[i]; |
|---|
| 380 | int k = NXT[j]; |
|---|
| 381 | |
|---|
| 382 | float s = (float)sqrtf((mat[i][i] - (mat[j][j] + mat[k][k])) + 1.0f); |
|---|
| 383 | |
|---|
| 384 | q[i] = s * 0.5f; |
|---|
| 385 | |
|---|
| 386 | if (s != 0.0f) s = 0.5f / s; |
|---|
| 387 | |
|---|
| 388 | q[j] = (mat[i][j] + mat[j][i]) * s; |
|---|
| 389 | q[k] = (mat[i][k] + mat[k][i]) * s; |
|---|
| 390 | q[3] = (mat[j][k] - mat[k][j]) * s; |
|---|
| 391 | |
|---|
| 392 | this->v.x = q[0]; |
|---|
| 393 | this->v.y = q[1]; |
|---|
| 394 | this->v.z = q[2]; |
|---|
| 395 | this->w = q[3]; |
|---|
| 396 | } |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | Quaternion Quaternion::lookAt(Vector from, Vector to, Vector up) |
|---|
| 400 | { |
|---|
| 401 | Vector n = to - from; |
|---|
| 402 | n.normalize(); |
|---|
| 403 | Vector v = n.cross(up); |
|---|
| 404 | v.normalize(); |
|---|
| 405 | Vector u = v.cross(n); |
|---|
| 406 | |
|---|
| 407 | float matrix[3][3]; |
|---|
| 408 | matrix[0][0] = v.x; |
|---|
| 409 | matrix[0][1] = v.y; |
|---|
| 410 | matrix[0][2] = v.z; |
|---|
| 411 | matrix[1][0] = u.x; |
|---|
| 412 | matrix[1][1] = u.y; |
|---|
| 413 | matrix[1][2] = u.z; |
|---|
| 414 | matrix[2][0] = -n.x; |
|---|
| 415 | matrix[2][1] = -n.y; |
|---|
| 416 | matrix[2][2] = -n.z; |
|---|
| 417 | |
|---|
| 418 | Quaternion quat; |
|---|
| 419 | quat.from3x3(matrix); |
|---|
| 420 | return quat; |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | /** |
|---|
| 425 | * @brief outputs some nice formated debug information about this quaternion |
|---|
| 426 | */ |
|---|
| 427 | void Quaternion::debug() const |
|---|
| 428 | { |
|---|
| 429 | PRINT(0)("real a=%f; imag: x=%f y=%f z=%f\n", w, v.x, v.y, v.z); |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | /** |
|---|
| 433 | * @brief another better Quaternion Debug Function. |
|---|
| 434 | */ |
|---|
| 435 | void Quaternion::debug2() const |
|---|
| 436 | { |
|---|
| 437 | Vector axis = this->getSpacialAxis(); |
|---|
| 438 | PRINT(0)("angle = %f, axis: ax=%f, ay=%f, az=%f\n", this->getSpacialAxisAngle(), axis.x, axis.y, axis.z ); |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | |
|---|