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

@@ -25,12 +25,17 @@ IF (WIN32)
ADD_EXECUTABLE(AppCcdPhysicsDemo
main.cpp
CcdPhysicsDemo.cpp
CcdPhysicsSetup.h
CcdPhysicsSetup.cpp
${BULLET_PHYSICS_SOURCE_DIR}/build3/bullet.rc
)
ELSE()
ADD_EXECUTABLE(AppCcdPhysicsDemo
main.cpp
CcdPhysicsDemo.cpp
CcdPhysicsSetup.h
CcdPhysicsSetup.cpp
)
ENDIF()

View File

@@ -167,26 +167,11 @@ void CcdPhysicsDemo::initPhysics()
m_ShootBoxInitialSpeed = 4000.f;
m_defaultContactProcessingThreshold = 0.f;
///collision configuration contains default setup for memory, collision setup
m_collisionConfiguration = new btDefaultCollisionConfiguration();
// m_collisionConfiguration->setConvexConvexMultipointIterations();
///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
//m_dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,BOX_SHAPE_PROXYTYPE,m_collisionConfiguration->getCollisionAlgorithmCreateFunc(CONVEX_SHAPE_PROXYTYPE,CONVEX_SHAPE_PROXYTYPE));
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);
GraphicsPhysicsBridge bridge;
m_physicsSetup.initPhysics(bridge);
m_dynamicsWorld = m_physicsSetup.m_dynamicsWorld;
m_dynamicsWorld->getSolverInfo().m_solverMode |=SOLVER_USE_2_FRICTION_DIRECTIONS|SOLVER_RANDMIZE_ORDER;
m_dynamicsWorld ->setDebugDrawer(&sDebugDrawer);
//m_dynamicsWorld->getSolverInfo().m_splitImpulse=false;
@@ -203,108 +188,6 @@ void CcdPhysicsDemo::initPhysics()
m_dynamicsWorld->setGravity(btVector3(0,-10,0));
///create a few basic rigid bodies
btBoxShape* box = new btBoxShape(btVector3(btScalar(110.),btScalar(1.),btScalar(110.)));
// box->initializePolyhedralFeatures();
btCollisionShape* groundShape = box;
// btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
m_collisionShapes.push_back(groundShape);
//m_collisionShapes.push_back(new btCylinderShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)));
m_collisionShapes.push_back(new btBoxShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)));
btTransform groundTransform;
groundTransform.setIdentity();
//groundTransform.setOrigin(btVector3(5,5,5));
//We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
{
btScalar mass(0.);
//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)
groundShape->calculateLocalInertia(mass,localInertia);
//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
body->setFriction(0.5);
//body->setRollingFriction(0.3);
//add the body to the dynamics world
m_dynamicsWorld->addRigidBody(body);
}
{
//create a few dynamic rigidbodies
// Re-using the same collision is better for memory usage and performance
btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
//btCollisionShape* colShape = new btSphereShape(btScalar(1.));
m_collisionShapes.push_back(colShape);
/// Create Dynamic Objects
btTransform startTransform;
startTransform.setIdentity();
btScalar mass(1.f);
//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)
colShape->calculateLocalInertia(mass,localInertia);
int gNumObjects = 120;//120;
int i;
for (i=0;i<gNumObjects;i++)
{
btCollisionShape* shape = m_collisionShapes[1];
btTransform trans;
trans.setIdentity();
//stack them
int colsize = 10;
int row = (i*CUBE_HALF_EXTENTS*2)/(colsize*2*CUBE_HALF_EXTENTS);
int row2 = row;
int col = (i)%(colsize)-colsize/2;
if (col>3)
{
col=11;
row2 |=1;
}
btVector3 pos(col*2*CUBE_HALF_EXTENTS + (row2%2)*CUBE_HALF_EXTENTS,
row*2*CUBE_HALF_EXTENTS+CUBE_HALF_EXTENTS+EXTRA_HEIGHT,0);
trans.setOrigin(pos);
float mass = 1.f;
btRigidBody* body = localCreateRigidBody(mass,trans,shape);
body->setAnisotropicFriction(shape->getAnisotropicRollingFrictionDirection(),btCollisionObject::CF_ANISOTROPIC_ROLLING_FRICTION);
body->setFriction(0.5);
//body->setRollingFriction(.3);
///when using m_ccdMode
if (m_ccdMode==USE_CCD)
{
body->setCcdMotionThreshold(CUBE_HALF_EXTENTS);
body->setCcdSweptSphereRadius(0.9*CUBE_HALF_EXTENTS);
}
}
}
}
void CcdPhysicsDemo::clientResetScene()
@@ -381,40 +264,8 @@ void CcdPhysicsDemo::shootBox(const btVector3& destination)
void CcdPhysicsDemo::exitPhysics()
{
m_physicsSetup.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;
}

