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

  1. #include <ode/ode.h>
  2. #include "car.h"
  3.  
  4. void CCar::init(dWorldID w, dSpaceID s, double x, double y, double z)
  5. {
  6.     if(alive)
  7.         destroy();
  8.     int i;
  9.     
  10.     world_citzen=w;
  11.     space_citzen=s;
  12.     
  13.     dMass m;
  14.     chassis = dBodyCreate(w);
  15.     dBodySetPosition(chassis, x, y, z);
  16.     dMassSetBox(&m, 1, 2, 3, 1.5);
  17.     dBodySetMass(chassis, &m);
  18.     chassisbox = dCreateBox(s, 2, 3, 1.5);
  19.     dGeomSetBody(chassisbox, chassis);
  20.     
  21.     for(i=0; i<4; i++)
  22.     {
  23.         wheel[i] = dBodyCreate(w);
  24.         dMassSetSphere(&m, 5, 0.8);
  25.         dBodySetMass(wheel[i], &m);
  26.         wheelsphere[i] = dCreateSphere(s, 0.8);
  27.         dGeomSetBody(wheelsphere[i], wheel[i]);
  28.     }
  29.     dBodySetPosition(wheel[0], x-1.9, y-1.5, z-1.3);
  30.     dBodySetPosition(wheel[1], x+1.9, y-1.5, z-1.3);
  31.     dBodySetPosition(wheel[2], x-1.9, y+1.5, z-1.3);
  32.     dBodySetPosition(wheel[3], x+1.9, y+1.5, z-1.3);
  33.     
  34.     for(i=0; i<4; i++)
  35.     {
  36.         wheeljoint[i] = dJointCreateHinge2(w, 0);
  37.         dJointAttach(wheeljoint[i], chassis, wheel[i]);
  38.         const dReal *a = dBodyGetPosition(wheel[i]);
  39.         dJointSetHinge2Anchor(wheeljoint[i], a[0], a[1], a[2]);
  40.         dJointSetHinge2Axis1(wheeljoint[i], 0, 0, 1);
  41.         dJointSetHinge2Axis2(wheeljoint[i], 1, 0, 0);
  42.         
  43.         dJointSetHinge2Param(wheeljoint[i], dParamSuspensionERP, 0.6);
  44.         dJointSetHinge2Param(wheeljoint[i], dParamSuspensionCFM, 0.08);
  45.     }
  46.     for(i=0; i<2; i++)
  47.     {
  48.         dJointSetHinge2Param(wheeljoint[i], dParamLoStop, 0);
  49.         dJointSetHinge2Param(wheeljoint[i], dParamHiStop, 0);
  50.     }
  51.     steer=0;
  52.     for(i=2; i<4; i++)
  53.     {
  54.         dJointSetHinge2Param(wheeljoint[i], dParamLoStop, steer);
  55.         dJointSetHinge2Param(wheeljoint[i], dParamHiStop, steer);
  56.         dJointSetHinge2Param(wheeljoint[i], dParamSuspensionERP, 0);
  57.         dJointSetHinge2Param(wheeljoint[i], dParamSuspensionCFM, 0);
  58.     }
  59.     
  60.     alive=1;
  61. }
  62.  
  63. void CCar::destroy(void)
  64. {
  65.     int i;
  66.     for(i=0; i<4; i++)
  67.         dJointDestroy(wheeljoint[i]);
  68.     
  69.     dGeomDestroy(chassisbox);
  70.     for(i=0; i<4; i++)
  71.         dGeomDestroy(wheelsphere[i]);
  72.         
  73.     dBodyDestroy(chassis);
  74.     for(i=0; i<4; i++)
  75.         dBodyDestroy(wheel[i]);
  76.  
  77.     alive=0;
  78. }
  79.  
  80. void CCar::fly(void)
  81. {
  82.     dBodyAddForce(chassis, 0, 0, 2000);
  83. }
  84.  
  85. void CCar::set_engine(int mode)
  86. {
  87.     int i;
  88.     if(mode==CARENGINE_COAST)
  89.     {
  90.         for(i=0; i<4; i++)
  91.             dJointSetHinge2Param(wheeljoint[i], dParamFMax2, 0);
  92.     }
  93.     if(mode==CARENGINE_BRAKE)
  94.     {
  95.         for(i=0; i<4; i++)
  96.         {
  97.             dJointSetHinge2Param(wheeljoint[i], dParamVel2, 0);
  98.             dJointSetHinge2Param(wheeljoint[i], dParamFMax2, 200);
  99.         }
  100.     }
  101.     if(mode==CARENGINE_FORWARD)
  102.     {
  103.         for(i=0; i<4; i++)
  104.         {
  105.                       dJointSetHinge2Param(wheeljoint[i], dParamVel2, 13);
  106.             dJointSetHinge2Param(wheeljoint[i], dParamFMax2, 90);
  107.         }
  108.     }
  109.     if(mode==CARENGINE_REVERSE)
  110.     {
  111.         for(i=0; i<4; i++)
  112.         {
  113.             dJointSetHinge2Param(wheeljoint[i], dParamVel2, -13);
  114.             dJointSetHinge2Param(wheeljoint[i], dParamFMax2, 90);
  115.         }
  116.     }
  117. }
  118.  
  119. void CCar::set_steer(double s)
  120. {
  121.     steer=s;
  122.     if(steer>0.5)
  123.         steer=0.5;
  124.     if(steer<-0.5)
  125.         steer=-0.5;
  126.     adjust_steer();
  127. }
  128.  
  129. void CCar::add_steer(double s)
  130. {
  131.     steer+=s;
  132.     if(steer>0.5)
  133.         steer=0.5;
  134.     if(steer<-0.5)
  135.         steer=-0.5;
  136.     adjust_steer();
  137. }
  138.  
  139. void CCar::adjust_steer(void)
  140. {
  141.         if(steer==0)
  142.         {
  143.             dJointSetHinge2Param(wheeljoint[2], dParamLoStop, 0);
  144.             dJointSetHinge2Param(wheeljoint[2], dParamHiStop, 0);
  145.             dJointSetHinge2Param(wheeljoint[3], dParamLoStop, 0);
  146.             dJointSetHinge2Param(wheeljoint[3], dParamHiStop, 0);
  147.         }
  148.         else if(steer>0)
  149.         {
  150.             dJointSetHinge2Param(wheeljoint[2], dParamLoStop, M_PI/2-atan((tan(M_PI/2-steer)*3+1.9)/3));
  151.             dJointSetHinge2Param(wheeljoint[2], dParamHiStop, M_PI/2-atan((tan(M_PI/2-steer)*3+1.9)/3)+0.02);
  152.             dJointSetHinge2Param(wheeljoint[3], dParamLoStop, M_PI/2-atan((tan(M_PI/2-steer)*3-1.9)/3));
  153.             dJointSetHinge2Param(wheeljoint[3], dParamHiStop, M_PI/2-atan((tan(M_PI/2-steer)*3-1.9)/3)+0.02);
  154.         }
  155.         else
  156.         {
  157.             dJointSetHinge2Param(wheeljoint[2], dParamLoStop, atan((tan(M_PI/2+steer)*3-1.9)/3)-M_PI/2);
  158.             dJointSetHinge2Param(wheeljoint[2], dParamHiStop, atan((tan(M_PI/2+steer)*3-1.9)/3)-M_PI/2+0.02);
  159.             dJointSetHinge2Param(wheeljoint[3], dParamLoStop, atan((tan(M_PI/2+steer)*3+1.9)/3)-M_PI/2);
  160.             dJointSetHinge2Param(wheeljoint[3], dParamHiStop, atan((tan(M_PI/2+steer)*3+1.9)/3)-M_PI/2+0.02);
  161.         }
  162. }
  163.  
  164.  
  165.