Start re-organizing demos so the physics setup can be shared easier (explicit create graphics objects, init/exit physics etc)

Add B3G_RETURN key code, only implemented in Windows so far (todo: Mac, Linux)
Fix Windows key management (use WM_CHAR event instead of WM_KEYUP
Add Return (OnKeyReturn) key support TreeNode, so we can select an item using the return key.
This commit is contained in:
Erwin Coumans
2014-06-24 10:14:06 -07:00
parent 28f19f1bab
commit 68f798a2da
29 changed files with 746 additions and 655 deletions

View File

@@ -6,28 +6,15 @@
#define ARRAY_SIZE_X 5
#define ARRAY_SIZE_Z 5
void BasicDemoPhysicsSetup::initPhysics()
void BasicDemoPhysicsSetup::initPhysics(GraphicsPhysicsBridge& gfxBridge)
{
///collision configuration contains default setup for memory, collision setup
m_collisionConfiguration = new btDefaultCollisionConfiguration();
//m_collisionConfiguration->setConvexConvexMultipointIterations();
createEmptyDynamicsWorld();
///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
m_broadphase = new btDbvtBroadphase();
///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;
m_solver = sol;
m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
m_dynamicsWorld->setGravity(btVector3(0,-10,0));
///create a few basic rigid bodies
btBoxShape* groundShape = createBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
gfxBridge.createCollisionShapeGraphicsObject(groundShape);
//groundShape->initializePolyhedralFeatures();
// btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
@@ -39,7 +26,8 @@ void BasicDemoPhysicsSetup::initPhysics()
{
btScalar mass(0.);
createRigidBody(mass,groundTransform,groundShape, btVector4(0,0,1,1));
btRigidBody* body = createRigidBody(mass,groundTransform,groundShape, btVector4(0,0,1,1));
gfxBridge.createRigidBodyGraphicsObject(body, btVector3(0, 1, 0));
}
@@ -48,6 +36,8 @@ void BasicDemoPhysicsSetup::initPhysics()
// Re-using the same collision is better for memory usage and performance
btBoxShape* colShape = createBoxShape(btVector3(1,1,1));
gfxBridge.createCollisionShapeGraphicsObject(colShape);
//btCollisionShape* colShape = new btSphereShape(btScalar(1.));
m_collisionShapes.push_back(colShape);
@@ -77,7 +67,9 @@ void BasicDemoPhysicsSetup::initPhysics()
btScalar(2.0*j)));
createRigidBody(mass,startTransform,colShape);
btRigidBody* body = createRigidBody(mass,startTransform,colShape);
gfxBridge.createRigidBodyGraphicsObject(body, btVector3(1, 1, 0));
}
}
}
@@ -88,84 +80,10 @@ void BasicDemoPhysicsSetup::initPhysics()
void BasicDemoPhysicsSetup::stepSimulation(float deltaTime)
{
m_dynamicsWorld->stepSimulation(deltaTime);
}
btBoxShape* BasicDemoPhysicsSetup::createBoxShape(const btVector3& halfExtents)
{
btBoxShape* box = new btBoxShape(halfExtents);
return box;
}
btRigidBody* BasicDemoPhysicsSetup::createRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape, const btVector4& color)
{
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//
body->setUserIndex(-1);
m_dynamicsWorld->addRigidBody(body);
return body;
}
void BasicDemoPhysicsSetup::exitPhysics()
{
//cleanup in the reverse order of creation/initialization
//remove the rigidbodies from the dynamics world and delete them
int i;
for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
{
btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
btRigidBody* body = btRigidBody::upcast(obj);
if (body && body->getMotionState())
{
delete body->getMotionState();
}
m_dynamicsWorld->removeCollisionObject( obj );
delete obj;
}
//delete collision shapes
for (int j=0;j<m_collisionShapes.size();j++)
{
btCollisionShape* shape = m_collisionShapes[j];
delete shape;
}
m_collisionShapes.clear();
delete m_dynamicsWorld;
delete m_solver;
delete m_broadphase;
delete m_dispatcher;
delete m_collisionConfiguration;
}