View File

@@ -33,19 +33,14 @@ class btConstraintSolver;
struct btCollisionAlgorithmCreateFunc;
class btDefaultCollisionConfiguration;
#include "CcdPhysicsSetup.h"
///CcdPhysicsDemo is good starting point for learning the code base and porting.
class CcdPhysicsDemo : public PlatformDemoApplication
{
//keep the collision shapes, for deletion/cleanup
btAlignedObjectArray<btCollisionShape*> m_collisionShapes;
btBroadphaseInterface* m_broadphase;
btCollisionDispatcher* m_dispatcher;
btConstraintSolver* m_solver;
CcdPhysicsSetup m_physicsSetup;
enum
{
@@ -54,7 +49,6 @@ class CcdPhysicsDemo : public PlatformDemoApplication
};
int m_ccdMode;
btDefaultCollisionConfiguration* m_collisionConfiguration;
public:

View File

@@ -0,0 +1,179 @@
#include "CcdPhysicsSetup.h"
#include "btBulletDynamicsCommon.h"
#define CUBE_HALF_EXTENTS 1.f
#define EXTRA_HEIGHT 1.f
void KinematicObjectSetup::initPhysics(GraphicsPhysicsBridge& gfxBridge)
{
createEmptyDynamicsWorld();
{
btBoxShape* box = new btBoxShape(btVector3(btScalar(10.), btScalar(1.), btScalar(10.)));
gfxBridge.createCollisionShapeGraphicsObject(box);
btTransform startTrans;
startTrans.setIdentity();
startTrans.setOrigin(btVector3(0, -1, 0));
btRigidBody* body = createRigidBody(0, startTrans, box);
body->setMotionState(0);
body->setFriction(1);
body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
body->setActivationState(DISABLE_DEACTIVATION);
gfxBridge.createRigidBodyGraphicsObject(body, btVector3(0,1,0));
}
{
btBoxShape* box = new btBoxShape(btVector3(btScalar(1.), btScalar(1.), btScalar(1.)));
gfxBridge.createCollisionShapeGraphicsObject(box);
btTransform startTrans;
startTrans.setIdentity();
startTrans.setOrigin(btVector3(0, 1, 0));
btRigidBody* body = createRigidBody(1, startTrans, box);
body->setFriction(1);
body->setActivationState(DISABLE_DEACTIVATION);
gfxBridge.createRigidBodyGraphicsObject(body, btVector3(1, 1, 0));
}
}
void KinematicObjectSetup::stepSimulation(float deltaTime)
{
if (m_dynamicsWorld)
{
btCollisionObject* colObj = m_dynamicsWorld->getCollisionObjectArray()[0];
btRigidBody* body = btRigidBody::upcast(colObj);
if (body)
{
btMotionState* ms = body->getMotionState();
btTransform startTrans;
startTrans.setIdentity();
static float time = 0.f;
time += 0.01f;
static float xPos = 0.f;
xPos = sinf(time)*10.f;
startTrans.setOrigin(btVector3(xPos, -1, 0));
if (ms)
{
ms->setWorldTransform(startTrans);
}
else
{
body->setWorldTransform(startTrans);
}
}
m_dynamicsWorld->stepSimulation(deltaTime);
}
}
void CcdPhysicsSetup::initPhysics(GraphicsPhysicsBridge& gfxBridge)
{
createEmptyDynamicsWorld();
///create a few basic rigid bodies
btBoxShape* box = new btBoxShape(btVector3(btScalar(110.), btScalar(1.), btScalar(110.)));
gfxBridge.createCollisionShapeGraphicsObject(box);
// box->initializePolyhedralFeatures();
btCollisionShape* groundShape = box;
m_collisionShapes.push_back(groundShape);
//m_collisionShapes.push_back(new btCylinderShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)));
m_collisionShapes.push_back(new btBoxShape(btVector3(CUBE_HALF_EXTENTS, CUBE_HALF_EXTENTS, CUBE_HALF_EXTENTS)));
btTransform groundTransform;
groundTransform.setIdentity();
//groundTransform.setOrigin(btVector3(5,5,5));
//We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
{
btScalar mass(0.);
//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)
groundShape->calculateLocalInertia(mass, localInertia);
//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, groundShape, localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
gfxBridge.createRigidBodyGraphicsObject(body, btVector3(0, 1, 0));
body->setFriction(0.5);
//body->setRollingFriction(0.3);
//add the body to the dynamics world
m_dynamicsWorld->addRigidBody(body);
}
{
//create a few dynamic rigidbodies
// Re-using the same collision is better for memory usage and performance
btCollisionShape* colShape = new btBoxShape(btVector3(1, 1, 1));
gfxBridge.createCollisionShapeGraphicsObject(colShape);
//btCollisionShape* colShape = new btSphereShape(btScalar(1.));
m_collisionShapes.push_back(colShape);
/// Create Dynamic Objects
btTransform startTransform;
startTransform.setIdentity();
btScalar mass(1.f);
//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)
colShape->calculateLocalInertia(mass, localInertia);
int gNumObjects = 120;//120;
int i;
for (i = 0; i<gNumObjects; i++)
{
btCollisionShape* shape = colShape;// m_collisionShapes[1];
btTransform trans;
trans.setIdentity();
//stack them
int colsize = 10;
int row = (i*CUBE_HALF_EXTENTS * 2) / (colsize * 2 * CUBE_HALF_EXTENTS);
int row2 = row;
int col = (i) % (colsize)-colsize / 2;
if (col>3)
{
col = 11;
row2 |= 1;
}
btVector3 pos(col * 2 * CUBE_HALF_EXTENTS + (row2 % 2)*CUBE_HALF_EXTENTS,
row * 2 * CUBE_HALF_EXTENTS + CUBE_HALF_EXTENTS + EXTRA_HEIGHT, 0);
trans.setOrigin(pos);
float mass = 1.f;
btRigidBody* body = createRigidBody(mass, trans, shape);
gfxBridge.createRigidBodyGraphicsObject(body, btVector3(1, 1, 0));
body->setAnisotropicFriction(shape->getAnisotropicRollingFrictionDirection(), btCollisionObject::CF_ANISOTROPIC_ROLLING_FRICTION);
body->setFriction(0.5);
//body->setRollingFriction(.3);
///when using m_ccdMode
//if (m_ccdMode == USE_CCD)
{
body->setCcdMotionThreshold(CUBE_HALF_EXTENTS);
body->setCcdSweptSphereRadius(0.9*CUBE_HALF_EXTENTS);
}
}
}
}

View File

@@ -0,0 +1,23 @@
#ifndef CCD_PHYSICS_SETUP_H
#define CCD_PHYSICS_SETUP_H
#include "../CommonRigidBodySetup.h"
struct CcdPhysicsSetup : public CommonRigidBodySetup
{
virtual void initPhysics(GraphicsPhysicsBridge& gfxBridge);
};
struct KinematicObjectSetup : public CommonRigidBodySetup
{
virtual void initPhysics(GraphicsPhysicsBridge& gfxBridge);
virtual void stepSimulation(float deltaTime);
};
#endif //CCD_PHYSICS_SETUP_H