Advertisement
thecplusplusguy

bulletphysics tutorial 2 - adding more shapes

Oct 7th, 2012
1,823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.93 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //bullet physics tutorial 2, adding more shapes
  3. #include <iostream>
  4. #include <SDL/SDL.h>
  5. #include <GL/gl.h>
  6. #include <GL/glu.h>
  7. #include "camera.h"
  8. #include <vector>
  9. #include <bullet/btBulletDynamicsCommon.h>
  10.  
  11. camera cam;
  12. GLUquadricObj* quad;
  13. btDynamicsWorld* world;
  14. btDispatcher* dispatcher;
  15. btCollisionConfiguration* collisionConfig;
  16. btBroadphaseInterface* broadphase;
  17. btConstraintSolver* solver;
  18. std::vector<btRigidBody*> bodies;
  19.  
  20. btRigidBody* addSphere(float rad,float x,float y,float z,float mass)
  21. {
  22.     btTransform t;
  23.     t.setIdentity();
  24.     t.setOrigin(btVector3(x,y,z));
  25.     btSphereShape* sphere=new btSphereShape(rad);
  26.     btVector3 inertia(0,0,0);
  27.     if(mass!=0.0)
  28.         sphere->calculateLocalInertia(mass,inertia);
  29.    
  30.     btMotionState* motion=new btDefaultMotionState(t);
  31.     btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia);
  32.     btRigidBody* body=new btRigidBody(info);
  33.     world->addRigidBody(body);
  34.     bodies.push_back(body);
  35.     return body;
  36. }
  37.  
  38. void renderSphere(btRigidBody* sphere)
  39. {
  40.     if(sphere->getCollisionShape()->getShapeType()!=SPHERE_SHAPE_PROXYTYPE)
  41.         return;
  42.     glColor3f(1,0,0);
  43.     float r=((btSphereShape*)sphere->getCollisionShape())->getRadius();
  44.     btTransform t;
  45.     sphere->getMotionState()->getWorldTransform(t);
  46.     float mat[16];
  47.     t.getOpenGLMatrix(mat);
  48.     glPushMatrix();
  49.         glMultMatrixf(mat); //translation,rotation
  50.         gluSphere(quad,r,20,20);
  51.     glPopMatrix();
  52. }
  53.  
  54. btRigidBody* addCylinder(float d,float h,float x,float y,float z,float mass)
  55. {
  56.     btTransform t;
  57.     t.setIdentity();
  58.     t.setOrigin(btVector3(x,y,z));
  59.     btCylinderShape* sphere=new btCylinderShape(btVector3(d/2.0,h/2.0,d/2.0));
  60.     btVector3 inertia(0,0,0);
  61.     if(mass!=0.0)
  62.         sphere->calculateLocalInertia(mass,inertia);
  63.    
  64.     btMotionState* motion=new btDefaultMotionState(t);
  65.     btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia);
  66.     btRigidBody* body=new btRigidBody(info);
  67.     world->addRigidBody(body);
  68.     bodies.push_back(body);
  69.     return body;
  70. }
  71.  
  72. void renderCylinder(btRigidBody* sphere)
  73. {
  74.     if(sphere->getCollisionShape()->getShapeType()!=CYLINDER_SHAPE_PROXYTYPE)
  75.         return;
  76.     glColor3f(1,0,0);
  77.     btVector3 extent=((btCylinderShape*)sphere->getCollisionShape())->getHalfExtentsWithoutMargin();
  78.     btTransform t;
  79.     sphere->getMotionState()->getWorldTransform(t);
  80.     float mat[16];
  81.     t.getOpenGLMatrix(mat);
  82.     glPushMatrix();
  83.         glMultMatrixf(mat); //translation,rotation
  84.         glTranslatef(0,extent.y(),0);
  85.         glRotatef(90,1,0,0);
  86.         gluCylinder(quad,extent.x(),extent.x(),extent.y()*2.0,20,20);
  87.     glPopMatrix();
  88. }
  89.  
  90. btRigidBody* addCone(float d,float h,float x,float y,float z,float mass)
  91. {
  92.     btTransform t;
  93.     t.setIdentity();
  94.     t.setOrigin(btVector3(x,y,z));
  95.     btConeShape* sphere=new btConeShape(d,h);
  96.     btVector3 inertia(0,0,0);
  97.     if(mass!=0.0)
  98.         sphere->calculateLocalInertia(mass,inertia);
  99.    
  100.     btMotionState* motion=new btDefaultMotionState(t);
  101.     btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia);
  102.     btRigidBody* body=new btRigidBody(info);
  103.     world->addRigidBody(body);
  104.     bodies.push_back(body);
  105.     return body;
  106. }
  107.  
  108. void renderCone(btRigidBody* sphere)
  109. {
  110.     if(sphere->getCollisionShape()->getShapeType()!=CONE_SHAPE_PROXYTYPE)
  111.         return;
  112.     glColor3f(1,0,0);
  113.     float r=((btConeShape*)sphere->getCollisionShape())->getRadius();
  114.     float h=((btConeShape*)sphere->getCollisionShape())->getHeight();
  115.     btTransform t;
  116.     sphere->getMotionState()->getWorldTransform(t);
  117.     float mat[16];
  118.     t.getOpenGLMatrix(mat);
  119.     glPushMatrix();
  120.         glMultMatrixf(mat); //translation,rotation
  121.         glTranslatef(0,h/2.0,0);
  122.         glRotatef(90,1,0,0);
  123.         gluCylinder(quad,0,r,h,20,20);
  124.     glPopMatrix();
  125. }
  126.  
  127. btRigidBody* addBox(float width,float height,float depth,float x,float y,float z,float mass)
  128. {
  129.     btTransform t;
  130.     t.setIdentity();
  131.     t.setOrigin(btVector3(x,y,z));
  132.     btBoxShape* sphere=new btBoxShape(btVector3(width/2.0,height/2.0,depth/2.0));
  133.     btVector3 inertia(0,0,0);
  134.     if(mass!=0.0)
  135.         sphere->calculateLocalInertia(mass,inertia);
  136.    
  137.     btMotionState* motion=new btDefaultMotionState(t);
  138.     btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia);
  139.     btRigidBody* body=new btRigidBody(info);
  140.     world->addRigidBody(body);
  141.     bodies.push_back(body);
  142.     return body;
  143. }
  144.  
  145. void renderBox(btRigidBody* sphere)
  146. {
  147.     if(sphere->getCollisionShape()->getShapeType()!=BOX_SHAPE_PROXYTYPE)
  148.         return;
  149.     glColor3f(1,0,0);
  150.     btVector3 extent=((btBoxShape*)sphere->getCollisionShape())->getHalfExtentsWithoutMargin();
  151.     btTransform t;
  152.     sphere->getMotionState()->getWorldTransform(t);
  153.     float mat[16];
  154.     t.getOpenGLMatrix(mat);
  155.     glPushMatrix();
  156.         glMultMatrixf(mat); //translation,rotation
  157.         glBegin(GL_QUADS);
  158.             glVertex3f(-extent.x(),extent.y(),-extent.z());
  159.             glVertex3f(-extent.x(),-extent.y(),-extent.z());
  160.             glVertex3f(-extent.x(),-extent.y(),extent.z());
  161.             glVertex3f(-extent.x(),extent.y(),extent.z());     
  162.         glEnd();
  163.         glBegin(GL_QUADS);
  164.             glVertex3f(extent.x(),extent.y(),-extent.z());
  165.             glVertex3f(extent.x(),-extent.y(),-extent.z());
  166.             glVertex3f(extent.x(),-extent.y(),extent.z());
  167.             glVertex3f(extent.x(),extent.y(),extent.z());      
  168.         glEnd();
  169.         glBegin(GL_QUADS);
  170.             glVertex3f(-extent.x(),extent.y(),extent.z());
  171.             glVertex3f(-extent.x(),-extent.y(),extent.z());
  172.             glVertex3f(extent.x(),-extent.y(),extent.z());
  173.             glVertex3f(extent.x(),extent.y(),extent.z());      
  174.         glEnd();
  175.         glBegin(GL_QUADS);
  176.             glVertex3f(-extent.x(),extent.y(),-extent.z());
  177.             glVertex3f(-extent.x(),-extent.y(),-extent.z());
  178.             glVertex3f(extent.x(),-extent.y(),-extent.z());
  179.             glVertex3f(extent.x(),extent.y(),-extent.z());     
  180.         glEnd();
  181.         glBegin(GL_QUADS);
  182.             glVertex3f(-extent.x(),extent.y(),-extent.z());
  183.             glVertex3f(-extent.x(),extent.y(),extent.z());
  184.             glVertex3f(extent.x(),extent.y(),extent.z());
  185.             glVertex3f(extent.x(),extent.y(),-extent.z());     
  186.         glEnd();
  187.         glBegin(GL_QUADS);
  188.             glVertex3f(-extent.x(),-extent.y(),-extent.z());
  189.             glVertex3f(-extent.x(),-extent.y(),extent.z());
  190.             glVertex3f(extent.x(),-extent.y(),extent.z());
  191.             glVertex3f(extent.x(),-extent.y(),-extent.z());    
  192.         glEnd();       
  193.     glPopMatrix();
  194. }
  195.  
  196. void renderPlane(btRigidBody* plane)
  197. {
  198.     if(plane->getCollisionShape()->getShapeType()!=STATIC_PLANE_PROXYTYPE)
  199.         return;
  200.     glColor3f(0.8,0.8,0.8);
  201.     btTransform t;
  202.     plane->getMotionState()->getWorldTransform(t);
  203.     float mat[16];
  204.     t.getOpenGLMatrix(mat);
  205.     glPushMatrix();
  206.         glMultMatrixf(mat); //translation,rotation
  207.         glBegin(GL_QUADS);
  208.             glVertex3f(-1000,0,1000);
  209.             glVertex3f(-1000,0,-1000);
  210.             glVertex3f(1000,0,-1000);
  211.             glVertex3f(1000,0,1000);
  212.         glEnd();
  213.     glPopMatrix();
  214. }
  215.  
  216.  
  217. void init(float angle)
  218. {
  219.     quad=gluNewQuadric();
  220.     collisionConfig=new btDefaultCollisionConfiguration();
  221.     dispatcher=new btCollisionDispatcher(collisionConfig);
  222.     broadphase=new btDbvtBroadphase();
  223.     solver=new btSequentialImpulseConstraintSolver();
  224.     world=new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfig);
  225.     world->setGravity(btVector3(0,-10,0));
  226.    
  227.     btTransform t;
  228.     t.setIdentity();
  229.     t.setOrigin(btVector3(0,0,0));
  230.     btStaticPlaneShape* plane=new btStaticPlaneShape(btVector3(0,1,0),0);
  231.     btMotionState* motion=new btDefaultMotionState(t);
  232.     btRigidBody::btRigidBodyConstructionInfo info(0.0,motion,plane);
  233.     btRigidBody* body=new btRigidBody(info);
  234.     world->addRigidBody(body);
  235.     bodies.push_back(body);
  236.    
  237.     addSphere(1.0,0,20,0,1.0);
  238.    
  239.     glClearColor(0,0,0,1);
  240.     glMatrixMode(GL_PROJECTION);
  241.         glLoadIdentity();
  242.         gluPerspective(angle,640.0/480.0,1,1000);
  243.     glMatrixMode(GL_MODELVIEW);
  244.     //initskybox();
  245.     glEnable(GL_DEPTH_TEST);
  246.     cam.setLocation(vector3d(10,10,10));    //the player will be top of the terrain
  247. }
  248.  
  249.  
  250. void display()
  251. {
  252.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  253.     glLoadIdentity();
  254.     cam.Control();
  255.     //drawSkybox(50);
  256.     cam.UpdateCamera();
  257.     for(int i=0;i<bodies.size();i++)
  258.     {
  259.         if(bodies[i]->getCollisionShape()->getShapeType()==STATIC_PLANE_PROXYTYPE)
  260.             renderPlane(bodies[i]);
  261.         else if(bodies[i]->getCollisionShape()->getShapeType()==SPHERE_SHAPE_PROXYTYPE)
  262.             renderSphere(bodies[i]);
  263.         else if(bodies[i]->getCollisionShape()->getShapeType()==CYLINDER_SHAPE_PROXYTYPE)
  264.             renderCylinder(bodies[i]);
  265.         else if(bodies[i]->getCollisionShape()->getShapeType()==CONE_SHAPE_PROXYTYPE)
  266.             renderCone(bodies[i]);
  267.         else if(bodies[i]->getCollisionShape()->getShapeType()==BOX_SHAPE_PROXYTYPE)
  268.             renderBox(bodies[i]);
  269.     }
  270. }
  271.  
  272.  
  273.  
  274. int main()
  275. {
  276.     SDL_Init(SDL_INIT_EVERYTHING);
  277.     SDL_SetVideoMode(640,480,32,SDL_OPENGL);
  278.     Uint32 start;
  279.     SDL_Event event;
  280.     bool running=true;
  281.     float angle=50;
  282.     init(angle);
  283.     addCylinder(2,5,0,30,0,1.0);
  284.     addCone(2,5,5,30,0,1.0);
  285.     addBox(10,2,3,0,40,0,1.0);
  286.     while(running)
  287.     {
  288.         start=SDL_GetTicks();
  289.         while(SDL_PollEvent(&event))
  290.         {
  291.             switch(event.type)
  292.             {
  293.                 case SDL_QUIT:
  294.                     running=false;
  295.                     break;
  296.                 case SDL_KEYDOWN:
  297.                     switch(event.key.keysym.sym)
  298.                     {
  299.                         case SDLK_ESCAPE:
  300.                             running=false;
  301.                             break;
  302.                         case SDLK_y:
  303.                             cam.mouseIn(false);
  304.                             break;
  305.                         case SDLK_SPACE:
  306.                             btRigidBody* sphere=addSphere(1.0,cam.getLocation().x,cam.getLocation().y,cam.getLocation().z,1.0);
  307.                             vector3d look=cam.getVector()*20;
  308.                             sphere->setLinearVelocity(btVector3(look.x,look.y,look.z));
  309.                             break;
  310.                     }
  311.                     break;
  312.                 case SDL_KEYUP:
  313.                     switch(event.key.keysym.sym)
  314.                     {
  315.                    
  316.                     }
  317.                     break;         
  318.                 case SDL_MOUSEBUTTONDOWN:
  319.                     cam.mouseIn(true);
  320.                     break;
  321.                    
  322.             }
  323.         }
  324.         world->stepSimulation(1/60.0);
  325.         display();
  326.         SDL_GL_SwapBuffers();
  327.         if(1000.0/60>SDL_GetTicks()-start)
  328.             SDL_Delay(1000.0/60-(SDL_GetTicks()-start));
  329.     }
  330.     //killskybox();
  331.     for(int i=0;i<bodies.size();i++)
  332.     {
  333.         world->removeCollisionObject(bodies[i]);
  334.         btMotionState* motionState=bodies[i]->getMotionState();
  335.         btCollisionShape* shape=bodies[i]->getCollisionShape();
  336.         delete bodies[i];
  337.         delete shape;
  338.         delete motionState;
  339.     }
  340.     delete dispatcher;
  341.     delete collisionConfig;
  342.     delete solver;
  343.     delete broadphase;
  344.     delete world;
  345.     SDL_Quit();
  346.     gluDeleteQuadric(quad);
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement