Advertisement
Guest User

Untitled

a guest
Aug 11th, 2011
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <btBulletDynamicsCommon.h>
  3.  
  4. int main(int argc, char** argv)
  5. {
  6.   btGeneric6DofConstraint* slider = NULL;
  7.   btCollisionConfiguration* colConf = new btDefaultCollisionConfiguration();
  8.   btCollisionDispatcher* colDis = new btCollisionDispatcher(colConf);
  9.   btBroadphaseInterface* pCache = new btDbvtBroadphase();
  10.   btConstraintSolver* cSolver = new btSequentialImpulseConstraintSolver();
  11.   btDynamicsWorld* world = new btDiscreteDynamicsWorld(colDis, pCache, cSolver, colConf);
  12.  
  13.   world->setGravity(btVector3(0, 0, -9.81));
  14.  
  15.   // Static sphere
  16.   btCollisionShape* staticSh = new btSphereShape(1);
  17.   btScalar staticmass = 0;
  18.   btVector3 staticinertia(0,0,0);
  19.   staticSh->calculateLocalInertia(staticmass, staticinertia);
  20.   btDefaultMotionState* staticMs = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0,0,3)));
  21.   btRigidBody::btRigidBodyConstructionInfo staticBodyCI(staticmass, staticMs, staticSh, staticinertia);
  22.   btRigidBody* staticBody = new btRigidBody(staticBodyCI);
  23.   world->addRigidBody(staticBody);
  24.  
  25.   // Hanging sphere
  26.   btCollisionShape* hangSh = new btSphereShape(1);
  27.   btScalar mass = 1;
  28.   btVector3 inertia(0,0,0);
  29.   hangSh->calculateLocalInertia(mass, inertia);
  30.   btDefaultMotionState* hangMs = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0,0,1)));
  31.   btRigidBody::btRigidBodyConstructionInfo hangBodyCI(mass, hangMs, hangSh, inertia);
  32.   btRigidBody* hangBody = new btRigidBody(hangBodyCI);
  33.   world->addRigidBody(hangBody);
  34.  
  35.   // The slider constraint (made from a 6DOF as on the examples)
  36.   btVector3 sliderWorldPos(0,0,3);
  37.   btVector3 sliderAxis(0,0,1);
  38.   btScalar angle = 0.f;
  39.   btMatrix3x3 sliderOrientation(btQuaternion(sliderAxis ,angle));
  40.   btTransform trans;
  41.   trans.setIdentity();
  42.   trans.setOrigin(sliderWorldPos);
  43.   //trans.setBasis(sliderOrientation); // Already commented in example
  44.   btTransform sliderTransform = trans;
  45.  
  46.   btTransform frameInA, frameInB;
  47.   frameInA = btTransform::getIdentity();
  48.   frameInB = btTransform::getIdentity();
  49.   frameInA.setOrigin(btVector3(0., 0., 5.));
  50.   frameInB.setOrigin(btVector3(0., 0., 5.));
  51.  
  52.   // bool useLinearReferenceFrameA = false;//use fixed frame B for linear llimits
  53.   bool useLinearReferenceFrameA = true;//use fixed frame A for linear llimits
  54.   btGeneric6DofConstraint* spSlider6Dof = new btGeneric6DofConstraint(*staticBody, *hangBody, frameInA, frameInB, useLinearReferenceFrameA);
  55.   spSlider6Dof->setLinearLowerLimit(btVector3(0,0,0.1));
  56.   spSlider6Dof->setLinearUpperLimit(btVector3(0,0,1.5));
  57.  
  58.   //range should be small, otherwise singularities will 'explode' the constraint
  59.   //        spSlider6Dof->setAngularLowerLimit(btVector3(-1.5,0,0));
  60.   //        spSlider6Dof->setAngularUpperLimit(btVector3(1.5,0,0));
  61.   //        spSlider6Dof->setAngularLowerLimit(btVector3(0,0,0));
  62.   //        spSlider6Dof->setAngularUpperLimit(btVector3(0,0,0));
  63.   spSlider6Dof->setAngularLowerLimit(btVector3(-SIMD_PI,0,0));
  64.   spSlider6Dof->setAngularUpperLimit(btVector3(1.5,0,0));
  65.  
  66.   spSlider6Dof->getTranslationalLimitMotor()->m_enableMotor[0] = true;
  67.   spSlider6Dof->getTranslationalLimitMotor()->m_targetVelocity[0] = -5.0f;
  68.   spSlider6Dof->getTranslationalLimitMotor()->m_maxMotorForce[0] = 0.1f;
  69.  
  70.  
  71.   world->addConstraint(spSlider6Dof);
  72.   spSlider6Dof->setDbgDrawSize(btScalar(5.f));
  73.  
  74.   for(int i = 0; i < 300; i++)
  75.   {
  76.     world->stepSimulation(1/60.f, 10);
  77.     btTransform tStatic;
  78.     btTransform tHang;
  79.     staticBody->getMotionState()->getWorldTransform(tStatic);
  80.     hangBody->getMotionState()->getWorldTransform(tHang);
  81.  
  82.     std::cout << "static: " << tStatic.getOrigin().getZ()
  83.           << " hang: " << tHang.getOrigin().getZ() << std::endl;
  84.   }
  85.  
  86.   delete staticSh, hangSh;
  87.   delete staticMs, hangMs;
  88.   delete staticBody, hangBody;
  89.   delete world;
  90.   delete cSolver;
  91.   delete pCache;
  92.   delete colDis;
  93.   delete colConf;
  94.   return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement