BulletPhysics,RigidBody and soft body Interface <2>
来源:互联网 发布:中山大学停止网络教育 编辑:程序博客网 时间:2024/06/07 03:13
#include "StdAfx.h"#include "PhysicsImpl.h"PhysicsImpl::PhysicsImpl(void){m_pDynamicsWorld = NULL;m_pBroadphase= NULL;m_pDispatcher= NULL;m_pSolver= NULL;m_pCollisionConfiguration= NULL;}PhysicsImpl::~PhysicsImpl(void){ExitPhysics();}float PhysicsImpl::getDeltaTimeMicroseconds(){#ifdef USE_BT_CLOCKbtScalar dt = (btScalar)m_Clock.getTimeMicroseconds();m_Clock.reset();return dt;#elsereturn btScalar(16666.);#endif}btRigidBody* PhysicsImpl::LocalCreateRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape){btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));//rigidbody is dynamic if and only if mass is non zero, otherwise staticbool isDynamic = (mass != 0.f);btVector3 localInertia(0,0,0);if (isDynamic)shape->calculateLocalInertia(mass,localInertia);//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects#define USE_MOTIONSTATE 1#ifdef USE_MOTIONSTATEbtDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);btRigidBody* body = new btRigidBody(cInfo);//body->setContactProcessingThreshold(m_defaultContactProcessingThreshold);#elsebtRigidBody* body = new btRigidBody(mass,0,shape,localInertia);body->setWorldTransform(startTransform);#endifm_pDynamicsWorld->addRigidBody(body);return body;}void PhysicsImpl::DynamicsWorldCreate(void){}void PhysicsImpl::OnPhysicsLoop(){clientMoveAndDisplay();}void PhysicsImpl::clientMoveAndDisplay(){ //simple dynamics world doesn't handle fixed-time-stepping float ms = getDeltaTimeMicroseconds(); ///step the simulation if (m_pDynamicsWorld) { m_pDynamicsWorld->stepSimulation(ms / 1000000.f ); m_softBodyWorldInfo.m_sparsesdf.GarbageCollect();} displayCallback();}void PhysicsImpl::displayCallback(void) { /*glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); renderme(); //optional but useful: debug drawing to detect problems if (m_dynamicsWorld) m_dynamicsWorld->debugDrawWorld(); //绘图刷新 glFlush(); swapBuffers();*/}void PhysicsImpl::InitPhysics(void){///collision configuration contains default setup for memory, collision setup//m_pCollisionConfiguration = new btDefaultCollisionConfiguration();//softrigidbodym_pCollisionConfiguration = new btSoftBodyRigidBodyCollisionConfiguration();//m_collisionConfiguration->setConvexConvexMultipointIterations();///use the default collision dispatcher. For parallel processing you can use //a diffent dispatcher (see Extras/BulletMultiThreaded)m_pDispatcher = newbtCollisionDispatcher(m_pCollisionConfiguration);/*int maxProxies = 1024;btVector3 worldAabbMin(-10000,-10000,-10000);btVector3 worldAabbMax( 10000, 10000, 10000);m_pBroadphase = new bt32BitAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);*/m_pBroadphase = new btDbvtBroadphase();///the default constraint solver. //For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;m_pSolver = sol;m_softBodyWorldInfo.m_dispatcher = m_pDispatcher;m_softBodyWorldInfo.m_broadphase = m_pBroadphase;m_softBodyWorldInfo.m_gravity.setValue(0,-10,0);m_softBodyWorldInfo.m_sparsesdf.Initialize();//m_pDynamicsWorld = new btDiscreteDynamicsWorld(m_pDynamicsWorld = new btSoftRigidDynamicsWorld(m_pDispatcher,m_pBroadphase,m_pSolver,m_pCollisionConfiguration);m_pDynamicsWorld->setGravity(btVector3(0,-10,0));//floor///create a few basic rigid bodiesbtCollisionShape* m_pGroundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));//btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);m_collisionShapes.push_back(m_pGroundShape);btTransform groundTransform;groundTransform.setIdentity();groundTransform.setOrigin(btVector3(0,-50,0));//softm_pDynamicsWorld->getDispatchInfo().m_enableSPU = true;m_pDynamicsWorld->setGravity(btVector3(0,-10,0));DynamicsWorldCreate();}void PhysicsImpl::ExitPhysics(){//cleanup in the reverse order of creation/initialization//remove the rigidbodies from the dynamics world and delete themint i;for (i = m_pDynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--){btCollisionObject* obj = m_pDynamicsWorld->getCollisionObjectArray()[i];btRigidBody* body = btRigidBody::upcast(obj);if (body && body->getMotionState()){delete body->getMotionState();}m_pDynamicsWorld->removeCollisionObject( obj );delete obj;}//delete collision shapesfor (int j=0;j<m_collisionShapes.size();j++){btCollisionShape* shape = m_collisionShapes[j];delete shape;}//delete dynamics worlddelete m_pDynamicsWorld;//delete solverdelete m_pSolver;//delete broadphasedelete m_pBroadphase;//delete dispatcherdelete m_pBroadphase;delete m_pCollisionConfiguration;}