Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.89 KB | None | 0 0
  1. /*
  2. 29/Sep/2010 Umer Noor
  3. Edited original BasicDemo file
  4. Starting point for using Bullet's debugDrawer.
  5. For now we are not using Bullet for physics.
  6. */
  7.  
  8. /*
  9. Bullet Continuous Collision Detection and Physics Library
  10. Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
  11.  
  12. This software is provided 'as-is', without any express or implied warranty.
  13. In no event will the authors be held liable for any damages arising from the use of this software.
  14. Permission is granted to anyone to use this software for any purpose,
  15. including commercial applications, and to alter it and redistribute it freely,
  16. subject to the following restrictions:
  17.  
  18. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  19. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  20. 3. This notice may not be removed or altered from any source distribution.
  21. */
  22.  
  23.  
  24.  
  25. #include "BasicDemo.h"
  26. #include "GlutStuff.h"
  27. ///btBulletDynamicsCommon.h is the main Bullet include file, contains most common include files.
  28. #include "btBulletDynamicsCommon.h"
  29. #include <stdio.h> //printf debugging
  30. #include <iostream>
  31.  
  32. float fRadius = 1.0;
  33. const float fCoefficientOfRestitution = 1.0;
  34. const float fMass = 0.5;
  35. bool isColliding = false;
  36.  
  37. btVector3 rotationalVec;
  38. btVector3 angVel;
  39. btVector3 upVec = btVector3(0.0, 1.0, 0.0);
  40. btVector3 Fdrag = btVector3(0.0,0.0,0.0);
  41.  
  42. btMatrix3x3 inertiaTensor = btMatrix3x3();
  43.  
  44. void BasicDemo::clientMoveAndDisplay()
  45. {
  46.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  47.  
  48.     //Enter physic code
  49.     float fTimeStep = 1.0 / 100.0;
  50.     btTransform startTransform;
  51.     startTransform.setIdentity();
  52.  
  53.     inertiaTensor.setValue(btScalar((2.0f / 5.0f) * fMass * (fRadius * fRadius)), 0, 0, 0, btScalar((2.0f / 5.0f) * fMass * (fRadius * fRadius)), 0, 0, 0, btScalar((2.0f / 5.0f) * fMass * (fRadius * fRadius)));
  54.  
  55.     inertiaTensor.inverse();
  56.  
  57.     btVector3 distance = vPosition[0] - vPosition[1];
  58.  
  59.     for (int ii = 0; ii < 2; ii++)
  60.     {
  61.         //Collision Detection
  62.         //Calculate Distance between both vectors
  63.  
  64.         if (distance.length() < fRadius * 2) {
  65.             isColliding = true;
  66.             std::cout << "Colliding!" << std::endl;
  67.         }
  68.         else {
  69.             isColliding = false;
  70.             std::cout << "Not colliding!" << std::endl;
  71.         }
  72.  
  73.         if (vVelocity[ii] != btVector3(0, 0, 0)) {
  74.             rotationalVec = upVec.cross(vVelocity[ii]);
  75.         }
  76.         else {
  77.             rotationalVec = btVector3(0.0, 1.0, 0.0);
  78.         }
  79.        
  80.         if (isColliding)
  81.         {
  82.             //Normalize
  83.             distance.safeNormalize();
  84.  
  85.             btVector3 collnorm = vPosition[1] - (distance * fRadius);
  86.  
  87.             float vRel = collnorm.dot(vVelocity[0] - vVelocity[1]);
  88.  
  89.             float numerator = -vRel * (fCoefficientOfRestitution + 1.0f);
  90.  
  91.             //Point of collision
  92.             btVector3 r1 = collnorm - vPosition[0];
  93.             btVector3 r2 = collnorm - vPosition[1];
  94.            
  95.             btVector3 part1 = r1.cross(collnorm);
  96.             part1 = inertiaTensor * part1;
  97.             part1.cross(collnorm);
  98.  
  99.             float part1final = collnorm.dot(part1);
  100.            
  101.             btVector3 part2 = r2.cross(collnorm);
  102.             part2 = inertiaTensor * part2;
  103.             part2.cross(collnorm);
  104.  
  105.             float part2final = collnorm.dot(part2);
  106.  
  107.             float denominator = (1.0f / fMass) + (1.0f / fMass) + part1final + part2final;
  108.  
  109.             float impluse = numerator / denominator;
  110.  
  111.             //float impluse = (-vRel * (fCoefficientOfRestitution + 1)) / ((1.0f / fMass) + (1.0f / fMass) + collnorm.dot((inertiaTensor * (r1.cross(collnorm))).cross(r1)) + collnorm.dot((inertiaTensor * (r2.cross(collnorm))).cross(r2)));
  112.  
  113.             //Quiz 1
  114.             //vVelocity[ii] = vVelocityInit[ii] - 2 * (vVelocityInit[ii].dot(distance) * distance);
  115.  
  116.             vVelocity[0] = vVelocity[0] + (impluse * collnorm) / fMass;
  117.             vVelocity[1] = vVelocity[1] + (-impluse * collnorm) / fMass;
  118.  
  119.         }
  120.  
  121.  
  122.         //Quiz 1
  123.         /*Fdrag = -0.3f * vVelocity[ii];
  124.         vAcceleration[ii] = Fdrag;*/
  125.         vVelocityInit[ii] = vVelocity[ii];
  126.  
  127.         // Update angular velocity
  128.         rotationalVec.safeNormalize();
  129.         float angVelMag = vVelocity[ii].getMagnitude() / fTimeStep;
  130.         angVel = angVelMag * rotationalVec;
  131.  
  132.         // Update orientation quaternion
  133.         qOrientationInit[ii] = qOrientation[ii];
  134.         qOrientation[ii] = qOrientationInit[ii] + (fTimeStep / 2) * angVel * qOrientationInit[ii] * fTimeStep;
  135.  
  136.         // Update position (ignore friction)
  137.         vPosition[ii] += vVelocity[ii] * fTimeStep + 0.5f * vAcceleration[ii] * (fTimeStep * fTimeStep);
  138.         vVelocity[ii] += vAcceleration[ii] * fTimeStep;
  139.  
  140.         // Input data into motionstate for each ball
  141.         startTransform.setOrigin(vPosition[ii]);
  142.         startTransform.setRotation(qOrientation[ii]);
  143.         m_rigidBody[ii]->getMotionState()->setWorldTransform(startTransform);
  144.  
  145.     }
  146.  
  147.  
  148.     // OpenGL calls
  149.     m_dynamicsWorld->debugDrawWorld();
  150.     renderme();
  151.     glFlush();
  152.     glutSwapBuffers();
  153.  
  154. }
  155.  
  156.  
  157.  
  158. void BasicDemo::displayCallback(void) {
  159.  
  160.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  161.    
  162.     renderme();
  163.  
  164.     //optional but useful: debug drawing to detect problems
  165.     if (m_dynamicsWorld)
  166.         m_dynamicsWorld->debugDrawWorld();
  167.  
  168.     glFlush();
  169.     glutSwapBuffers();
  170. }
  171.  
  172.  
  173.  
  174.  
  175.  
  176. void BasicDemo::initPhysics()
  177. {
  178.     setTexturing(true);
  179.     setShadows(true);
  180.  
  181.     setCameraDistance(btScalar(10.0));
  182.  
  183.     ///collision configuration contains default setup for memory, collision setup
  184.     m_collisionConfiguration = new btDefaultCollisionConfiguration();
  185.  
  186.     ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
  187.     m_dispatcher = new  btCollisionDispatcher(m_collisionConfiguration);
  188.  
  189.     m_broadphase = new btDbvtBroadphase();
  190.  
  191.     ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
  192.     btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;
  193.     m_solver = sol;
  194.  
  195.     m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
  196.  
  197.     m_dynamicsWorld->setGravity(btVector3(0,-10,0));
  198.  
  199.     ///create a few basic rigid bodies
  200.     btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
  201.    
  202.     m_collisionShapes.push_back(groundShape);
  203.  
  204.     btTransform groundTransform;
  205.     groundTransform.setIdentity();
  206.     groundTransform.setOrigin(btVector3(0,-50,0));
  207.  
  208.     //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
  209.     {
  210.         btScalar mass(0.);
  211.  
  212.         //rigidbody is dynamic if and only if mass is non zero, otherwise static
  213.         bool isDynamic = (mass != 0.f);
  214.  
  215.         btVector3 localInertia(0,0,0);
  216.         if (isDynamic)
  217.             groundShape->calculateLocalInertia(mass,localInertia);
  218.  
  219.         //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  220.         btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
  221.         btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
  222.         btRigidBody* body = new btRigidBody(rbInfo);
  223.         body->setRestitution(btScalar(0.8f));  
  224.         //add the body to the dynamics world
  225.         m_dynamicsWorld->addRigidBody(body);
  226.     }
  227.  
  228.     // Here are the dynamic bodies
  229.     {
  230.         ///btCollisionShape* colShape = new btBoxShape(btVector3(1, 1, 1));    
  231.         btCollisionShape* colShape = new btSphereShape(1.0);
  232.  
  233.         m_collisionShapes.push_back(colShape);
  234.  
  235.         /// Create Dynamic Objects
  236.         btTransform startTransform;
  237.         startTransform.setIdentity();
  238.  
  239.         btScalar mass(fMass);
  240.  
  241.         //rigidbody is dynamic if and only if mass is non zero, otherwise static
  242.         bool isDynamic = (mass != 0.f);
  243.  
  244.         btVector3 localInertia(0,0,0);
  245.         if (isDynamic)
  246.             colShape->calculateLocalInertia(mass,localInertia);
  247.  
  248.         float start_x[] = { 0.0, 1.4 }; // collision angle
  249.         float start_y[] = { 1.0, 1.0 };
  250.         float start_z[] = { -4.0, 4.0 };
  251.  
  252.         // set up 2 rigid bodies and put them into the btAlignedObjectArray called m_rigidBody
  253.         for(int ii = 0; ii < 2; ii++){
  254.             startTransform.setOrigin(btVector3( start_x[ii], start_y[ii], start_z[ii] ));          
  255.             //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  256.             btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
  257.             btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia); 
  258.             btRigidBody* ball = new btRigidBody(rbInfo);
  259.             ball->setActivationState(ISLAND_SLEEPING);
  260.             ball->setRestitution(fCoefficientOfRestitution);   
  261.             m_dynamicsWorld->addRigidBody(ball);
  262.             ball->setActivationState(ISLAND_SLEEPING);
  263.             m_rigidBody.push_back(ball);
  264.    
  265.         }
  266.  
  267.         float start_velx[] = { 0.0, 0.0 };
  268.         float start_vely[] = { 0.0, 0.0 };
  269.         float start_velz[] = { 3.0, 0.0 };
  270.  
  271.         for (int ii = 0; ii < 2; ii++) {
  272.             qOrientation[ii].setValue(0.0, 0.0, 0.0, 1.0);
  273.             vAngularVelocity[ii].setValue(0.0, 0.0, 0.0);
  274.             vVelocity[ii].setValue(start_velx[ii], start_vely[ii], start_velz[ii]);
  275.             vPosition[ii].setValue(start_x[ii], start_y[ii], start_z[ii]);
  276.             vAcceleration[ii].setValue(0.0, 0.0, 0.0);
  277.             vTorque[ii].setValue(0.0, 0.0, 0.0);
  278.         }
  279.  
  280.     }
  281.  
  282.     clientResetScene();
  283. }
  284.    
  285.  
  286. void BasicDemo::exitPhysics()
  287. {
  288.  
  289.     //cleanup in the reverse order of creation/initialization
  290.  
  291.     //remove the rigidbodies from the dynamics world and delete them
  292.     int i;
  293.     for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
  294.     {
  295.         btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
  296.         btRigidBody* body = btRigidBody::upcast(obj);
  297.         if (body && body->getMotionState())
  298.         {
  299.             delete body->getMotionState();
  300.         }
  301.         m_dynamicsWorld->removeCollisionObject( obj );
  302.         delete obj;
  303.     }
  304.  
  305.     //delete collision shapes
  306.     for (int j=0;j<m_collisionShapes.size();j++)
  307.     {
  308.         btCollisionShape* shape = m_collisionShapes[j];
  309.         delete shape;
  310.     }
  311.  
  312.     delete m_dynamicsWorld;
  313.    
  314.     delete m_solver;
  315.    
  316.     delete m_broadphase;
  317.    
  318.     delete m_dispatcher;
  319.  
  320.     delete m_collisionConfiguration;
  321.  
  322.    
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement