#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_CLOCK
btScalar dt = (btScalar)m_Clock.getTimeMicroseconds();
m_Clock.reset();
return dt;
#else
return 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 static
bool 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_MOTIONSTATE
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);
btRigidBody* body = new btRigidBody(cInfo);
//body->setContactProcessingThreshold(m_defaultContactProcessingThreshold);
#else
btRigidBody* body = new btRigidBody(mass,0,shape,localInertia);
body->setWorldTransform(startTransform);
#endif
m_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();
//softrigidbody
m_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 = new btCollisionDispatcher(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 bodies
btCollisionShape* 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));
//soft
m_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 them
int 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 shapes
for (int j=0;j<m_collisionShapes.size();j++)
{
btCollisionShape* shape = m_collisionShapes[j];
delete shape;
}
//delete dynamics world
delete m_pDynamicsWorld;
//delete solver
delete m_pSolver;
//delete broadphase
delete m_pBroadphase;
//delete dispatcher
delete m_pBroadphase;
delete m_pCollisionConfiguration;
}
BulletPhysics,RigidBody and soft body Interface
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.