Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2112 in orxonox.OLD


Ignore:
Timestamp:
Jul 12, 2004, 10:28:25 PM (20 years ago)
Author:
chris
Message:

orxonox/branches/chris: Implemented quaternion class… but something is still f up… the camera keeps pointing into the wrong direction… matrix rotation calculation seems not to be my strenght…

Location:
orxonox/branches/chris/src
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/chris/src/camera.cc

    r2100 r2112  
    3030{
    3131        bound = NULL;
    32         desired_place.r = Vector (0,0,0);
    33         desired_place.w = Rotation (0,0,0);
    34         actual_place.r = Vector (0,0,0);
    35         actual_place.w = Rotation (0,0,0);
    3632}
    3733
     
    5349{
    5450        update_desired_place ();
     51        jump (NULL);
    5552}
    5653
     
    7370                {
    7471                        desired_place.r = Vector (0,0,0);
    75                         desired_place.w = Rotation (0,0,0);
     72                        desired_place.w = Quaternion ();
    7673                }
    7774}
     
    8683{
    8784        glMatrixMode (GL_PROJECTION);
     85  glLoadIdentity ();
    8886                // view
    8987                // TO DO: implement options for frustum generation
    90         glFrustum( -1.0,1.0,-1.0,1.0,1.5,20.0);
     88        glFrustum( -1.0,1.0,-1.0,1.0,1.5,250.0);
     89        glRotatef(h,1,0,0);
     90        glRotatef(h/2,0,1,0);
     91        glRotatef(h/4,0,0,1);
     92        h += 3;
     93//      glRotatef(90,0,0,1);
    9194        // rotation
    92   float matrix[16];
    93   actual_place.w.glmatrix (matrix);
    94   glLoadMatrixf (matrix);
     95  float matrix[4][4];
     96  actual_place.w.conjugate().matrix (matrix);
     97  glMultMatrixf ((float*)matrix);
    9598        // translation
    96   glTranslatef (actual_place.r.x, actual_place.r.y, actual_place.r.z);
     99  glTranslatef (-actual_place.r.x, -actual_place.r.y,- actual_place.r.z);
    97100 
    98101  glMatrixMode (GL_MODELVIEW);
    99102  glLoadIdentity ();
     103 
     104  printf("Camera@%f/%f/%f\n",actual_place.r.x,actual_place.r.y,actual_place.r.z);
     105  Vector unique(1,0,0);
     106  Vector test = actual_place.w.apply (unique);
     107  printf("Camera&%f/%f/%f\n", test.x, test.y, test.z);
    100108}
    101109
  • orxonox/branches/chris/src/camera.h

    r2100 r2112  
    2626        void update_desired_place ();
    2727       
     28        float h;
     29       
    2830 public:
    2931  Camera ();
  • orxonox/branches/chris/src/collision.cc

    r2100 r2112  
    180180  bool r = false;
    181181  int ia, ib;
    182   Vector mra = pa->r + rotate_vector( ta->m, pa->w);
    183   Vector mrb = pb->r + rotate_vector( tb->m, pb->w);
     182  Vector mra = pa->r + pa->w.apply(ta->m);
     183  Vector mrb = pb->r + pb->w.apply(tb->m);
    184184  CC_Tree* use_a, *use_b;
    185185 
     
    333333  bool r = false;
    334334  int i;
    335   Vector mr = p->r + rotate_vector( t->m, p->w);
     335  Vector mr = p->r + p->w.apply (t->m);
    336336  CC_Tree* use_t;
    337337  Vector* ips;
  • orxonox/branches/chris/src/coordinates.h

    r2014 r2112  
    1818  float dist;           //!< The distance that has already been traveled on the track
    1919  Vector pos;           //!< The position relative to the offset marked by the distance already covered
    20   Rotation rot;         //!< The direction the object is heading (relative to track direction)
     20  Quaternion rot;         //!< The direction the object is heading (relative to track direction)
    2121} Location;
    2222
     
    2828{
    2929  Vector r;              //!< Absolute x/y/z coordinates
    30   Rotation w;            //!< Absolute orientation
     30  Quaternion w;            //!< Absolute orientation
    3131} Placement;
    3232
  • orxonox/branches/chris/src/orxonox.cc

    r2105 r2112  
    139139  glClearColor(0.0, 0.0, 0.0, 0.0);
    140140  glEnable(GL_DEPTH_TEST);
     141  glEnable(GL_COLOR);
    141142  glShadeModel(GL_FLAT);
    142143 
  • orxonox/branches/chris/src/player.cc

    r2105 r2112  
    3333void Player::post_spawn ()
    3434{
    35         travel_speed = 1.0;
     35        travel_speed = 10.0;
    3636        velocity = Vector();
    3737        bUp = bDown = bLeft = bRight = bAscend = bDescend = false;
     
    7171{
    7272        glMatrixMode(GL_MODELVIEW);
    73         float matrix[16];
    74   get_placement()->w.glmatrix (matrix);
    75   glLoadMatrixf (matrix);
     73        float matrix[4][4];
     74  get_placement()->w.matrix (matrix);
     75  glLoadMatrixf ((float*)matrix);
    7676
    7777        glTranslatef(get_placement()->r.x,get_placement()->r.y,get_placement()->r.z);
     
    8585        glVertex3f(-1,+0.5,0);
    8686        glEnd();
     87       
     88        printf("Player@%f/%f/%f\n", get_placement()->r.x, get_placement()->r.y, get_placement()->r.z);
    8789}
    8890
     
    9092{
    9193        *locbuf = *get_location();
    92         locbuf->dist += 5.0;
     94        //locbuf->dist += 5.0;
    9395}
    9496
  • orxonox/branches/chris/src/track.cc

    r2101 r2112  
    4242void Track::map_camera (Location* lookat, Placement* camplc)
    4343{
     44printf("Track|Mapping camera: %d (%f)\n", lookat->part, lookat->dist);
    4445        Line trace(*offset, *end - *offset);
    4546        float l = trace.len ();
    4647       
     48//      camplc->r = *offset + Vector(0,0,0.5);
     49//      camplc->w = Quaternion (trace.a, Vector(0,0,1));
     50       
    4751        camplc->r = trace.r + (trace.a * ((lookat->dist-10.0) / l)) + Vector(0,0,5.0);
    48         camplc->w = Rotation( (trace.r + (trace.a * ((lookat->dist) / l))) - camplc->r);
     52        camplc->w = Quaternion((trace.r + (trace.a * ((lookat->dist) / l)) - camplc->r), Vector(0,0,1));
     53       
    4954}
    5055
    5156bool Track::map_coords (Location* loc, Placement* plc)
    5257{
     58printf("Track|Mapping coords: %d (%f)\n", loc->part, loc->dist);
    5359        Line trace(*offset, *end - *offset);
    5460        float l = trace.len ();
     
    6167        }
    6268       
    63         Rotation dir(trace.a);
     69        Quaternion dir(trace.a, Vector(0,0,1));
    6470       
    65         plc->r = trace.r + (trace.a * ((loc->dist) / l)) + rotate_vector( loc->pos, dir);
     71        plc->r = trace.r + (trace.a * ((loc->dist) / l)) + dir.apply(loc->pos);
    6672        plc->w = dir * loc->rot;
    6773       
  • orxonox/branches/chris/src/vector.cc

    r2101 r2112  
    1414   main-programmer: Christian Meyer
    1515   co-programmer: ...
     16   
     17   Quaternion code borrowed from an Gamasutra article by Nick Bobick and Ken Shoemake
    1618*/
    1719
     
    177179  f = acos( v1 * v2 / (v1.len() * v2.len()));
    178180  return f * 180 / PI;
     181}
     182
     183Quaternion::Quaternion ()
     184{
     185        w = 1;
     186        v = Vector(0,0,0);
     187}
     188
     189Quaternion::Quaternion (float angle, const Vector& axis)
     190{
     191        w = cos(angle/2);
     192        v = axis * sin(angle/2);
     193}
     194
     195Quaternion::Quaternion (const Vector& dir, const Vector& up)
     196{
     197        Vector z = dir;
     198  z.normalize();
     199  Vector x = up.cross(z);
     200  x.normalize();
     201  Vector y = z.cross(x);
     202 
     203  float m[4][4];
     204  m[0][0] = x.x;
     205  m[0][1] = x.y;
     206  m[0][2] = x.z;
     207  m[0][3] = 0;
     208  m[1][0] = y.x;
     209  m[1][1] = y.y;
     210  m[1][2] = y.z;
     211  m[1][3] = 0;
     212  m[2][0] = z.x;
     213  m[2][1] = z.y;
     214  m[2][2] = z.z;
     215  m[2][3] = 0;
     216  m[3][0] = 0;
     217  m[3][1] = 0;
     218  m[3][2] = 0;
     219  m[3][3] = 1;
     220 
     221  *this = Quaternion (m);
     222}
     223
     224Quaternion::Quaternion (float roll, float pitch, float yaw)
     225{
     226        float cr, cp, cy, sr, sp, sy, cpcy, spsy;
     227       
     228        // calculate trig identities
     229        cr = cos(roll/2);
     230        cp = cos(pitch/2);
     231        cy = cos(yaw/2);
     232       
     233        sr = sin(roll/2);
     234        sp = sin(pitch/2);
     235        sy = sin(yaw/2);
     236       
     237        cpcy = cp * cy;
     238        spsy = sp * sy;
     239       
     240        w = cr * cpcy + sr * spsy;
     241        v.x = sr * cpcy - cr * spsy;
     242        v.y = cr * sp * cy + sr * cp * sy;
     243        v.z = cr * cp * sy - sr * sp * cy;
     244}
     245
     246Quaternion Quaternion::operator*(const Quaternion& q) const
     247{
     248        float A, B, C, D, E, F, G, H;
     249        Quaternion r;
     250       
     251        A = (w   + v.x)*(q.w   + q.v.x);
     252        B = (v.z - v.y)*(q.v.y - q.v.z);
     253        C = (w   - v.x)*(q.v.y + q.v.z);
     254        D = (v.y + v.z)*(q.w   - q.v.x);
     255        E = (v.x + v.z)*(q.v.x + q.v.y);
     256        F = (v.x - v.z)*(q.v.x - q.v.y);
     257        G = (w   + v.y)*(q.w   - q.v.z);
     258        H = (w   - v.y)*(q.w   + q.v.z);
     259       
     260        r.w = B +  (-E - F + G + H)/2;
     261        r.v.x = A - (E + F + G + H)/2;
     262        r.v.y = C + (E - F + G - H)/2;
     263        r.v.z = D + (E - F - G + H)/2;
     264       
     265        return r;
     266}
     267
     268Quaternion Quaternion::operator+(const Quaternion& q) const
     269{
     270        Quaternion r(*this);
     271        r.w = r.w + q.w;
     272        r.v = r.v + q.v;
     273        return r;
     274}
     275
     276Quaternion Quaternion::operator- (const Quaternion& q) const
     277{
     278        Quaternion r(*this);
     279        r.w = r.w - q.w;
     280        r.v = r.v - q.v;
     281        return r;
     282}
     283
     284Vector Quaternion::apply (Vector& v) const
     285{
     286        Quaternion q;
     287        q.v = v;
     288        q.w = 0;
     289        q = *this * q * conjugate();
     290        return q.v;
     291}
     292
     293Quaternion Quaternion::operator*(const float& f) const
     294{
     295        Quaternion r(*this);
     296        r.w = r.w*f;
     297        r.v = r.v*f;
     298        return r;
     299}
     300
     301Quaternion Quaternion::operator/(const float& f) const
     302{
     303        if( f == 0) return Quaternion();
     304        Quaternion r(*this);
     305        r.w = r.w/f;
     306        r.v = r.v/f;
     307        return r;
     308}
     309
     310Quaternion Quaternion::conjugate() const
     311{
     312        Quaternion r(*this);
     313        r.v = Vector() - r.v;
     314        return r;
     315}
     316
     317float Quaternion::norm() const
     318{
     319        return w*w + v.x*v.x + v.y*v.y + v.z*v.z;
     320}
     321
     322Quaternion Quaternion::inverse() const
     323{
     324        float n = norm();
     325        if (n != 0)
     326        {
     327                return conjugate() / norm();
     328        }
     329        else return Quaternion();
     330}
     331
     332void Quaternion::matrix (float m[4][4]) const
     333{
     334        float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
     335
     336        // calculate coefficients
     337        x2 = v.x + v.x;
     338  y2 = v.y + v.y;
     339        z2 = v.z + v.z;
     340        xx = v.x * x2; xy = v.x * y2; xz = v.x * z2;
     341        yy = v.y * y2; yz = v.y * z2; zz = v.z * z2;
     342        wx = w * x2; wy = w * y2; wz = w * z2;
     343       
     344        m[0][0] = 1.0 - (yy + zz); m[1][0] = xy - wz;
     345        m[2][0] = xz + wy; m[3][0] = 0.0;
     346       
     347        m[0][1] = xy + wz; m[1][1] = 1.0 - (xx + zz);
     348        m[2][1] = yz - wx; m[3][1] = 0.0;
     349       
     350        m[0][2] = xz - wy; m[1][2] = yz + wx;
     351        m[2][2] = 1.0 - (xx + yy); m[3][2] = 0.0;
     352       
     353        m[0][3] = 0; m[1][3] = 0;
     354        m[2][3] = 0; m[3][3] = 1;
     355}
     356
     357Quaternion::Quaternion (float m[4][4])
     358{
     359       
     360  float  tr, s, q[4];
     361  int    i, j, k;
     362
     363  int nxt[3] = {1, 2, 0};
     364
     365  tr = m[0][0] + m[1][1] + m[2][2];
     366
     367        // check the diagonal
     368  if (tr > 0.0)
     369  {
     370    s = sqrt (tr + 1.0);
     371    w = s / 2.0;
     372    s = 0.5 / s;
     373    v.x = (m[1][2] - m[2][1]) * s;
     374    v.y = (m[2][0] - m[0][2]) * s;
     375    v.z = (m[0][1] - m[1][0]) * s;
     376        }
     377        else
     378        {
     379                // diagonal is negative
     380        i = 0;
     381        if (m[1][1] > m[0][0]) i = 1;
     382    if (m[2][2] > m[i][i]) i = 2;
     383    j = nxt[i];
     384    k = nxt[j];
     385
     386    s = sqrt ((m[i][i] - (m[j][j] + m[k][k])) + 1.0);
     387     
     388    q[i] = s * 0.5;
     389           
     390    if (s != 0.0) s = 0.5 / s;
     391       
     392          q[3] = (m[j][k] - m[k][j]) * s;
     393    q[j] = (m[i][j] + m[j][i]) * s;
     394    q[k] = (m[i][k] + m[k][i]) * s;
     395
     396        v.x = q[0];
     397        v.y = q[1];
     398        v.z = q[2];
     399        w = q[3];
     400  }
    179401}
    180402
  • orxonox/branches/chris/src/vector.h

    r2100 r2112  
    3636float angle_rad (const Vector& v1, const Vector& v2);
    3737
    38 //! 3D rotation
     38//! Quaternion
     39/**
     40        Class to handle 3-dimensional rotation efficiently
     41*/
     42class Quaternion
     43{
     44 public:
     45        Vector v;
     46        float w;
     47
     48        Quaternion ();
     49        Quaternion (float m[4][4]);
     50        Quaternion (float angle, const Vector& axis);
     51        Quaternion (const Vector& dir, const Vector& up);
     52        Quaternion (float roll, float pitch, float yaw);
     53       
     54        Quaternion operator/ (const float& f) const;
     55        Quaternion operator* (const float& f) const;
     56        Quaternion operator* (const Quaternion& q) const;
     57        Quaternion operator+ (const Quaternion& q) const;
     58        Quaternion operator- (const Quaternion& q) const;
     59        Quaternion conjugate () const;
     60        Quaternion inverse () const;
     61        Vector apply (Vector& f) const;
     62        float norm () const;
     63        void matrix (float m[4][4]) const;
     64};
     65
     66//! 3D rotation (OBSOLETE)
    3967/**
    4068  Class to handle 3-dimensional rotations.
  • orxonox/branches/chris/src/world.cc

    r2104 r2112  
    9090          l = l->get_next();
    9191        }
     92       
     93        // draw debug coord system
     94        glLoadIdentity();
     95        glBegin(GL_LINES);
     96        glColor3f(1,0,0);
     97/*      for( float x = -128.0; x < 128.0; x += 5.0)
     98        {
     99                for( float y = -128.0; y < 128.0; y += 5.0)
     100                {
     101                        glVertex3f(x,y,-128.0);
     102                        glVertex3f(x,y,128.0);
     103                }
     104        }
     105        glColor3f(0,1,0);
     106        for( float y = -128.0; y < 128.0; y += 5.0)
     107        {
     108                for( float z = -128.0; z < 128.0; z += 5.0)
     109                {
     110                        glVertex3f(-128.0,y,z);
     111                        glVertex3f(128.0,y,z);
     112                }
     113        }
     114        glColor3f(0,0,1);
     115        for( float x = -128.0; x < 128.0; x += 5.0)
     116        {
     117                for( float z = -128.0; z < 128.0; z += 5.0)
     118                {
     119                        glVertex3f(x,-128.0,z);
     120                        glVertex3f(x,128.0,z);
     121                }
     122        }*/
     123        //draw track
     124        glColor3f(0,1,1);
     125        for( int i = 0; i < tracklen; i++)
     126        {
     127                glVertex3f(pathnodes[i].x,pathnodes[i].y,pathnodes[i].z);
     128                glVertex3f(pathnodes[(i+1)%tracklen].x,pathnodes[(i+1)%tracklen].y,pathnodes[(i+1)%tracklen].z);
     129        }
     130        glEnd();
    92131}
    93132
  • orxonox/branches/chris/src/world.h

    r2101 r2112  
    5050                zeroloc.part = 0;
    5151                zeroloc.pos = Vector();
    52                 zeroloc.rot = Rotation();
     52                zeroloc.rot = Quaternion();
    5353                loc = &zeroloc;
    5454        }
  • orxonox/branches/chris/src/world_entity.cc

    r2104 r2112  
    8383void WorldEntity::draw()
    8484{
    85   cout << "WorldEntity::draw()" << endl;
     85printf("WorldEntity|draw()\n");
    8686}
    8787
Note: See TracChangeset for help on using the changeset viewer.