Advertisement
Guest User

widdled down BullPhysSim

a guest
Dec 27th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.58 KB | None | 0 0
  1. #include <sstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <math.h>
  5. #include <time.h>
  6. #include <fstream>
  7. #include <cstdio>
  8.  
  9. #define CONSTRAINT_DEBUG_SIZE 0.2f
  10.  
  11. #include "btBulletDynamicsCommon.h"
  12. #include "GlutStuff.h"
  13. #include "GL_ShapeDrawer.h"
  14. #include "LinearMath/btIDebugDraw.h"
  15. #include "GLDebugDrawer.h"
  16.  
  17. #include "GlutDemoApplication.h"
  18. #include "btBulletCollisionCommon.h"
  19. #include "btBulletDynamicsCommon.h"
  20.  
  21. using namespace std;
  22.  
  23. class BullPhysSim : public GlutDemoApplication
  24. {
  25.     //keep the collision shapes, for deletion/cleanup
  26.     btAlignedObjectArray<btCollisionShape*> m_collisionShapes;
  27.  
  28.     btBroadphaseInterface * m_broadphase;
  29.     btCollisionDispatcher * m_dispatcher;
  30.     btConstraintSolver    * m_solver;
  31.     btDefaultCollisionConfiguration * m_collisionConfiguration;
  32.  
  33.     btCollisionShape  * geom[6];
  34.     btCollisionObject * fixedGround;
  35.     unsigned long int simtick;     
  36.  
  37. public:
  38.     btRigidBody       * body[6];
  39.     unsigned long int timeStep;
  40.     int threadID;
  41.  
  42. public:
  43.     void initPhysics();
  44.     void exitPhysics();
  45.  
  46.     virtual ~BullPhysSim()
  47.     {
  48.         exitPhysics();
  49.     }
  50.      
  51.     virtual void clientMoveAndDisplay();
  52.     virtual void displayCallback();
  53.     virtual void keyboardCallback(unsigned char key, int x, int y);
  54.    
  55.     static DemoApplication* Create()
  56.     {
  57.         BullPhysSim * demo = new BullPhysSim();
  58.         demo->myinit();
  59.         demo->initPhysics();
  60.         return demo;
  61.     }
  62.  
  63.      virtual void renderme() {       
  64.         extern GLDebugDrawer gDebugDrawer;
  65.           // Call the parent method.
  66.         GlutDemoApplication::renderme();    
  67.     }
  68.  
  69.     void CreateBox(
  70.         int index,
  71.         double x,       double y,       double z,
  72.         double length,  double width,   double height );
  73.    
  74.     void CreateSphere(
  75.         int index,
  76.         double x,       double y,       double z,
  77.         double radius );
  78.  
  79.  
  80.     void DeleteObject( int index );
  81.     btVector3 PointWorldToLocal(int index, btVector3 &p);
  82.     btVector3 AxisWorldToLocal( int index, btVector3 &a);
  83. };
  84.  
  85.  
  86.  
  87.  
  88.  
  89. void BullPhysSim::initPhysics()
  90. {
  91.     simtick = 0;
  92.     timeStep = 0;  
  93.  
  94.     // Setup the basic world
  95.     setTexturing(true);
  96.     setShadows(true);
  97.     setCameraDistance(btScalar(5.));
  98.  
  99.     m_collisionConfiguration = new btDefaultCollisionConfiguration();
  100.     m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
  101.  
  102.     btVector3 worldAabbMin(-10000,-10000,-10000);
  103.     btVector3 worldAabbMax(10000,10000,10000);
  104.     m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax);
  105.  
  106.     m_solver = new btSequentialImpulseConstraintSolver;
  107.     m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
  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.     CreateBox(0,
  124.               0., 2., 0.,
  125.               1., 1., 0.2 );
  126.    
  127.     CreateSphere(1,
  128.                 -3.00 , 2. , 0.0 ,
  129.                 0.27 );    // East kneeball
  130.  
  131.     CreateSphere(2,
  132.                 3.00 , 2. , 0.0 ,
  133.                 0.27 );    // West kneeball
  134.  
  135.     CreateSphere(3,
  136.                 0.0 , 2. , 3.00 ,
  137.                 0.27 );    // North kneeball
  138.  
  139.     CreateSphere(4,
  140.                 0.0 , 2. , -3.00 ,
  141.                 0.27 );    // South kneeball       
  142.    
  143.     clientResetScene();    
  144. }
  145.  
  146.  
  147.  
  148. void BullPhysSim::clientMoveAndDisplay()
  149. {
  150.     double deltaT;
  151.     //.
  152.  
  153.     float ms = getDeltaTimeMicroseconds();
  154.     float minFPS = 90000.f/60.f;
  155.     if (ms > minFPS)
  156.         ms = minFPS;
  157.     deltaT = ms / 90000.f;
  158.  
  159.  
  160.     if (m_dynamicsWorld)
  161.     {                          
  162.         m_dynamicsWorld->stepSimulation( btScalar(deltaT) );       
  163.         timeStep++;
  164.     }
  165. }
  166.  
  167.  
  168. void BullPhysSim
  169. ::exitPhysics()
  170. {
  171.     int i, nocans;
  172.    
  173.     nocans=5;
  174.     i=0;
  175.     while(i<nocans) {
  176.         DeleteObject(i);
  177.         i++;
  178.     }
  179.      
  180.      
  181.     //cleanup in the reverse order of creation/initialization
  182.     //remove the rigidbodies from the dynamics world and delete them
  183.    
  184.     for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
  185.     {
  186.         btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
  187.         btRigidBody* body = btRigidBody::upcast(obj);
  188.         if (body && body->getMotionState())
  189.         {
  190.             delete body->getMotionState();
  191.         }
  192.         m_dynamicsWorld->removeCollisionObject( obj );
  193.         delete obj;
  194.     }
  195.  
  196.      
  197.     //delete collision shapes
  198.     for (int j=0;j<m_collisionShapes.size();j++)
  199.     {
  200.         btCollisionShape* shape = m_collisionShapes[j];
  201.         delete shape;
  202.     }
  203.      
  204.  
  205.     delete m_dynamicsWorld;
  206.     delete m_solver;
  207.     delete m_broadphase;
  208.     delete m_dispatcher;
  209.     delete m_collisionConfiguration;     
  210. }
  211.  
  212.  
  213.  
  214. void BullPhysSim
  215. ::DeleteObject( int index )
  216. {
  217.     m_dynamicsWorld->removeRigidBody( body[index] );
  218.     delete body[index];
  219.     delete geom[index];
  220. }
  221.  
  222.  
  223. void BullPhysSim
  224. ::CreateBox(
  225.         int index,
  226.         double x ,      double y ,      double z ,
  227.         double length , double width , double height)
  228. {
  229.     btCollisionShape * btCSptr;
  230.     btDefaultMotionState * myMotionState;
  231.     btTransform offset;
  232.     btTransform transform;
  233.     btTransform startTransform;
  234.     btVector3 localInertia(0,0,0);
  235.     double mass;
  236.     //.
  237.    
  238.     mass = 1.000;
  239.  
  240.     // `offset` is the location of the entire robot in world.
  241.     offset.setIdentity();
  242.     offset.setOrigin(  btVector3( btScalar(0.) , btScalar(0.) , btScalar(0.))    );
  243.  
  244.     // `transform` is the  local body coordinates of appendages.  
  245.     transform.setIdentity();
  246.     transform.setOrigin(
  247.         btVector3(
  248.             btScalar(x) ,
  249.             btScalar(y) ,
  250.             btScalar(z) )
  251.     );
  252.  
  253.     geom[index] = new btBoxShape(  
  254.             btVector3(
  255.                 btScalar(length) ,
  256.                 btScalar(height) ,
  257.                 btScalar(width ) )
  258.         );
  259.     btCSptr = geom[index];
  260.  
  261.     btCSptr->calculateLocalInertia( btScalar(mass) , localInertia );  
  262.  
  263.     myMotionState = new btDefaultMotionState(offset*transform);
  264.    
  265.     btRigidBody::btRigidBodyConstructionInfo rbInfo(
  266.         btScalar(mass),myMotionState,btCSptr,localInertia
  267.         );
  268.        
  269.     body[index] = new btRigidBody(rbInfo); 
  270.  
  271.     m_dynamicsWorld->addRigidBody( body[index] );
  272. }
  273.  
  274.  
  275.  
  276.  
  277.  
  278. void BullPhysSim
  279. ::CreateSphere(
  280.         int index,
  281.         double x,       double y,       double z,
  282.         double radius )
  283. {
  284.     btCollisionShape * btCSptr;
  285.     btDefaultMotionState * myMotionState;
  286.     btTransform offset;
  287.     btTransform transform;
  288.     btTransform startTransform;
  289.     btVector3 localInertia(0,0,0);
  290.     double mass;
  291.     //.
  292.    
  293.     mass = 0.30;
  294.  
  295.     // `offset` is the location of the entire robot in world.
  296.     offset.setIdentity();
  297.     offset.setOrigin(  btVector3( btScalar(0.) , btScalar(0.) , btScalar(0.))    );
  298.  
  299.     // `transform` is the  local body coordinates of appendages.  
  300.     transform.setIdentity();
  301.     transform.setOrigin(
  302.         btVector3(
  303.             btScalar(x) ,
  304.             btScalar(y) ,
  305.             btScalar(z) )
  306.     );
  307.  
  308.     geom[index] = new btSphereShape(  btScalar(radius)  );
  309.     btCSptr = geom[index];
  310.  
  311.     btCSptr->calculateLocalInertia( btScalar(mass) , localInertia );  
  312.  
  313.     myMotionState = new btDefaultMotionState(offset*transform);
  314.    
  315.     btRigidBody::btRigidBodyConstructionInfo rbInfo(
  316.         btScalar(mass) , myMotionState,btCSptr,localInertia);
  317.        
  318.     body[index] = new btRigidBody(rbInfo);
  319.  
  320.     m_dynamicsWorld->addRigidBody( body[index] );
  321. }
  322.  
  323. btVector3 BullPhysSim
  324. ::PointWorldToLocal(int index, btVector3 &p)
  325. {
  326.   btVector3 btvloc;
  327.   btTransform local1  = body[index]->getCenterOfMassTransform().inverse();  
  328.   //.
  329.   btvloc = (local1)*(p);
  330.   return (btvloc);
  331. }
  332.  
  333. btVector3 BullPhysSim
  334. ::AxisWorldToLocal(int index, btVector3 &a) {
  335.   btTransform local1 = body[index]->getCenterOfMassTransform().inverse();
  336.   btVector3 zero(0,0,0);
  337.   local1.setOrigin(zero);
  338.   return (
  339.       local1 * a
  340.     );
  341. }
  342.  
  343.  
  344. void BullPhysSim::displayCallback()
  345. {
  346.     // .. //
  347. }
  348.  
  349. void BullPhysSim::keyboardCallback(unsigned char key, int x, int y)
  350. {
  351.     switch (key)
  352.     {      
  353.     case 'n':
  354.         {
  355.            
  356.         }
  357.         break;
  358.     default:
  359.         DemoApplication::keyboardCallback(key, x, y);
  360.     }
  361.  
  362.    
  363. }
  364.  
  365.  
  366.  
  367.  
  368.  
  369. long TaskDebug(int passedindex )
  370. {
  371.     BullPhysSim BPS;
  372.     int T ;
  373.     long esc_step;
  374.     bool Life;
  375.     //.    
  376.     T = passedindex;       
  377.  
  378.     BPS.threadID = T;
  379.     BPS.initPhysics();
  380.     BPS.getDynamicsWorld()->setDebugDrawer(&gDebugDrawer);
  381.  
  382.     Life = true;
  383.     while( Life ) {
  384.         if( BPS.timeStep <= 3000 ) {
  385.             BPS.clientMoveAndDisplay();
  386.         } else {
  387.             esc_step = BPS.timeStep;
  388.             Life = false;
  389.         }
  390.     }
  391.  
  392.     return esc_step;
  393. }
  394.  
  395.  
  396.  
  397. // ////////////////////
  398. // MAIN ENTRY POINT
  399. // /////////////////////
  400. int main(int argc,char* argv[])
  401. {    
  402.     int mat = 0;
  403.     long retTS;
  404.  
  405.     while( mat < 12 ) {
  406.         cout << "calling TaskDebug( " ;
  407.         cout << mat;
  408.         cout << endl;
  409.         retTS = TaskDebug(mat);
  410.         cout << "returned from TaskDebug() with timeStep = ";
  411.         cout << retTS;
  412.         cout << endl << endl;
  413.         mat++;
  414.     }
  415.  
  416.     return 1;
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement