Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7348 in orxonox.OLD


Ignore:
Timestamp:
Apr 19, 2006, 2:55:29 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: new Quaternion Functionality. Also added <abs-dir> to TurbineHover

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/math/quaternion.cc

    r7191 r7348  
    3535/////////////////
    3636/**
    37  * calculates a lookAt rotation
     37 * @brief calculates a lookAt rotation
    3838 * @param dir: the direction you want to look
    3939 * @param up: specify what direction up should be
    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.
     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.
    4747 *
    4848 * @TODO !!! OPTIMIZE THIS !!!
     
    7676
    7777/**
    78  * calculates a rotation from euler angles
     78 * @brief calculates a rotation from euler angles
    7979 * @param roll: the roll in radians
    8080 * @param pitch: the pitch in radians
     
    104104
    105105/**
    106  * convert the Quaternion to a 4x4 rotational glMatrix
     106 * @brief convert the Quaternion to a 4x4 rotational glMatrix
    107107 * @param m: a buffer to store the Matrix in
    108108 */
     
    134134
    135135/**
    136  * Slerps this QUaternion performs a smooth move.
     136 * @brief Slerps this QUaternion performs a smooth move.
    137137 * @param toQuat to this Quaternion
    138138 * @param t \% inth the the direction[0..1]
     
    167167  scale1 = sin(t * omega) / sinom;
    168168  this->v = Vector(scale0 * this->v.x + scale1 * tol[0],
    169                     scale0 * this->v.y + scale1 * tol[1],
    170                     scale0 * this->v.z + scale1 * tol[2]);
     169                   scale0 * this->v.y + scale1 * tol[1],
     170                   scale0 * this->v.z + scale1 * tol[2]);
    171171  this->w = scale0 * this->w + scale1 * tol[3];
    172172}
     
    174174
    175175/**
    176  * performs a smooth move.
     176 * @brief performs a smooth move.
    177177 * @param from  where
    178178 * @param to where
     
    189189
    190190  if( cosom < 0.0 )
    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     }
     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  }
    198198  else
    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     }
     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  }
    205205
    206206  omega = acos(cosom);
     
    209209  scale1 = sin(t * omega) / sinom;
    210210  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]),
    213213                    scale0 * from.w + scale1 * tol[3]);
    214214}
    215215
    216 
    217 /**
    218  *  convert a rotational 4x4 glMatrix into a Quaternion
     216/**
     217 * @returns the heading
     218 */
     219float 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 */
     233float Quaternion::getAttitude() const
     234{
     235  return asin(2.0 * (v.x*v.y + v.z*w));
     236}
     237
     238/**
     239 * @returns the Bank
     240 */
     241float 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
    219252 * @param m: a 4x4 matrix in glMatrix order
    220253 */
     
    229262  tr = m[0][0] + m[1][1] + m[2][2];
    230263
    231         // check the diagonal
     264  // check the diagonal
    232265  if (tr > 0.0)
    233266  {
     
    238271    v.y = (m[2][0] - m[0][2]) * s;
    239272    v.z = (m[0][1] - m[1][0]) * s;
    240         }
    241         else
    242         {
    243                 // diagonal is negative
    244         i = 0;
    245         if (m[1][1] > m[0][0]) i = 1;
     273  }
     274  else
     275  {
     276    // diagonal is negative
     277    i = 0;
     278    if (m[1][1] > m[0][0]) i = 1;
    246279    if (m[2][2] > m[i][i]) i = 2;
    247280    j = nxt[i];
     
    254287    if (s != 0.0) s = 0.5 / s;
    255288
    256           q[3] = (m[j][k] - m[k][j]) * s;
     289    q[3] = (m[j][k] - m[k][j]) * s;
    257290    q[j] = (m[i][j] + m[j][i]) * s;
    258291    q[k] = (m[i][k] + m[k][i]) * s;
    259292
    260         v.x = q[0];
    261         v.y = q[1];
    262         v.z = q[2];
    263         w = q[3];
    264   }
    265 }
    266 
    267 /**
    268  * outputs some nice formated debug information about this quaternion
     293    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
    269302*/
    270303void Quaternion::debug() const
     
    273306}
    274307
     308/**
     309 * @brief another better Quaternion Debug Function.
     310 */
    275311void Quaternion::debug2() const
    276312{
  • trunk/src/lib/math/quaternion.h

    r7191 r7348  
    9696  inline void normalize() { float n = this->norm(); this->v /= n; this->w/=n; };
    9797
     98  float getHeading() const;
     99  float getAttitude() const;
     100  float getBank() const;
    98101  /** @returns the rotational axis of this Quaternion */
    99102  inline Vector getSpacialAxis() const { return this->v / sin(acos(w));/*sqrt(v.x*v.x + v.y*v.y + v.z+v.z);*/ };
  • trunk/src/world_entities/space_ships/turbine_hover.cc

    r7346 r7348  
    220220void TurbineHover::loadParams(const TiXmlElement* root)
    221221{
    222   WorldEntity::loadParams(root);
    223 }
    224 
     222  Playable::loadParams(root);
     223}
     224
     225void TurbineHover::setPlayDirection(const Quaternion& rot, float speed)
     226{
     227  this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
     228}
    225229
    226230void TurbineHover::enter()
  • trunk/src/world_entities/space_ships/turbine_hover.h

    r7346 r7348  
    2222
    2323    virtual void loadParams(const TiXmlElement* root);
    24     virtual void setPlayDirection(const Quaternion& rot, float speed = 0.0f) {/* FIXME */};
     24    virtual void setPlayDirection(const Quaternion& rot, float speed = 0.0f);
    2525    virtual void enter();
    2626    virtual void leave();
Note: See TracChangeset for help on using the changeset viewer.