Changeset 7348 in orxonox.OLD for trunk/src/lib/math
- Timestamp:
- Apr 19, 2006, 2:55:29 PM (19 years ago)
- Location:
- trunk/src/lib/math
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/math/quaternion.cc
r7191 r7348 35 35 ///////////////// 36 36 /** 37 * calculates a lookAt rotation37 * @brief calculates a lookAt rotation 38 38 * @param dir: the direction you want to look 39 39 * @param up: specify what direction up should be 40 41 42 43 44 45 46 40 * 41 * Mathematically this determines the rotation a (0,0,1)-Vector has to undergo to point 42 * the same way as dir. If you want to use this with cameras, you'll have to reverse the 43 * dir Vector (Vector(0,0,0) - your viewing direction) or you'll point the wrong way. You 44 * can use this for meshes as well (then you do not have to reverse the vector), but keep 45 * in mind that if you do that, the model's front has to point in +z direction, and left 46 * and right should be -x or +x respectively or the mesh wont rotate correctly. 47 47 * 48 48 * @TODO !!! OPTIMIZE THIS !!! … … 76 76 77 77 /** 78 * calculates a rotation from euler angles78 * @brief calculates a rotation from euler angles 79 79 * @param roll: the roll in radians 80 80 * @param pitch: the pitch in radians … … 104 104 105 105 /** 106 * convert the Quaternion to a 4x4 rotational glMatrix106 * @brief convert the Quaternion to a 4x4 rotational glMatrix 107 107 * @param m: a buffer to store the Matrix in 108 108 */ … … 134 134 135 135 /** 136 * Slerps this QUaternion performs a smooth move.136 * @brief Slerps this QUaternion performs a smooth move. 137 137 * @param toQuat to this Quaternion 138 138 * @param t \% inth the the direction[0..1] … … 167 167 scale1 = sin(t * omega) / sinom; 168 168 this->v = Vector(scale0 * this->v.x + scale1 * tol[0], 169 170 169 scale0 * this->v.y + scale1 * tol[1], 170 scale0 * this->v.z + scale1 * tol[2]); 171 171 this->w = scale0 * this->w + scale1 * tol[3]; 172 172 } … … 174 174 175 175 /** 176 * performs a smooth move.176 * @brief performs a smooth move. 177 177 * @param from where 178 178 * @param to where … … 189 189 190 190 if( cosom < 0.0 ) 191 192 193 194 195 196 197 191 { 192 cosom = -cosom; 193 tol[0] = -to.v.x; 194 tol[1] = -to.v.y; 195 tol[2] = -to.v.z; 196 tol[3] = -to.w; 197 } 198 198 else 199 200 201 202 203 204 199 { 200 tol[0] = to.v.x; 201 tol[1] = to.v.y; 202 tol[2] = to.v.z; 203 tol[3] = to.w; 204 } 205 205 206 206 omega = acos(cosom); … … 209 209 scale1 = sin(t * omega) / sinom; 210 210 return Quaternion(Vector(scale0 * from.v.x + scale1 * tol[0], 211 scale0 * from.v.y + scale1 * tol[1],212 scale0 * from.v.z + scale1 * tol[2]),211 scale0 * from.v.y + scale1 * tol[1], 212 scale0 * from.v.z + scale1 * tol[2]), 213 213 scale0 * from.w + scale1 * tol[3]); 214 214 } 215 215 216 217 /** 218 * convert a rotational 4x4 glMatrix into a Quaternion 216 /** 217 * @returns the heading 218 */ 219 float Quaternion::getHeading() const 220 { 221 float pole = this->v.x*this->v.y + this->v.z*this->w; 222 if (fabsf(pole) != 0.5) 223 return atan2(2.0* (v.y*w - v.x*v.z), 1 - 2.0*(v.y*v.y - v.z*v.z)); 224 else if (pole == .5) // North Pole 225 return 2.0 * atan2(v.x, w); 226 else // South Pole 227 return -2.0 * atan2(v.x, w); 228 } 229 230 /** 231 * @returns the Attitude 232 */ 233 float Quaternion::getAttitude() const 234 { 235 return asin(2.0 * (v.x*v.y + v.z*w)); 236 } 237 238 /** 239 * @returns the Bank 240 */ 241 float Quaternion::getBank() const 242 { 243 if (fabsf(this->v.x*this->v.y + this->v.z*this->w) != 0.5) 244 return atan2(2.0*(v.x*w-v.y*v.z) , 1 - 2.0*(v.x*v.x - v.z*v.z)); 245 else 246 return 0.0f; 247 } 248 249 250 /** 251 * @brief convert a rotational 4x4 glMatrix into a Quaternion 219 252 * @param m: a 4x4 matrix in glMatrix order 220 253 */ … … 229 262 tr = m[0][0] + m[1][1] + m[2][2]; 230 263 231 264 // check the diagonal 232 265 if (tr > 0.0) 233 266 { … … 238 271 v.y = (m[2][0] - m[0][2]) * s; 239 272 v.z = (m[0][1] - m[1][0]) * s; 240 241 242 243 244 245 273 } 274 else 275 { 276 // diagonal is negative 277 i = 0; 278 if (m[1][1] > m[0][0]) i = 1; 246 279 if (m[2][2] > m[i][i]) i = 2; 247 280 j = nxt[i]; … … 254 287 if (s != 0.0) s = 0.5 / s; 255 288 256 289 q[3] = (m[j][k] - m[k][j]) * s; 257 290 q[j] = (m[i][j] + m[j][i]) * s; 258 291 q[k] = (m[i][k] + m[k][i]) * s; 259 292 260 261 262 263 264 } 265 } 266 267 /** 268 * outputs some nice formated debug information about this quaternion293 v.x = q[0]; 294 v.y = q[1]; 295 v.z = q[2]; 296 w = q[3]; 297 } 298 } 299 300 /** 301 * @brief outputs some nice formated debug information about this quaternion 269 302 */ 270 303 void Quaternion::debug() const … … 273 306 } 274 307 308 /** 309 * @brief another better Quaternion Debug Function. 310 */ 275 311 void Quaternion::debug2() const 276 312 { -
trunk/src/lib/math/quaternion.h
r7191 r7348 96 96 inline void normalize() { float n = this->norm(); this->v /= n; this->w/=n; }; 97 97 98 float getHeading() const; 99 float getAttitude() const; 100 float getBank() const; 98 101 /** @returns the rotational axis of this Quaternion */ 99 102 inline Vector getSpacialAxis() const { return this->v / sin(acos(w));/*sqrt(v.x*v.x + v.y*v.y + v.z+v.z);*/ };
Note: See TracChangeset
for help on using the changeset viewer.