home *** CD-ROM | disk | FTP | other *** search
/ 3D Games (Spidla) / 3dhry1.iso / carterrain / src / camera.cpp next >
Encoding:
C/C++ Source or Header  |  2003-03-17  |  1.3 KB  |  85 lines

  1. #include "camera.h"
  2.  
  3. void CCamera::init(dBodyID c)
  4. {
  5.     car=c;
  6.     mode=CAMERA_CHASE1;
  7.     update_tpos();
  8.     update_ttar();
  9.     position=tpos;
  10.     target=ttar;
  11. }
  12.  
  13. void CCamera::update_tpos(void)
  14. {
  15.     const dReal *p = dBodyGetPosition(car);
  16.     const dReal *r = dBodyGetRotation(car);
  17.     
  18.     if(mode==CAMERA_CHASE0)
  19.     {
  20.         tpos.x=p[0]-r[1]*7;
  21.         tpos.y=p[1]-r[5]*7;
  22.         tpos.z=p[2]+3;
  23.     }
  24.     if(mode==CAMERA_CHASE1)
  25.     {
  26.         tpos.x=p[0]-r[1]*15;
  27.         tpos.y=p[1]-r[5]*15;
  28.         tpos.z=p[2]+5;
  29.     }
  30.     if(mode==CAMERA_CHASE2)
  31.     {
  32.         tpos.x=p[0]-r[1]*25;
  33.         tpos.y=p[1]-r[5]*25;
  34.         tpos.z=p[2]+10;
  35.     }
  36. }
  37.  
  38. void CCamera::update_ttar(void)
  39. {
  40.     const dReal *p = dBodyGetPosition(car);
  41.     if(mode==CAMERA_CHASE0)
  42.     {
  43.         ttar.x=p[0];
  44.         ttar.y=p[1];
  45.         ttar.z=p[2]+0.8;
  46.     }
  47.     if(mode==CAMERA_CHASE1)
  48.     {
  49.         ttar.x=p[0];
  50.         ttar.y=p[1];
  51.         ttar.z=p[2];
  52.     }
  53.     if(mode==CAMERA_CHASE2)
  54.     {
  55.         ttar.x=p[0];
  56.         ttar.y=p[1];
  57.         ttar.z=p[2];
  58.     }
  59. }
  60.  
  61. void CCamera::update(double t)
  62. {
  63.     update_tpos();
  64.     update_ttar();
  65.     
  66.     if((tpos-position).length() > t*11)
  67.         position=position+((tpos-position).unit()*t*11);
  68.     else
  69.         position=tpos;
  70.  
  71.     if((ttar-target).length() > t*11)
  72.         target=target+((ttar-target).unit()*t*11);
  73.     else
  74.         target=ttar;
  75. }
  76.  
  77. void CCamera::set_mode(int m)
  78. {
  79.     mode=m;
  80.     update_tpos();
  81.     update_ttar();
  82.     position=tpos;
  83.     target=ttar;
  84. }
  85.