Advertisement
Guest User

RagdollDemo.cpp

a guest
Dec 27th, 2014
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.66 KB | None | 0 0
  1. /*
  2. RagdollDemo.cpp
  3.  
  4. */
  5.  
  6. #include <sstream>
  7. #include <iostream>
  8. #include <string>
  9. #include <math.h>
  10. #include <time.h>
  11. #include <fstream>
  12. #include <cstdio>
  13. #include <cmath>
  14. #include <float.h>
  15.  
  16. #define CONSTRAINT_DEBUG_SIZE 0.2f
  17. #ifndef M_PI
  18. #define M_PI       3.14159265358979323846
  19. #endif
  20.  
  21. #ifndef M_PI_2
  22. #define M_PI_2     1.57079632679489661923
  23. #endif
  24.  
  25. #ifndef M_PI_4
  26. #define M_PI_4     0.785398163397448309616
  27. #endif
  28.  
  29. #include "btBulletDynamicsCommon.h"
  30. #include "GlutStuff.h"
  31. #include "GL_ShapeDrawer.h"
  32. #include "LinearMath/btIDebugDraw.h"
  33. #include "GLDebugDrawer.h"
  34. #include "RagdollDemo.h"
  35. #include "RagDollclass.h"
  36.  
  37. using namespace std;
  38.  
  39.  
  40. void ConsoleCollideIDs(int Acid, int Bcid);
  41.  
  42. static RagdollDemo * ragdollDemo;
  43. static RagdollDemo * ragdollptrs[12];
  44.  
  45. #include "ContactCallbacks.h" // ..
  46.  
  47. bool myContactProcessedCallback(btManifoldPoint& cp,
  48.                                 void* body0, void* body1)
  49. {
  50.     int groundID = 9;
  51.     int conID1,conID2;
  52.     int * ID1;
  53.     int * ID2; 
  54.  
  55.     btCollisionObject * o1 = static_cast<btCollisionObject*>(body0);
  56.     btCollisionObject * o2 = static_cast<btCollisionObject*>(body1);
  57.    
  58.     ID1 = static_cast<int*>(o1->getUserPointer());
  59.     ID2 = static_cast<int*>(o2->getUserPointer());
  60.    
  61.     conID1 = (*ID1);   
  62.     conID2 = (*ID2);
  63.    
  64.     if((conID1<10)&&(conID2<10) ){
  65.         if( (conID1 == groundID) || (conID2 == groundID ) ){                       
  66.             ragdollDemo->touches[conID1]=1;
  67.             ragdollDemo->touches[conID2]=1;
  68.             ragdollDemo->touchPoints[conID1] = cp.m_positionWorldOnB;
  69.             ragdollDemo->touchPoints[conID2] = cp.m_positionWorldOnB;
  70.         }
  71.     }
  72.     return false;
  73. }
  74.  
  75.  
  76.  
  77. void RagdollDemo::initPhysics()
  78. {  
  79.     int i,  nocans;
  80.     //.
  81.  
  82.     // Setup the basic world
  83.     setTexturing(true);
  84.     setShadows(true);
  85.     setCameraDistance(btScalar(5.));
  86.     btVector3 worldAabbMin(-10000,-10000,-10000);
  87.     btVector3 worldAabbMax(10000,10000,10000);
  88.  
  89.     m_collisionConfiguration    = new btDefaultCollisionConfiguration();
  90.     m_dispatcher                = new btCollisionDispatcher(m_collisionConfiguration);
  91.     m_broadphase                = new btAxisSweep3 (worldAabbMin, worldAabbMax);
  92.     m_solver                    = new btSequentialImpulseConstraintSolver;
  93.     m_dynamicsWorld             = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
  94.  
  95.  
  96.     srand( (unsigned)(time(NULL)) );
  97.     pause = true;
  98.     oneStep = false;
  99.     position_sane = true;
  100.     globaljointA = 0.001;  
  101.     simtick = 0;
  102.     timeStep = 0;
  103.     i=0;
  104.     while( i<45 ) {
  105.         IDs[i] = i;
  106.         i++;
  107.     }
  108.    
  109.     // Setup a big ground box
  110.     {
  111.         btCollisionShape * groundShape = new btBoxShape(btVector3(btScalar(300.),btScalar(10.),btScalar(300.)));
  112.         m_collisionShapes.push_back(groundShape);
  113.         btTransform groundTransform;
  114.         groundTransform.setIdentity();
  115.         groundTransform.setOrigin(btVector3(0,-10,0));
  116.  
  117.         fixedGround = new btCollisionObject();
  118.         fixedGround->setCollisionShape(groundShape);
  119.         fixedGround->setWorldTransform(groundTransform);
  120.         m_dynamicsWorld->addCollisionObject(fixedGround);      
  121.     }  
  122.    
  123.     //cout << "CreateBox(0...)" << endl;
  124.     CreateBox(0,
  125.               0., 2., 0.,
  126.               1., 1., 0.2 ); // Create the robot body
  127.  
  128.     CreateCylinder(1,
  129.               2.02 , 2. , 0.0 ,
  130.               0.2 , 0.2 , 0.8 ,
  131.               M_PI_2 , 0. , 0. ); // West thigh
  132.  
  133.     CreateCylinder(2,
  134.               -2.02 , 2. , 0.0 ,
  135.               0.2 , 0.2 , 0.8 ,
  136.               M_PI_2 , 0. , 0. ); // East thigh
  137.  
  138.     CreateCylinder(3,
  139.               0. , 2. , -2.02 ,
  140.               0.2 , 0.2 , 0.8 ,
  141.               0 , 0. , M_PI_2 ); // South thigh
  142.  
  143.     CreateCylinder(4,
  144.               0. , 2. , 2.02  ,
  145.               0.2 , 0.2 , 0.8 ,
  146.               0 , 0. , M_PI_2 ); // North thigh
  147.  
  148.     CreateCylinder(5,
  149.               3.10 , 0.89 , 0.0 ,
  150.               0.2 ,  0.2 , 0.89 ,
  151.               0 , 0. , 0. ); // West foot
  152.  
  153.     CreateCylinder(6,
  154.               -3.10 , 0.89 , 0.0 ,
  155.               0.2 ,   0.2 , 0.89 ,
  156.               0 , 0. , 0. ); // East foot
  157.  
  158.     CreateCylinder(7,
  159.               0.00 , 0.89 , 3.10 ,
  160.               0.2 ,  0.2 , 0.89 ,
  161.               0 , 0. , 0. ); // North foot
  162.  
  163.     CreateCylinder(8,
  164.               0.00 , 0.89 , -3.10 ,
  165.               0.2 ,   0.2 , 0.89 ,
  166.               0 , 0. , 0. ); // South foot
  167.  
  168.  
  169.     if( !OPTloadbestweights ) {
  170.         //cout << "BuildWallofCans()" << endl;
  171.         BuildWallofCans();   // wall of cans
  172.     }
  173.  
  174.     CreateBox(9,
  175.               0., 2.01, 105.,
  176.               4., 2., 2. ); // Create a north marker
  177.    
  178.     CreateSphere(10,
  179.                 -3.00 , 2. , 0.0 ,
  180.                 0.27 );    // East kneeball
  181.  
  182.     CreateSphere(11,
  183.                 3.00 , 2. , 0.0 ,
  184.                 0.27 );    // West kneeball
  185.  
  186.     CreateSphere(12,
  187.                 0.0 , 2. , 3.00 ,
  188.                 0.27 );    // North kneeball
  189.  
  190.     CreateSphere(13,
  191.                 0.0 , 2. , -3.00 ,
  192.                 0.27 );    // South kneeball
  193.        
  194.    
  195.  
  196.     CreateWeld(0, 2, 10 ); // Weld east kneeball(10) to east thigh(2)
  197.     CreateWeld(1, 1, 11 ); // Weld west kneeball(11) to west thigh(1)
  198.     CreateWeld(2, 4, 12 ); // Weld north kneeball(12) to north thigh(4)
  199.     CreateWeld(3, 3, 13 ); // Weld south kneeball(13) to south thigh(3)
  200.        
  201.  
  202.     if( OPTloadbestweights ) {
  203.         nocans = 15;
  204.     } else {
  205.         nocans = 42;
  206.     }
  207.  
  208.     i=0;
  209.     while( i < nocans ) {
  210.         if(i==9) {
  211.             fixedGround->setUserPointer( &(IDs[i]) );
  212.             (body[i])->setUserPointer(  &(IDs[14]) );
  213.         } else {
  214.             if( i < 14 ) {
  215.                 (body[i])->setUserPointer(  &(IDs[i] ));
  216.             }
  217.  
  218.             if( i > 14 ) {
  219.                 (body[i])->setUserPointer(  &(IDs[i] ));
  220.             }
  221.         }
  222.         i++;
  223.     }
  224.  
  225.     //cout << "CreateHinge(0.." << endl;
  226.  
  227.     // JOINT = 0
  228.     // East thigh(2) connects East foot(6)
  229.     CreateHinge(0 , 2 , 6 ,
  230.         -3.10 , 2.00 , 0.0,
  231.          0.00 , 0.00 , -1.00 );
  232.      
  233.     // JOINT = 1
  234.     // West thigh(1) connects West foot(5)
  235.     CreateHinge(1 , 1 , 5,
  236.          3.10 , 2.00 , 0.0,
  237.          0.00 , 0.00 , -1.00 );
  238.  
  239.     // JOINT = 2
  240.     // South thigh(3) connects South foot(8)
  241.     CreateHinge(2 , 8 , 3,
  242.           0.00 ,  2.00 , -3.10,
  243.          -1.00 , 0.00 , 0.00 );
  244.  
  245.     // JOINT = 3
  246.     // North thigh(4) connects North foot(7)
  247.     CreateHinge(3 , 7 , 4,
  248.           0.00 ,  2.00 , 3.10,
  249.          -1.00 , 0.00 ,  0.00 );
  250.    
  251.    
  252.     // JOINT = 4
  253.     // body connects East thigh(2)
  254.     CreateHinge(4 , 0 , 2,
  255.           -1.02 , 2.00 , 0.00,
  256.            0.00 , 0.00 , -1.00 );
  257.  
  258.     // JOINT = 5
  259.     // body connects West thigh(1)
  260.     CreateHinge(5 , 0 , 1,
  261.            1.02 , 2.00 , 0.00,
  262.            0.00 , 0.00 , -1.00 );
  263.  
  264.     // JOINT = 6
  265.     // body connects North thigh(4)
  266.     CreateHinge(6 , 0 , 4,
  267.             0.00 , 2.00 , 1.02,
  268.            -1.00 , 0.00 , 0.00 );
  269.  
  270.     // JOINT = 7
  271.     // body connects South thigh(3)
  272.     CreateHinge(7 , 0 , 3,
  273.             0.00 , 2.00 , -1.02,
  274.            -1.00 , 0.00 , 0.00 );
  275.     i=0;
  276.     while(i<8){
  277.         (joints[i])->enableMotor(true);
  278.         i++;
  279.     }  
  280.  
  281.     if( threadID < 0 ) {
  282.         //cout << "gContactProcessedCallback" << endl;
  283.         ragdollDemo = this;
  284.         gContactProcessedCallback = myContactProcessedCallback;
  285.     } else {
  286.         ragdollptrs[threadID] = this;      
  287.         AssignToCallback();
  288.     }
  289.  
  290.     //cout << "clientResetScene()" << endl;
  291.     pause = false;
  292.     clientResetScene();    
  293. }
  294.  
  295.  
  296. // * //
  297.  
  298. void RagdollDemo::clientMoveAndDisplay()
  299. {
  300.     long int rollover_escape;
  301.     int nn, m;
  302.     bool sS;   
  303.     bool air;
  304.     double deltaT;
  305.     double motorCommand;   
  306.     //.
  307.  
  308.     if( OPTdrawgraphics ) {
  309.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  310.     }
  311.  
  312.     //simple dynamics world doesn't handle fixed-time-stepping
  313.     float ms = getDeltaTimeMicroseconds();
  314.     float minFPS = 90000.f/60.f;
  315.     if (ms > minFPS) ms = minFPS;
  316.     deltaT = ms / 90000.f;
  317.  
  318.     if (m_dynamicsWorld)
  319.     {
  320.         sS = false;
  321.         if(!pause){
  322.             sS=true;
  323.         }
  324.         if( pause && oneStep ) {
  325.             oneStep = false;
  326.             sS = true;         
  327.         }
  328.  
  329.         if(sS) {           
  330.             m=0;
  331.             while( m<15 ) {
  332.                 touches[m] = 0;
  333.                 m++;
  334.             }
  335.             air = true; // because of the above.
  336.            
  337.            
  338.             rollover_escape = 1;
  339.             while ( air && (rollover_escape < 1510) )
  340.             {                  
  341.                     m_dynamicsWorld->stepSimulation( btScalar(deltaT) );
  342.  
  343.                     if( !SanityCheck( 500.0 ) ) {
  344.                         // The physics simulation is crashing?
  345.                         rollover_escape = 9999;  
  346.                         cout << "Sanity check failed at timeStep = ";
  347.                         cout << timeStep;
  348.                         cout <<endl;
  349.                     }
  350.  
  351.                     air = airborne();
  352.                     rollover_escape++;
  353.             }
  354.  
  355.             if( rollover_escape > 1505 ) {
  356.                 // The robot is upside down, or it fell off the earth.                     
  357.                 if( OPTdrawgraphics ) {
  358.                     pause = true;  // Trial runs pause.                          
  359.                 } else {
  360.                     timeStep = 987123123;  // Fitness tests should terminate.
  361.                 }
  362.             }
  363.            
  364.            
  365.  
  366.             if( simtick == 0 ) {
  367.                 m=0;
  368.                 while(m<8) {               
  369.                     nn=0;
  370.                     while( nn < 4 ) {
  371.                         // We need the touches from 5,6,7, and 8
  372.                         touchsensor[nn] = (double)touches[nn+5];
  373.                         nn++;
  374.                     }
  375.  
  376.                     motorCommand = 0.0;
  377.                     nn=0;
  378.                     while( nn < 4 ) {
  379.                         motorCommand = motorCommand +
  380.                                        (touchsensor[nn] * weights[nn][m]);
  381.                         nn++;
  382.                     }
  383.  
  384.                     motorCommand = tanh(motorCommand);
  385.                     motorCommand = motorCommand * 45.0;
  386.                     ActuateJoint2(m , motorCommand , -1.  ,  deltaT );
  387.                     m++;
  388.                 }          
  389.             }
  390.  
  391.             simtick = (simtick+1)%23;
  392.         }
  393.  
  394.         if(OPTdrawgraphics) {
  395.             //optional but useful: debug drawing
  396.             m_dynamicsWorld->debugDrawWorld();
  397.         }
  398.  
  399.         timeStep++;
  400.     }
  401.  
  402.     if(OPTdrawgraphics) {
  403.         renderme();
  404.         glFlush();
  405.         glutSwapBuffers();
  406.     }
  407. }
  408.  
  409.  
  410. // * //
  411.  
  412. void    RagdollDemo::exitPhysics()
  413. {
  414.     int i, nocans;
  415.  
  416.     i=8;
  417.     while(i>0){
  418.         i--;
  419.         (joints[i])->enableMotor(false);
  420.     }  
  421.  
  422.     i=8;
  423.     while(i>0) {
  424.         i--;
  425.         DestroyHinge(i);
  426.     }  
  427.  
  428.     i=4;
  429.     while(i>0) {
  430.         i--;
  431.         DestroyWeld(i);
  432.     }      
  433.      
  434.  
  435.     if( OPTloadbestweights ){nocans = 14;}
  436.     else                    {nocans = 42;}
  437.      
  438.     i=nocans;
  439.     while(i>0) {
  440.         i--;
  441.         if( i != 14 ){  DeleteObject(i); }
  442.     }
  443.      
  444.    
  445.    
  446.     //cleanup in the reverse order of creation/initialization
  447.     //remove the rigidbodies from the dynamics world and delete them
  448.    
  449.     for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
  450.     {
  451.         btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
  452.         btRigidBody* body = btRigidBody::upcast(obj);
  453.         if (body && body->getMotionState())
  454.         {
  455.             delete body->getMotionState();
  456.         }
  457.         m_dynamicsWorld->removeCollisionObject( obj );
  458.         delete obj;
  459.     }
  460.  
  461.      
  462.     //delete collision shapes
  463.     for (int j=0;j<m_collisionShapes.size();j++)
  464.     {
  465.         btCollisionShape* shape = m_collisionShapes[j];
  466.         delete shape;
  467.     }
  468.    
  469.     delete m_dynamicsWorld;
  470.     delete m_solver;
  471.     delete m_broadphase;
  472.     delete m_dispatcher;
  473.     delete m_collisionConfiguration;   
  474. }
  475.  
  476.  
  477. double RagdollDemo
  478. ::Fitness(void)
  479. {      
  480.     if( position_sane ) {
  481.         btRigidBody *  spbody   = body[0];
  482.         btTransform btT         = spbody->getWorldTransform();
  483.         btVector3 mbodypos      = btT.getOrigin();
  484.         return( (double)mbodypos.getZ() );
  485.     } else {
  486.         return( -99.88888 );
  487.     }
  488. }
  489.  
  490.  
  491.  
  492. bool RagdollDemo
  493. ::SanityCheck(double maxbound)
  494. {
  495.     double xrobo, yrobo, zrobo;
  496.     btRigidBody *  spbody   ;
  497.     btTransform btT         ;
  498.     btVector3 mbodypos      ;
  499.     //.
  500.  
  501.     spbody      = body[0];
  502.     btT         = spbody->getWorldTransform();
  503.     mbodypos    = btT.getOrigin();
  504.     zrobo       = (double)mbodypos.getZ();
  505.     xrobo       = (double)mbodypos.getX();
  506.     yrobo       = (double)mbodypos.getY(); 
  507.  
  508.     if( _isnan( zrobo ) ) {
  509.         cout << "Z coord is not a number." << endl;
  510.         position_sane = false;
  511.     }
  512.  
  513.     if( _isnan( xrobo ) ) {
  514.         cout << "X coord is not a number." << endl;
  515.         position_sane = false;
  516.     }
  517.  
  518.     if( _isnan( yrobo ) ) {
  519.         cout << "Y coord is not a number." << endl;
  520.         position_sane = false;
  521.     }
  522.  
  523.     zrobo = fabs(zrobo);
  524.     xrobo = fabs(xrobo);
  525.     yrobo = fabs(yrobo);
  526.  
  527.     if( zrobo > maxbound ) position_sane = false;
  528.     if( xrobo > maxbound ) position_sane = false;
  529.     if( yrobo > maxbound ) position_sane = false;
  530.  
  531.     return position_sane;
  532. }
  533.  
  534.  
  535. void RagdollDemo
  536. ::WeightFileHandler(void)
  537. {
  538.     int succget;
  539.     long int fpwL;
  540.     char fullpathw[256];
  541.     //.
  542.  
  543.     strcpy(fullpathw, "C:\\Project_Integration\\");
  544.     strcat(fullpathw, OPTbestweightsfile );
  545.     fpwL = strlen(fullpathw);
  546.     cout << "fullpathw[] has length ";
  547.     cout << fpwL << endl;  
  548.     succget = GetFileWeights( fullpathw );
  549.     if( succget == 1 ) {       
  550.         cout << "Loaded weights from file ";                   
  551.     }else {
  552.         cout << "!ERROR. Failed to read file ";
  553.     }
  554.     cout << fullpathw;
  555.     cout << endl;      
  556. }
  557.  
  558.  
  559.  
  560. void RagdollDemo
  561. ::BuildWallofCans(void)
  562. {
  563.     int can;
  564.     double Xct, Yct, pang, sweep, R;
  565.     const double raddeg = 0.0174532925199433L;
  566.     //.
  567.  
  568.     R = 10.0;
  569.     sweep = 60.0 * raddeg;
  570.     pang  =  6.0 * raddeg;
  571.    
  572.     can=0;
  573.     while(can<10) {
  574.         Xct = -1.0 * R * cos(sweep);
  575.         Yct = R * sin(sweep);
  576.         CreateCylinder(15+can,
  577.               Xct , 0.4901 , Yct ,
  578.               0.4 ,   0.4 , 0.49 ,
  579.               0 , 0. , 0. );
  580.  
  581.         sweep += pang ;
  582.         can   += 1 ;
  583.     }
  584.  
  585.     sweep = 63.0 * raddeg;
  586.     pang =   6.0 * raddeg; 
  587.     can=0;
  588.     while(can<9) {
  589.         Xct = -1.0 * R * cos(sweep);
  590.         Yct = R * sin(sweep);
  591.         CreateCylinder(25+can,
  592.               Xct , 3.0 * 0.4901 , Yct ,
  593.               0.4 ,   0.4 , 0.49 ,
  594.               0 , 0. , 0. );
  595.  
  596.         sweep += pang ;
  597.         can   += 1 ;
  598.     }
  599.  
  600.     sweep = 66.0 * raddeg;
  601.     pang =   6.0 * raddeg; 
  602.     can=0;
  603.     while(can<8) {
  604.         Xct = -1.0 * R * cos(sweep);
  605.         Yct = R * sin(sweep);
  606.         CreateCylinder(34+can,
  607.               Xct , 5.0 * 0.4901 , Yct ,
  608.               0.4 ,   0.4 , 0.49 ,
  609.               0 , 0. , 0. );
  610.  
  611.         sweep += pang ;
  612.         can   += 1 ;
  613.     }
  614. }
  615.  
  616. void RagdollDemo
  617. ::CreateBox(
  618.         int index,
  619.         double x ,      double y ,      double z ,
  620.         double length , double width , double height)
  621. {
  622.     btCollisionShape * btCSptr;
  623.     btDefaultMotionState * myMotionState;
  624.     btTransform offset;
  625.     btTransform transform;
  626.     btTransform startTransform;
  627.     btVector3 localInertia(0,0,0);
  628.     double mass;
  629.     //.
  630.    
  631.     mass = 1.000;
  632.  
  633.     // `offset` is the location of the entire robot in world.
  634.     offset.setIdentity();
  635.     offset.setOrigin(  btVector3( btScalar(0.) , btScalar(0.) , btScalar(0.))    );
  636.  
  637.     // `transform` is the  local body coordinates of appendages.  
  638.     transform.setIdentity();
  639.     transform.setOrigin(
  640.         btVector3(
  641.             btScalar(x) ,
  642.             btScalar(y) ,
  643.             btScalar(z) )
  644.     );
  645.  
  646.     geom[index] = new btBoxShape(  
  647.             btVector3(
  648.                 btScalar(length) ,
  649.                 btScalar(height) ,
  650.                 btScalar(width ) )
  651.         );
  652.     btCSptr = geom[index];
  653.  
  654.     btCSptr->calculateLocalInertia( btScalar(mass) , localInertia );  
  655.  
  656.     myMotionState = new btDefaultMotionState(offset*transform);
  657.    
  658.     btRigidBody::btRigidBodyConstructionInfo rbInfo(
  659.         btScalar(mass),myMotionState,btCSptr,localInertia
  660.         );
  661.        
  662.     body[index] = new btRigidBody(rbInfo); 
  663.  
  664.     m_dynamicsWorld->addRigidBody( body[index] );
  665. }
  666.  
  667.  
  668.  
  669. void RagdollDemo
  670. ::CreateCylinder(
  671.         int index,
  672.         double x,       double y,       double z,
  673.         double length,  double width,   double height,
  674.         double xrot ,   double yrot ,   double zrot )
  675. {
  676.     btCollisionShape * btCSptr;
  677.     btDefaultMotionState * myMotionState;
  678.     btTransform offset;
  679.     btTransform transform;
  680.     btTransform startTransform;
  681.     btVector3 localInertia(0,0,0);
  682.     double mass;
  683.     //.
  684.    
  685.     mass = 1.000;
  686.  
  687.     // `offset` is the location of the entire robot in world.
  688.     offset.setIdentity();
  689.     offset.setOrigin(  btVector3( btScalar(0.) , btScalar(0.) , btScalar(0.))    );
  690.  
  691.     // `transform` is the  local body coordinates of appendages.  
  692.     transform.setIdentity();
  693.     transform.setOrigin(
  694.         btVector3(
  695.             btScalar(x) ,
  696.             btScalar(y) ,
  697.             btScalar(z) )
  698.     );
  699.     transform.getBasis().setEulerZYX(
  700.         btScalar(zrot) ,
  701.         btScalar(yrot),
  702.         btScalar(xrot) );
  703.  
  704.     geom[index] = new btCylinderShape(  
  705.             btVector3(
  706.                 btScalar(length) ,
  707.                 btScalar(height) ,
  708.                 btScalar(width ) )
  709.         );
  710.     btCSptr = geom[index];
  711.  
  712.     btCSptr->calculateLocalInertia( btScalar(mass) , localInertia );  
  713.  
  714.     myMotionState = new btDefaultMotionState(offset*transform);
  715.    
  716.     btRigidBody::btRigidBodyConstructionInfo rbInfo(
  717.         btScalar(mass),myMotionState,btCSptr,localInertia);
  718.        
  719.     body[index] = new btRigidBody(rbInfo);
  720.  
  721.     m_dynamicsWorld->addRigidBody( body[index] );
  722. }
  723.  
  724.  
  725. void RagdollDemo
  726. ::CreateSphere(
  727.         int index,
  728.         double x,       double y,       double z,
  729.         double radius )
  730. {
  731.     btCollisionShape * btCSptr;
  732.     btDefaultMotionState * myMotionState;
  733.     btTransform offset;
  734.     btTransform transform;
  735.     btTransform startTransform;
  736.     btVector3 localInertia(0,0,0);
  737.     double mass;
  738.     //.
  739.    
  740.     mass = 0.30;
  741.  
  742.     // `offset` is the location of the entire robot in world.
  743.     offset.setIdentity();
  744.     offset.setOrigin(  btVector3( btScalar(0.) , btScalar(0.) , btScalar(0.))    );
  745.  
  746.     // `transform` is the  local body coordinates of appendages.  
  747.     transform.setIdentity();
  748.     transform.setOrigin(
  749.         btVector3(
  750.             btScalar(x) ,
  751.             btScalar(y) ,
  752.             btScalar(z) )
  753.     );
  754.  
  755.     geom[index] = new btSphereShape(  btScalar(radius)  );
  756.     btCSptr = geom[index];
  757.  
  758.     btCSptr->calculateLocalInertia( btScalar(mass) , localInertia );  
  759.  
  760.     myMotionState = new btDefaultMotionState(offset*transform);
  761.    
  762.     btRigidBody::btRigidBodyConstructionInfo rbInfo(
  763.         btScalar(mass) , myMotionState,btCSptr,localInertia);
  764.        
  765.     body[index] = new btRigidBody(rbInfo);
  766.  
  767.     m_dynamicsWorld->addRigidBody( body[index] );
  768. }
  769.  
  770.  
  771.  
  772.  
  773. void RagdollDemo
  774. ::CreateHinge(int index,
  775.                      int body1, int body2,
  776.                      double x, double y, double z,
  777.                      double ax, double ay, double az)
  778. {
  779.     double convr, uplim, lowlim, defangle;
  780.     btVector3 p;
  781.     btVector3 a;
  782.     //.
  783.  
  784.     p.setX( btScalar(x) );
  785.     p.setY( btScalar(y) );
  786.     p.setZ( btScalar(z) );
  787.  
  788.     a.setX( btScalar(ax) );
  789.     a.setY( btScalar(ay) );
  790.     a.setZ( btScalar(az) );
  791.  
  792.     btVector3 p1 = PointWorldToLocal(body1, p);
  793.     btVector3 p2 = PointWorldToLocal(body2, p);
  794.  
  795.     btVector3 a1 = AxisWorldToLocal(body1, a);
  796.     btVector3 a2 = AxisWorldToLocal(body2, a);
  797.  
  798.     joints[index] =
  799.         new btHingeConstraint(*body[body1], *body[body2],
  800.                                p1, p2,
  801.                                a1, a2, false);
  802.  
  803.     switch(index) {
  804.  
  805.     // Knees
  806.     case 0:   // East
  807.         lowlim = -60.0;   // Swing out   ( Actuator -45 deg)
  808.         uplim  =  25.0;    // Pull in    ( Actuator +45 deg)
  809.         defangle = 270.0;      
  810.         break;
  811.     case 1:   // West
  812.         lowlim =  -25.0;   // pull in    ( Actuator +45 deg)
  813.         uplim  =  60.0;    // Swing out   ( Actuator -45 deg)
  814.         defangle = 270.0;          
  815.         break;
  816.     case 2:   // South
  817.         lowlim = -60.0;    // swing out   ( Actuator -45 deg)
  818.         uplim  =  25.0;     // pull in   ( Actuator +45 deg)
  819.         defangle = 90.0;               
  820.         break;
  821.     case 3:   // North
  822.         lowlim = -25.0;    // pull in   ( Actuator +45 deg)
  823.         uplim  =  60.0;   // swing out    ( Actuator -45 deg)
  824.         defangle = 90.0;           
  825.         break;
  826.  
  827.     // Body joints
  828.     case 4:    // East
  829.         lowlim =     -35.0;  // Leg above body    ( Actuator -45 deg)
  830.         uplim  =      45.0;  // Leg goes below  ( Actuator +45 deg)
  831.         defangle = 90.0;   
  832.         break;
  833.     case 5:  // West  *@*
  834.         lowlim =  -45.0;   // Leg goes below body  ( Actuator +45 deg)  
  835.         uplim  =   35.0;   // Leg goes above body   ( Actuator -45 deg)
  836.         defangle = 90.0;
  837.         break;
  838.     case 6:   // North
  839.         lowlim =   -35.0;  // Leg above body          ( Actuator -45 deg)
  840.         uplim  =    45.0;   // Leg goes below      ( Actuator +45 deg)  
  841.         defangle = 90.0;
  842.         break;
  843.     case 7:   // South *@*
  844.         lowlim =  -45.0;   // Leg goes below body    ( Actuator +45 deg)
  845.         uplim  =   35.0;   // Let goes up above body   ( Actuator -45 deg)
  846.         defangle = 90.0;
  847.         break;
  848.     }
  849.    
  850.     convr  = (double)0.0174532925199433;
  851.  
  852.     (joints[index])->setLimit(
  853.         btScalar((lowlim+defangle)*convr ) ,
  854.         btScalar((uplim +defangle)*convr )
  855.         );
  856.  
  857.     m_dynamicsWorld->addConstraint(joints[index]);
  858. }
  859.  
  860.  
  861.  
  862. void RagdollDemo
  863. ::CreateWeld(int index,
  864.              int body1, int body2 )
  865. {  
  866.     btRigidBody * rbA;
  867.     btRigidBody * rbB;
  868.     btTransform trA,trB;
  869.     btTransform globalFrame;
  870.     //.
  871.  
  872.  
  873.     rbA = body[body1];
  874.     rbB = body[body2];         
  875.     trA.setIdentity();
  876.     trB.setIdentity();
  877.     globalFrame.setIdentity();
  878.     globalFrame.setOrigin(btVector3( btScalar(0.) , btScalar(0.) , btScalar(0.)));
  879.  
  880.     trA = (  ((rbA)->getWorldTransform()).inverse() ) *  (globalFrame);
  881.     trB = (  ((rbB)->getWorldTransform()).inverse() ) *  (globalFrame);
  882.  
  883.     welds[index] = new btFixedConstraint( *rbA , *rbB , trA , trB );
  884.  
  885.     (welds[index])->setOverrideNumSolverIterations(100);
  886.  
  887.     bool disableCollisionsBetweenLinkedBodies=true;
  888.  
  889.     m_dynamicsWorld->addConstraint(welds[index], disableCollisionsBetweenLinkedBodies );   
  890. }
  891.  
  892.  
  893.  
  894.  
  895.  
  896. void RagdollDemo
  897. ::DeleteObject( int index )
  898. {
  899.     m_dynamicsWorld->removeRigidBody( body[index] );
  900.     delete body[index];
  901.     delete geom[index];
  902. }
  903.  
  904.  
  905. void RagdollDemo
  906. ::DestroyHinge( int index )
  907. {
  908.     m_dynamicsWorld->removeConstraint( joints[index] );
  909.     delete joints[index];
  910. }
  911.  
  912. void RagdollDemo
  913. ::DestroyWeld( int index )
  914. {
  915.     m_dynamicsWorld->removeConstraint( welds[index] );
  916.     delete welds[index];
  917. }
  918.  
  919.  
  920.  
  921.  
  922. btVector3 RagdollDemo
  923. ::PointWorldToLocal(int index, btVector3 &p)
  924. {
  925.   btVector3 btvloc;
  926.   btTransform local1  = body[index]->getCenterOfMassTransform().inverse();  
  927.   //.
  928.   btvloc = (local1)*(p);
  929.   return (btvloc);
  930. }
  931.  
  932. btVector3 RagdollDemo
  933. ::AxisWorldToLocal(int index, btVector3 &a) {
  934.   btTransform local1 = body[index]->getCenterOfMassTransform().inverse();
  935.   btVector3 zero(0,0,0);
  936.   local1.setOrigin(zero);
  937.   return (
  938.       local1 * a
  939.     );
  940. }
  941.  
  942.  
  943. double RagdollDemo
  944. ::RandomJoint(void)
  945. {
  946.     unsigned lu, hu;
  947.     unsigned long int HI, LO, J;
  948.     double rJ, rD;
  949.     //.
  950.    
  951.     lu = rand();
  952.     hu = rand();   
  953.     HI = ((unsigned long int)hu) &(0x00006FFF);  
  954.     LO = ((unsigned long int)lu) &(0x0000FFFF);
  955.     J  = (HI<<16) | (LO);
  956.     rJ = (double)J;
  957.     rD = (double)(0x6FFFFFFF);
  958.     return( (90.00L*(rJ/rD)) - (45.00L) );
  959. }
  960.  
  961.  
  962. double RagdollDemo
  963. ::RandomWeight(void)
  964. {
  965.     unsigned lu, hu;
  966.     unsigned long int HI, LO, J;
  967.     double rJ, rD;
  968.     //.
  969.    
  970.     lu = rand();
  971.     hu = rand();   
  972.     HI = ((unsigned long int)hu) &(0x00006FFF);  
  973.     LO = ((unsigned long int)lu) &(0x0000FFFF);
  974.     J  = (HI<<16) | (LO);
  975.     rJ = (double)J;
  976.     rD = (double)(0x6FFFFFFF);
  977.     return( (2.00L*(rJ/rD)) - (1.00L) );
  978. }
  979.  
  980.  
  981.  
  982. // * //  
  983. // ActuateJoint
  984. void RagdollDemo   
  985. ::ActuateJoint(
  986.     int jointIndex,
  987.     double desiredAngle,
  988.     double jointOffset,
  989.     double timeStep)
  990. {
  991.     double udA;
  992.     btHingeConstraint * lHC;
  993.     //.
  994.  
  995.     lHC = joints[jointIndex];
  996.     udA = ConvertActuatorToJoint( jointIndex , desiredAngle );
  997.     udA *= (double)0.0174532925199433L;
  998.  
  999.     lHC->setMaxMotorImpulse( 0.01 );
  1000.     lHC->setMotorTarget( btScalar(udA) , btScalar(timeStep) );
  1001. }
  1002.  
  1003.  
  1004. // * //
  1005. // * // ActuateJoint2  II
  1006. void RagdollDemo   
  1007. ::ActuateJoint2(   
  1008.     int jointIndex,
  1009.     double desiredAngle,
  1010.     double jointOffset,
  1011.     double timeStep)
  1012. {
  1013.     double udA, cjA, diff, mmi;
  1014.     btHingeConstraint * lHC;
  1015.     //.
  1016.  
  1017.     lHC = joints[jointIndex];
  1018.     cjA = lHC->getHingeAngle();
  1019.     udA = ConvertActuatorToJoint( jointIndex , desiredAngle );
  1020.     udA *= (double)0.0174532925199433L;
  1021.     diff = udA - cjA;
  1022.     mmi = 1.45;
  1023.     lHC->setMaxMotorImpulse( btScalar(mmi) );
  1024.     lHC->enableAngularMotor(true, btScalar(5.0*diff), btScalar(1.0) );
  1025.     lHC->setMaxMotorImpulse( btScalar(mmi) );
  1026. }
  1027.  
  1028.  
  1029.  
  1030. double RagdollDemo 
  1031. ::ConvertActuatorToJoint( int index , double actang )
  1032. {
  1033.     double usang, ja, station, retang;
  1034.  
  1035.     usang = actang / 45.0;
  1036.  
  1037.     switch(index) {
  1038.         // Knees
  1039.         case 0:   // East
  1040.             if( usang < 0.0 )   { ja = -60.0; }
  1041.             else                { ja =  25.0; }
  1042.             station = -90.0;   
  1043.             break;
  1044.         case 1:   // West
  1045.             if( usang < 0.0 )   { ja =  60.0; }
  1046.             else                { ja = -25.0; }
  1047.             station = -90.0;           
  1048.             break;
  1049.         case 2:   // South
  1050.             if( usang < 0.0 )   { ja = -60.0; }
  1051.             else                { ja =  25.0; }        
  1052.             station = 90.0;        
  1053.             break;
  1054.         case 3:   // North
  1055.             if( usang < 0.0 )   { ja =  60.0; }
  1056.             else                { ja = -25.0; }        
  1057.             station = 90.0;
  1058.             break;
  1059.  
  1060.         // Body joints
  1061.         case 4:    // East
  1062.             if( usang < 0.0 )   { ja = -35.0; }
  1063.             else                { ja =  45.0; }        
  1064.             station = 90.0;
  1065.             break;
  1066.            
  1067.         case 5:  // West  *@*
  1068.  
  1069.             if( usang < 0.0 )   { ja =  35.0; }
  1070.             else                { ja = -45.0; }        
  1071.             station = 90.0;
  1072.             break; 
  1073.            
  1074.         case 6:   // North
  1075.  
  1076.             if( usang < 0.0 )   { ja =  -35.0; }
  1077.             else                { ja =  45.0; }        
  1078.             station = 90.0;
  1079.             break;
  1080.  
  1081.            
  1082.         case 7:   // South *@*
  1083.             if( usang < 0.0 )   { ja =  35.0; }
  1084.             else                { ja = -45.0; }        
  1085.             station = 90.0;
  1086.             break;
  1087.     }
  1088.    
  1089.     retang = station +  ( (ja) * (fabs(usang)) );
  1090.  
  1091.     return (retang);
  1092. }
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099. void RagdollDemo::displayCallback()
  1100. {
  1101.     if(OPTdrawgraphics) {
  1102.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1103.  
  1104.         renderme();
  1105.  
  1106.         //optional but useful: debug drawing
  1107.         if (m_dynamicsWorld)
  1108.             m_dynamicsWorld->debugDrawWorld();
  1109.  
  1110.         glFlush();
  1111.         glutSwapBuffers();
  1112.     }
  1113. }
  1114.  
  1115. void RagdollDemo::keyboardCallback(unsigned char key, int x, int y)
  1116. {
  1117.     switch (key)
  1118.     {  
  1119.     case 'p':
  1120.         {
  1121.         pause = !pause;
  1122.         break;
  1123.         }
  1124.     case 'o':
  1125.         {
  1126.         oneStep = true;
  1127.         break;
  1128.         }
  1129.     case '1':
  1130.         {
  1131.         globaljointA = 45.0;
  1132.         break;
  1133.         }
  1134.     case '2':
  1135.         {
  1136.         globaljointA = 0.001;
  1137.         break;
  1138.         }
  1139.     case '3':
  1140.         {
  1141.         globaljointA = -45.0;
  1142.         break;
  1143.         }
  1144.     case 'n':
  1145.         {
  1146.             int sen;
  1147.             int mot;
  1148.             mot=0;
  1149.             while(mot<8) {
  1150.                 sen=0;
  1151.                 while(sen<4) {
  1152.                     weights[sen][mot] = RandomWeight();
  1153.                     sen++;
  1154.                 }
  1155.                 mot++;
  1156.             }      
  1157.         }
  1158.         break;
  1159.     default:
  1160.         DemoApplication::keyboardCallback(key, x, y);
  1161.     }
  1162.  
  1163.    
  1164. }
  1165.  
  1166.  
  1167.  
  1168.  
  1169. int RagdollDemo
  1170. ::GetFileWeights(char * GFWname )
  1171. {
  1172.     int wread;
  1173.     int sen, mot;
  1174.     double Wij;
  1175.     char Wijtxt[80];
  1176.     string line;
  1177.     ifstream myfile;
  1178.     //.
  1179.  
  1180.     myfile.open (GFWname, ios::in);
  1181.     if (myfile.is_open())
  1182.     {
  1183.         sen=0;
  1184.         mot=0;
  1185.         wread = 0;
  1186.         while ( getline (myfile,line) )
  1187.         {
  1188.             if( wread < 32 ) {
  1189.                 strcpy(Wijtxt, (char *)(line.c_str()) );
  1190.                 sscanf(Wijtxt, "%Lf,", &Wij );
  1191.                 weights[sen][mot] = Wij;               
  1192.                 sen++;
  1193.                 if( sen >= 4 ) {
  1194.                     sen = 0;
  1195.                     mot++;
  1196.                 }
  1197.             }
  1198.             wread++;
  1199.         }
  1200.         myfile.close();
  1201.         if( wread > 31 ) {
  1202.             return 1;
  1203.         } else {
  1204.             cout << "Failed to read all 32 weights from disk.";
  1205.             pause= true;
  1206.             return -3;
  1207.         }
  1208.     }else {
  1209.         cout << "Unable to open ";
  1210.         cout << GFWname;
  1211.         cout << endl;
  1212.         pause = true;
  1213.         return -1;
  1214.     }
  1215.  
  1216.     return 1;
  1217. }
  1218.  
  1219.  
  1220. int RagdollDemo
  1221. ::SavePosition( btRigidBody * spbody, char * SPname )
  1222. {
  1223.     int k;
  1224.     double zd;
  1225.     char positxt[80];  
  1226.     ofstream fitfile;  
  1227.     btTransform btT;
  1228.     btVector3 mbodypos;
  1229.     //.
  1230.  
  1231.     btT = spbody->getWorldTransform();
  1232.     mbodypos = btT.getOrigin();
  1233.     zd = (double)(mbodypos.getZ());        
  1234.     sprintf(positxt, "%.11Lf\r\n", zd );
  1235.     /*
  1236.     cout << "z = ";
  1237.     ConsoleFloat(zd);
  1238.     k = ConsoleWait();  */
  1239.  
  1240.     fitfile.open(SPname, ios::out );
  1241.     if (fitfile.is_open()) {
  1242.         k=0;
  1243.         while( k < 12 ) {
  1244.             fitfile << positxt;
  1245.             k++;
  1246.         }
  1247.         fitfile.close();       
  1248.         return 1;
  1249.     } else {
  1250.         cout << "Unable to open output fitness file." << endl;
  1251.         k = ConsoleWait();
  1252.         return -1;
  1253.     }
  1254.     return 1;
  1255. }
  1256.  
  1257.  
  1258.  
  1259. void RagdollDemo
  1260. ::ConsoleFloat(double cf)
  1261. {
  1262.     std::stringstream os;
  1263.     std::string cjstr;
  1264.     //.
  1265.     os.flush();
  1266.     os.precision(7);
  1267.     os << std::fixed << cf;
  1268.     cjstr = os.str();  
  1269.     std::cout << cjstr;
  1270.     std::cout << std::endl;
  1271. }
  1272.  
  1273.  
  1274. void RagdollDemo
  1275. ::ConsoleTouches(void)
  1276. {
  1277.     int t;
  1278.     std::stringstream os;
  1279.     std::string cjstr;
  1280.     //.
  1281.     os.flush();
  1282.     t=0;
  1283.     while(t<9) {
  1284.         if( t == 5 ) {
  1285.             os << " ";
  1286.         }
  1287.         os << touches[t];
  1288.         t++;
  1289.     }
  1290.     cjstr = os.str();
  1291.     cjstr.append("\r\n");
  1292.     std::cout << cjstr;
  1293. }
  1294.  
  1295.  
  1296. void ConsoleCollideIDs(int Acid, int Bcid)
  1297. {
  1298.     std::stringstream os;
  1299.     std::string cjstr; 
  1300.     //.
  1301.     os.flush();
  1302.     os << "ID1 = " << Acid << " , ID2 = " << Bcid << "\r\n";
  1303.     cjstr = os.str();
  1304.     std::cout << cjstr;
  1305. }
  1306.  
  1307.  
  1308. int ConsoleWait(void)
  1309. {
  1310.     int ink, lastgc;
  1311.    
  1312.     lastgc = ' ';  
  1313.     while((ink = getchar()) != EOF) {
  1314.         if (ink == '\n') {
  1315.             break;
  1316.         } else {
  1317.             lastgc = ink;
  1318.         }
  1319.     }
  1320.    
  1321.     return lastgc; 
  1322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement