Changeset 4578 in orxonox.OLD for orxonox/trunk/src/lib/math
- Timestamp:
- Jun 10, 2005, 2:52:46 AM (20 years ago)
- Location:
- orxonox/trunk/src/lib/math
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/lib/math/vector.cc
r4477 r4578 1 /* 1 /* 2 2 orxonox - the future of 3D-vertical-scrollers 3 3 … … 10 10 11 11 ### File Specific: 12 main-programmer: Christian Meyer 12 main-programmer: Christian Meyer 13 13 co-programmer: Patrick Boenzli : Vector::scale() 14 14 Vector::abs() 15 15 16 16 Quaternion code borrowed from an Gamasutra article by Nick Bobick and Ken Shoemake 17 17 */ … … 48 48 \brief Vector is looking in the positive direction on all axes after this 49 49 */ 50 Vector Vector::abs() 50 Vector Vector::abs() 51 51 { 52 52 Vector v(fabs(x), fabs(y), fabs(z)); … … 74 74 \param dir: the direction you want to look 75 75 \param up: specify what direction up should be 76 76 77 77 Mathematically this determines the rotation a (0,0,1)-Vector has to undergo to point 78 78 the same way as dir. If you want to use this with cameras, you'll have to reverse the 79 79 dir Vector (Vector(0,0,0) - your viewing direction) or you'll point the wrong way. You 80 can use this for meshes as well (then you do not have to reverse the vector), but keep 81 in mind that if you do that, the model's front has to point in +z direction, and left 80 can use this for meshes as well (then you do not have to reverse the vector), but keep 81 in mind that if you do that, the model's front has to point in +z direction, and left 82 82 and right should be -x or +x respectively or the mesh wont rotate correctly. 83 83 */ … … 85 85 { 86 86 Vector z = dir; 87 z.normalize(); 87 z.normalize(); 88 88 Vector x = up.cross(z); 89 x.normalize(); 89 x.normalize(); 90 90 Vector y = z.cross(x); 91 91 92 92 float m[4][4]; 93 93 m[0][0] = x.x; … … 107 107 m[3][2] = 0; 108 108 m[3][3] = 1; 109 109 110 110 *this = Quaternion (m); 111 111 } … … 120 120 { 121 121 float cr, cp, cy, sr, sp, sy, cpcy, spsy; 122 122 123 123 // calculate trig identities 124 124 cr = cos(roll/2); 125 125 cp = cos(pitch/2); 126 126 cy = cos(yaw/2); 127 127 128 128 sr = sin(roll/2); 129 129 sp = sin(pitch/2); 130 130 sy = sin(yaw/2); 131 131 132 132 cpcy = cp * cy; 133 133 spsy = sp * sy; 134 134 135 135 w = cr * cpcy + sr * spsy; 136 136 v.x = sr * cpcy - cr * spsy; … … 147 147 { 148 148 float A, B, C, D, E, F, G, H; 149 149 150 150 A = (w + v.x)*(q.w + q.v.x); 151 151 B = (v.z - v.y)*(q.v.y - q.v.z); 152 C = (w - v.x)*(q.v.y + q.v.z); 152 C = (w - v.x)*(q.v.y + q.v.z); 153 153 D = (v.y + v.z)*(q.w - q.v.x); 154 154 E = (v.x + v.z)*(q.v.x + q.v.y); … … 156 156 G = (w + v.y)*(q.w - q.v.z); 157 157 H = (w - v.y)*(q.w + q.v.z); 158 158 159 159 Quaternion r; 160 160 r.v.x = A - (E + F + G + H)/2; … … 234 234 \brief calculate the inverse value of the Quaternion 235 235 \return the inverse Quaternion 236 237 236 237 Note that this is equal to conjugate() if the Quaternion's norm is 1 238 238 */ 239 239 Quaternion Quaternion::inverse() const … … 253 253 void Quaternion::matrix (float m[4][4]) const 254 254 { 255 float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2; 256 255 float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2; 256 257 257 // calculate coefficients 258 258 x2 = v.x + v.x; 259 y2 = v.y + v.y; 259 y2 = v.y + v.y; 260 260 z2 = v.z + v.z; 261 261 xx = v.x * x2; xy = v.x * y2; xz = v.x * z2; 262 262 yy = v.y * y2; yz = v.y * z2; zz = v.z * z2; 263 263 wx = w * x2; wy = w * y2; wz = w * z2; 264 264 265 265 m[0][0] = 1.0 - (yy + zz); m[1][0] = xy - wz; 266 266 m[2][0] = xz + wy; m[3][0] = 0.0; 267 267 268 268 m[0][1] = xy + wz; m[1][1] = 1.0 - (xx + zz); 269 269 m[2][1] = yz - wx; m[3][1] = 0.0; 270 270 271 271 m[0][2] = xz - wy; m[1][2] = yz + wx; 272 272 m[2][2] = 1.0 - (xx + yy); m[3][2] = 0.0; 273 273 274 274 m[0][3] = 0; m[1][3] = 0; 275 275 m[2][3] = 0; m[3][3] = 1; … … 292 292 cosom = from.v.x * to.v.x + from.v.y * to.v.y + from.v.z * to.v.z + from.w * to.w; 293 293 294 if( cosom < 0.0 ) 295 { 296 cosom = -cosom; 294 if( cosom < 0.0 ) 295 { 296 cosom = -cosom; 297 297 tol[0] = -to.v.x; 298 298 tol[1] = -to.v.y; … … 307 307 tol[3] = to.w; 308 308 } 309 309 310 310 //if( (1.0 - cosom) > DELTA ) 311 311 //{ … … 332 332 */ 333 333 return Quaternion(Vector(scale0 * from.v.x + scale1 * tol[0], 334 335 336 334 scale0 * from.v.y + scale1 * tol[1], 335 scale0 * from.v.z + scale1 * tol[2]), 336 scale0 * from.w + scale1 * tol[3]); 337 337 } 338 338 … … 344 344 Quaternion::Quaternion (float m[4][4]) 345 345 { 346 346 347 347 float tr, s, q[4]; 348 348 int i, j, k; … … 352 352 tr = m[0][0] + m[1][1] + m[2][2]; 353 353 354 355 if (tr > 0.0) 354 // check the diagonal 355 if (tr > 0.0) 356 356 { 357 357 s = sqrt (tr + 1.0); … … 361 361 v.y = (m[2][0] - m[0][2]) * s; 362 362 v.z = (m[0][1] - m[1][0]) * s; 363 } 364 else 365 366 367 368 363 } 364 else 365 { 366 // diagonal is negative 367 i = 0; 368 if (m[1][1] > m[0][0]) i = 1; 369 369 if (m[2][2] > m[i][i]) i = 2; 370 370 j = nxt[i]; … … 372 372 373 373 s = sqrt ((m[i][i] - (m[j][j] + m[k][k])) + 1.0); 374 374 375 375 q[i] = s * 0.5; 376 376 377 377 if (s != 0.0) s = 0.5 / s; 378 379 378 379 q[3] = (m[j][k] - m[k][j]) * s; 380 380 q[j] = (m[i][j] + m[j][i]) * s; 381 381 q[k] = (m[i][k] + m[k][i]) * s; 382 382 383 384 385 386 383 v.x = q[0]; 384 v.y = q[1]; 385 v.z = q[2]; 386 w = q[3]; 387 387 } 388 388 } … … 486 486 \brief fills the specified buffer with a 4x4 glmatrix 487 487 \param buffer: Pointer to an array of 16 floats 488 488 489 489 Use this to get the rotation in a gl-compatible format 490 490 */ 491 491 void Rotation::glmatrix (float* buffer) 492 492 { 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 493 buffer[0] = m[0]; 494 buffer[1] = m[3]; 495 buffer[2] = m[6]; 496 buffer[3] = m[0]; 497 buffer[4] = m[1]; 498 buffer[5] = m[4]; 499 buffer[6] = m[7]; 500 buffer[7] = m[0]; 501 buffer[8] = m[2]; 502 buffer[9] = m[5]; 503 buffer[10] = m[8]; 504 buffer[11] = m[0]; 505 buffer[12] = m[0]; 506 buffer[13] = m[0]; 507 buffer[14] = m[0]; 508 buffer[15] = m[1]; 509 509 } 510 510 … … 513 513 \param r: another Rotation 514 514 \return the matrix product of the Rotations 515 515 516 516 Use this to rotate one rotation by another 517 517 */ 518 518 Rotation Rotation::operator* (const Rotation& r) 519 519 { 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 520 Rotation p; 521 522 p.m[0] = m[0]*r.m[0] + m[1]*r.m[3] + m[2]*r.m[6]; 523 p.m[1] = m[0]*r.m[1] + m[1]*r.m[4] + m[2]*r.m[7]; 524 p.m[2] = m[0]*r.m[2] + m[1]*r.m[5] + m[2]*r.m[8]; 525 526 p.m[3] = m[3]*r.m[0] + m[4]*r.m[3] + m[5]*r.m[6]; 527 p.m[4] = m[3]*r.m[1] + m[4]*r.m[4] + m[5]*r.m[7]; 528 p.m[5] = m[3]*r.m[2] + m[4]*r.m[5] + m[5]*r.m[8]; 529 530 p.m[6] = m[6]*r.m[0] + m[7]*r.m[3] + m[8]*r.m[6]; 531 p.m[7] = m[6]*r.m[1] + m[7]*r.m[4] + m[8]*r.m[7]; 532 p.m[8] = m[6]*r.m[2] + m[7]*r.m[5] + m[8]*r.m[8]; 533 534 return p; 535 535 } 536 536 … … 545 545 { 546 546 Vector t; 547 547 548 548 t.x = v.x * r.m[0] + v.y * r.m[1] + v.z * r.m[2]; 549 549 t.y = v.x * r.m[3] + v.y * r.m[4] + v.z * r.m[5]; … … 581 581 582 582 /** 583 \brief calculate the distance between a line and a point 584 \param v: the point 585 \return the distance between the Line and the point 586 */ 587 float Line::distancePoint (const sVec3D& v) const 588 { 589 Vector s(v[0], v[1], v[2]); 590 Vector d = s - r; 591 Vector u = a * d.dot( a); 592 return (d - u).len(); 593 } 594 595 /** 583 596 \brief calculate the two points of minimal distance of two lines 584 597 \param l: the other line … … 597 610 /** 598 611 \brief calculate the length of a line 599 \return the lenght of the line 612 \return the lenght of the line 600 613 */ 601 614 float Line::len() const -
orxonox/trunk/src/lib/math/vector.h
r4562 r4578 1 /*! 1 /*! 2 2 \file vector.h 3 3 \brief A basic 3D math framework 4 4 5 5 Contains classes to handle vectors, lines, rotations and planes 6 */ 6 */ 7 7 8 8 #ifndef _VECTOR_H … … 17 17 //! 3D Vector 18 18 /** 19 19 Class to handle 3D Vectors 20 20 */ 21 21 class Vector { … … 63 63 /** \brief normalizes the vector */ 64 64 inline void normalize() { 65 float l = len(); 66 if( unlikely(l == 0.0)) 67 { 68 69 70 71 72 73 z = z / l; 65 float l = len(); 66 if( unlikely(l == 0.0)) 67 { 68 // Prevent divide by zero 69 return; 70 } 71 x = x / l; 72 y = y / l; 73 z = z / l; 74 74 } 75 75 Vector getNormalized() const; … … 123 123 inline const Quaternion& operator*= (const float& f) {*this = *this * f; return *this;} 124 124 Quaternion operator* (const Quaternion& q) const; 125 /** \param q: the Quaternion to multiply by \returns the quaternion multiplied by q (this *= q) */ 125 /** \param q: the Quaternion to multiply by \returns the quaternion multiplied by q (this *= q) */ 126 126 inline const Quaternion operator*= (const Quaternion& q) {*this = *this * q; return *this; }; 127 127 /** \param q the Quaternion to add to this \returns the quaternion added with q (this + q) */ … … 141 141 float norm () const; 142 142 void matrix (float m[4][4]) const; 143 143 144 144 void debug(); 145 145 … … 161 161 class Rotation { 162 162 public: 163 163 164 164 float m[9]; //!< 3x3 Rotation Matrix 165 165 166 166 Rotation ( const Vector& v); 167 167 Rotation ( const Vector& axis, float angle); … … 169 169 Rotation (); 170 170 ~Rotation () {} 171 171 172 172 Rotation operator* (const Rotation& r); 173 173 174 174 void glmatrix (float* buffer); 175 175 }; … … 187 187 { 188 188 public: 189 189 190 190 Vector r; //!< Offset 191 191 Vector a; //!< Direction 192 192 193 193 Line ( Vector r, Vector a) : r(r), a(a) {} //!< assignment constructor 194 194 Line () : r(Vector(0,0,0)), a(Vector (1,1,1)) {} 195 195 ~Line () {} 196 196 197 197 float distance (const Line& l) const; 198 198 float distancePoint (const Vector& v) const; 199 float distancePoint (const sVec3D& v) const; 199 200 Vector* footpoints (const Line& l) const; 200 201 float len () const; 201 202 202 203 void rotate(const Rotation& rot); 203 204 }; … … 206 207 /** 207 208 Class to handle planes in 3-dimensional space 208 209 209 210 Critical for polygon-based collision detection 210 211 */ … … 212 213 { 213 214 public: 214 215 215 216 Vector n; //!< Normal vector 216 217 float k; //!< Offset constant 217 218 218 219 Plane (Vector a, Vector b, Vector c); 219 220 Plane (Vector norm, Vector p); … … 221 222 Plane () : n(Vector(1,1,1)), k(0) {} 222 223 ~Plane () {} 223 224 224 225 Vector intersectLine (const Line& l) const; 225 226 float distancePoint (const Vector& p) const;
Note: See TracChangeset
for help on using the changeset viewer.