Changeset 2551 in orxonox.OLD for orxonox/trunk/src/vector.cc
- Timestamp:
- Oct 11, 2004, 12:53:43 AM (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/vector.cc
r2190 r2551 12 12 13 13 ### File Specific: 14 main-programmer: Christian Meyer 15 co-programmer: ... 14 main-programmer: Christian Meyer 15 co-programmer: Patrick Boenzli : Vector::scale() 16 Vector::abs() 16 17 17 18 Quaternion code borrowed from an Gamasutra article by Nick Bobick and Ken Shoemake … … 147 148 z = z / l; 148 149 } 150 151 152 /** 153 \bref returns the voctor normalized to length 1.0 154 */ 155 156 Vector* Vector::getNormalized() 157 { 158 float l = len(); 159 if(l != 1.0) 160 { 161 return this; 162 } 163 else if(l == 0.0) 164 { 165 return 0; 166 } 167 168 Vector *normalizedVector = new Vector(x / l, y /l, z / l); 169 return normalizedVector; 170 } 171 172 173 void Vector::scale(const Vector& v) 174 { 175 x *= v.x; 176 y *= v.y; 177 z *= v.z; 178 } 179 149 180 150 181 /** … … 156 187 return sqrt (x*x+y*y+z*z); 157 188 } 189 190 191 192 Vector Vector::abs() 193 { 194 Vector v(fabs(x), fabs(y), fabs(z)); 195 return v; 196 } 197 158 198 159 199 /** … … 168 208 } 169 209 210 170 211 /** 171 212 \brief calculate the angle between two vectors in degrees … … 202 243 203 244 /** 204 \brief calculates a look_at rotation 205 \param dir: the direction you want to look 206 \param up: specify what direction up should be 207 208 Mathematically this determines the rotation a (0,0,1)-Vector has to undergo to point the same way as dir. 209 If you want to use this with cameras, you'll have to reverse the dir Vector (Vector(0,0,0) - your viewing direction) 210 or you'll point the wrong way. You can use this for meshes as well (then you do not have to reverse the vector), but keep in mind that if you do that, the model's front 211 has to point in +z direction, and left and right should be -x or +x respectively or the mesh wont rotate correctly. 245 \brief calculates a look_at rotation 246 \param dir: the direction you want to look 247 \param up: specify what direction up should be 248 249 Mathematically this determines the rotation a (0,0,1)-Vector has to undergo to point 250 the same way as dir. If you want to use this with cameras, you'll have to reverse the 251 dir Vector (Vector(0,0,0) - your viewing direction) or you'll point the wrong way. You 252 can use this for meshes as well (then you do not have to reverse the vector), but keep 253 in mind that if you do that, the model's front has to point in +z direction, and left 254 and right should be -x or +x respectively or the mesh wont rotate correctly. 212 255 */ 213 256 Quaternion::Quaternion (const Vector& dir, const Vector& up) 214 257 { 215 258 Vector z = dir; 216 259 z.normalize(); 217 260 Vector x = up.cross(z); … … 246 289 \param yaw: the yaw in radians 247 290 248 I DO HONESTLY NOT EXACTLY KNOW WHICH ANGLE REPRESENTS WHICH ROTATION. And I do not know in what order they are applied,249 I just copy-pasted the code.291 I DO HONESTLY NOT EXACTLY KNOW WHICH ANGLE REPRESENTS WHICH ROTATION. And I do not know 292 in what order they are applied, I just copy-pasted the code. 250 293 */ 251 294 Quaternion::Quaternion (float roll, float pitch, float yaw) … … 407 450 void Quaternion::matrix (float m[4][4]) const 408 451 { 409 410 411 412 452 float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2; 453 454 // calculate coefficients 455 x2 = v.x + v.x; 413 456 y2 = v.y + v.y; 414 z2 = v.z + v.z; 415 xx = v.x * x2; xy = v.x * y2; xz = v.x * z2; 416 yy = v.y * y2; yz = v.y * z2; zz = v.z * z2; 417 wx = w * x2; wy = w * y2; wz = w * z2; 418 419 m[0][0] = 1.0 - (yy + zz); m[1][0] = xy - wz; 420 m[2][0] = xz + wy; m[3][0] = 0.0; 421 422 m[0][1] = xy + wz; m[1][1] = 1.0 - (xx + zz); 423 m[2][1] = yz - wx; m[3][1] = 0.0; 424 425 m[0][2] = xz - wy; m[1][2] = yz + wx; 426 m[2][2] = 1.0 - (xx + yy); m[3][2] = 0.0; 427 428 m[0][3] = 0; m[1][3] = 0; 429 m[2][3] = 0; m[3][3] = 1; 430 } 431 432 /** 433 \brief convert a rotational 4x4 glMatrix into a Quaternion 434 \param m: a 4x4 matrix in glMatrix order 457 z2 = v.z + v.z; 458 xx = v.x * x2; xy = v.x * y2; xz = v.x * z2; 459 yy = v.y * y2; yz = v.y * z2; zz = v.z * z2; 460 wx = w * x2; wy = w * y2; wz = w * z2; 461 462 m[0][0] = 1.0 - (yy + zz); m[1][0] = xy - wz; 463 m[2][0] = xz + wy; m[3][0] = 0.0; 464 465 m[0][1] = xy + wz; m[1][1] = 1.0 - (xx + zz); 466 m[2][1] = yz - wx; m[3][1] = 0.0; 467 468 m[0][2] = xz - wy; m[1][2] = yz + wx; 469 m[2][2] = 1.0 - (xx + yy); m[3][2] = 0.0; 470 471 m[0][3] = 0; m[1][3] = 0; 472 m[2][3] = 0; m[3][3] = 1; 473 } 474 475 476 void Quaternion::quatSlerp(const Quaternion* from, const Quaternion* to, float t, Quaternion* res) 477 { 478 float tol[4]; 479 double omega, cosom, sinom, scale0, scale1; 480 DELTA = 0.2; 481 482 cosom = from->v.x * to->v.x + from->v.y * to->v.y + from->v.z * to->v.z + from->w * to->w; 483 484 if( cosom < 0.0 ) 485 { 486 cosom = -cosom; 487 tol[0] = -to->v.x; 488 tol[1] = -to->v.y; 489 tol[2] = -to->v.z; 490 tol[3] = -to->w; 491 } 492 else 493 { 494 tol[0] = to->v.x; 495 tol[1] = to->v.y; 496 tol[2] = to->v.z; 497 tol[3] = to->w; 498 } 499 500 //if( (1.0 - cosom) > DELTA ) 501 //{ 502 omega = acos(cosom); 503 sinom = sin(omega); 504 scale0 = sin((1.0 - t) * omega) / sinom; 505 scale1 = sin(t * omega) / sinom; 506 //} 507 /* 508 else 509 { 510 scale0 = 1.0 - t; 511 scale1 = t; 512 } 513 */ 514 res->v.x = scale0 * from->v.x + scale1 * tol[0]; 515 res->v.y = scale0 * from->v.y + scale1 * tol[1]; 516 res->v.z = scale0 * from->v.z + scale1 * tol[2]; 517 res->w = scale0 * from->w + scale1 * tol[3]; 518 } 519 520 521 /** 522 \brief convert a rotational 4x4 glMatrix into a Quaternion 523 \param m: a 4x4 matrix in glMatrix order 435 524 */ 436 525 Quaternion::Quaternion (float m[4][4])
Note: See TracChangeset
for help on using the changeset viewer.