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:
@@ -14,8 +14,8 @@
|
||||
|
||||
static const float scaling=0.35f;
|
||||
|
||||
BasicDemo::BasicDemo(SimpleOpenGL3App* app)
|
||||
:Bullet2RigidBodyDemo(app)
|
||||
BasicDemo::BasicDemo(SimpleOpenGL3App* app, CommonPhysicsSetup* physicsSetup)
|
||||
:Bullet2RigidBodyDemo(app,physicsSetup)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -25,124 +25,6 @@ BasicDemo::~BasicDemo()
|
||||
|
||||
void BasicDemo::createGround(int cubeShapeId)
|
||||
{
|
||||
{
|
||||
btVector4 color(0.3,0.3,1,1);
|
||||
btVector4 halfExtents(50,50,50,1);
|
||||
btTransform groundTransform;
|
||||
groundTransform.setIdentity();
|
||||
groundTransform.setOrigin(btVector3(0,-50,0));
|
||||
m_glApp->m_instancingRenderer->registerGraphicsInstance(cubeShapeId,groundTransform.getOrigin(),groundTransform.getRotation(),color,halfExtents);
|
||||
|
||||
btBoxShape* groundShape = new btBoxShape(btVector3(btScalar(halfExtents[0]),btScalar(halfExtents[1]),btScalar(halfExtents[2])));
|
||||
//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);
|
||||
//add the body to the dynamics world
|
||||
m_dynamicsWorld->addRigidBody(body);
|
||||
}
|
||||
}
|
||||
}
|
||||
void BasicDemo::initPhysics()
|
||||
{
|
||||
m_physicsSetup.m_glApp = m_glApp;
|
||||
m_physicsSetup.initPhysics();
|
||||
m_dynamicsWorld = m_physicsSetup.m_dynamicsWorld;
|
||||
|
||||
m_glApp->m_instancingRenderer->writeTransforms();
|
||||
}
|
||||
void BasicDemo::exitPhysics()
|
||||
{
|
||||
m_physicsSetup.exitPhysics();
|
||||
m_dynamicsWorld = 0;
|
||||
//Bullet2RigidBodyDemo::exitPhysics();
|
||||
}
|
||||
|
||||
//SimpleOpenGL3App* m_glApp;
|
||||
|
||||
btRigidBody* MyBasicDemoPhysicsSetup::createRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape, const btVector4& color)
|
||||
{
|
||||
btRigidBody* body = BasicDemoPhysicsSetup::createRigidBody(mass,startTransform,shape);
|
||||
int graphicsShapeId = shape->getUserIndex();
|
||||
btAssert(graphicsShapeId>=0);
|
||||
btVector3 localScaling = shape->getLocalScaling();
|
||||
|
||||
int graphicsInstanceId = m_glApp->m_instancingRenderer->registerGraphicsInstance(graphicsShapeId,startTransform.getOrigin(),startTransform.getRotation(),color,localScaling);
|
||||
body->setUserIndex(graphicsInstanceId);
|
||||
|
||||
//todo: create graphics representation
|
||||
return body;
|
||||
|
||||
}
|
||||
|
||||
btBoxShape* MyBasicDemoPhysicsSetup::createBoxShape(const btVector3& halfExtents)
|
||||
{
|
||||
btBoxShape* box = BasicDemoPhysicsSetup::createBoxShape(halfExtents);
|
||||
int cubeShapeId = m_glApp->registerCubeShape(halfExtents.x(),halfExtents.y(),halfExtents.z());
|
||||
box->setUserIndex(cubeShapeId);
|
||||
//todo: create graphics representation
|
||||
return box;
|
||||
}
|
||||
|
||||
|
||||
void BasicDemo::renderScene()
|
||||
{
|
||||
//sync graphics -> physics world transforms
|
||||
{
|
||||
for (int i=0;i<m_dynamicsWorld->getNumCollisionObjects();i++)
|
||||
{
|
||||
btCollisionObject* colObj = m_dynamicsWorld->getCollisionObjectArray()[i];
|
||||
btVector3 pos = colObj->getWorldTransform().getOrigin();
|
||||
btQuaternion orn = colObj->getWorldTransform().getRotation();
|
||||
int index = colObj ->getUserIndex();
|
||||
if (index>=0)
|
||||
{
|
||||
m_glApp->m_instancingRenderer->writeSingleInstanceTransformToCPU(pos,orn,index);
|
||||
}
|
||||
}
|
||||
m_glApp->m_instancingRenderer->writeTransforms();
|
||||
}
|
||||
|
||||
m_glApp->m_instancingRenderer->renderScene();
|
||||
}
|
||||
|
||||
|
||||
void BasicDemo::stepSimulation(float dt)
|
||||
{
|
||||
m_physicsSetup.stepSimulation(dt);
|
||||
m_physicsSetup.m_dynamicsWorld->debugDrawWorld();
|
||||
|
||||
|
||||
|
||||
/*
|
||||
//print applied force
|
||||
//contact points
|
||||
for (int i=0;i<m_dynamicsWorld->getDispatcher()->getNumManifolds();i++)
|
||||
{
|
||||
btPersistentManifold* contact = m_dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i);
|
||||
for (int c=0;c<contact->getNumContacts();c++)
|
||||
{
|
||||
btManifoldPoint& pt = contact->getContactPoint(c);
|
||||
btScalar dist = pt.getDistance();
|
||||
if (dist< contact->getContactProcessingThreshold())
|
||||
{
|
||||
printf("normalImpulse[%d.%d] = %f\n",i,c,pt.m_appliedImpulse);
|
||||
|
||||
} else
|
||||
{
|
||||
printf("?\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,36 +7,26 @@
|
||||
#include "../../../Demos/BasicDemo/BasicDemoPhysicsSetup.h"
|
||||
|
||||
|
||||
struct MyBasicDemoPhysicsSetup : public BasicDemoPhysicsSetup
|
||||
{
|
||||
SimpleOpenGL3App* m_glApp;
|
||||
|
||||
virtual btRigidBody* createRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape, const btVector4& color);
|
||||
|
||||
virtual btBoxShape* createBoxShape(const btVector3& halfExtents);
|
||||
};
|
||||
|
||||
class BasicDemo : public Bullet2RigidBodyDemo
|
||||
{
|
||||
|
||||
MyBasicDemoPhysicsSetup m_physicsSetup;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
static BulletDemoInterface* MyCreateFunc(SimpleOpenGL3App* app)
|
||||
{
|
||||
return new BasicDemo(app);
|
||||
CommonPhysicsSetup* physicsSetup = new BasicDemoPhysicsSetup();
|
||||
return new BasicDemo(app, physicsSetup);
|
||||
}
|
||||
|
||||
BasicDemo(SimpleOpenGL3App* app);
|
||||
BasicDemo(SimpleOpenGL3App* app, CommonPhysicsSetup* physicsSetup);
|
||||
virtual ~BasicDemo();
|
||||
|
||||
void createGround(int cubeShapeId);
|
||||
|
||||
virtual void initPhysics();
|
||||
virtual void exitPhysics();
|
||||
virtual void renderScene();
|
||||
virtual void stepSimulation(float dt);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -2,54 +2,102 @@
|
||||
#include "btBulletDynamicsCommon.h"
|
||||
#include "OpenGLWindow/SimpleOpenGL3App.h"
|
||||
|
||||
Bullet2RigidBodyDemo::Bullet2RigidBodyDemo(SimpleOpenGL3App* app)
|
||||
:m_glApp(app),
|
||||
m_pickedBody(0),
|
||||
m_pickedConstraint(0),
|
||||
m_controlPressed(false),
|
||||
m_altPressed(false)
|
||||
struct MyGraphicsPhysicsBridge : public GraphicsPhysicsBridge
|
||||
{
|
||||
m_config = 0;
|
||||
m_dispatcher = 0;
|
||||
m_bp = 0;
|
||||
m_solver = 0;
|
||||
m_dynamicsWorld = 0;
|
||||
SimpleOpenGL3App* m_glApp;
|
||||
|
||||
MyGraphicsPhysicsBridge(SimpleOpenGL3App* glApp)
|
||||
:m_glApp(glApp)
|
||||
{
|
||||
}
|
||||
virtual void createRigidBodyGraphicsObject(btRigidBody* body, const btVector3& color)
|
||||
{
|
||||
btCollisionShape* shape = body->getCollisionShape();
|
||||
btTransform startTransform = body->getWorldTransform();
|
||||
int graphicsShapeId = shape->getUserIndex();
|
||||
btAssert(graphicsShapeId >= 0);
|
||||
btVector3 localScaling = shape->getLocalScaling();
|
||||
int graphicsInstanceId = m_glApp->m_instancingRenderer->registerGraphicsInstance(graphicsShapeId, startTransform.getOrigin(), startTransform.getRotation(), color, localScaling);
|
||||
body->setUserIndex(graphicsInstanceId);
|
||||
}
|
||||
virtual void createCollisionShapeGraphicsObject(btCollisionShape* collisionShape)
|
||||
{
|
||||
//todo: support all collision shape types
|
||||
switch (collisionShape->getShapeType())
|
||||
{
|
||||
case BOX_SHAPE_PROXYTYPE:
|
||||
{
|
||||
btBoxShape* box = (btBoxShape*)collisionShape;
|
||||
btVector3 halfExtents = box->getHalfExtentsWithMargin();
|
||||
int cubeShapeId = m_glApp->registerCubeShape(halfExtents.x(), halfExtents.y(), halfExtents.z());
|
||||
box->setUserIndex(cubeShapeId);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
btAssert(0);
|
||||
}
|
||||
};
|
||||
}
|
||||
virtual void syncPhysicsToGraphics(const btDiscreteDynamicsWorld* rbWorld)
|
||||
{
|
||||
int numCollisionObjects = rbWorld->getNumCollisionObjects();
|
||||
for (int i = 0; i<numCollisionObjects; i++)
|
||||
{
|
||||
btCollisionObject* colObj = rbWorld->getCollisionObjectArray()[i];
|
||||
btVector3 pos = colObj->getWorldTransform().getOrigin();
|
||||
btQuaternion orn = colObj->getWorldTransform().getRotation();
|
||||
int index = colObj->getUserIndex();
|
||||
if (index >= 0)
|
||||
{
|
||||
m_glApp->m_instancingRenderer->writeSingleInstanceTransformToCPU(pos, orn, index);
|
||||
}
|
||||
}
|
||||
m_glApp->m_instancingRenderer->writeTransforms();
|
||||
}
|
||||
};
|
||||
|
||||
Bullet2RigidBodyDemo::Bullet2RigidBodyDemo(SimpleOpenGL3App* app, CommonPhysicsSetup* physicsSetup)
|
||||
:m_glApp(app),
|
||||
m_physicsSetup(physicsSetup),
|
||||
m_controlPressed(false),
|
||||
m_altPressed(false)
|
||||
{
|
||||
|
||||
}
|
||||
void Bullet2RigidBodyDemo::initPhysics()
|
||||
{
|
||||
m_config = new btDefaultCollisionConfiguration;
|
||||
m_dispatcher = new btCollisionDispatcher(m_config);
|
||||
m_bp = new btDbvtBroadphase();
|
||||
m_solver = new btSequentialImpulseConstraintSolver();
|
||||
m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_bp,m_solver,m_config);
|
||||
MyGraphicsPhysicsBridge glBridge(m_glApp);
|
||||
m_physicsSetup->initPhysics(glBridge);
|
||||
m_glApp->m_instancingRenderer->writeTransforms();
|
||||
|
||||
}
|
||||
|
||||
void Bullet2RigidBodyDemo::exitPhysics()
|
||||
{
|
||||
delete m_dynamicsWorld;
|
||||
m_dynamicsWorld=0;
|
||||
delete m_solver;
|
||||
m_solver=0;
|
||||
delete m_bp;
|
||||
m_bp=0;
|
||||
delete m_dispatcher;
|
||||
m_dispatcher=0;
|
||||
delete m_config;
|
||||
m_config=0;
|
||||
|
||||
m_physicsSetup->exitPhysics();
|
||||
|
||||
}
|
||||
|
||||
void Bullet2RigidBodyDemo::stepSimulation(float deltaTime)
|
||||
{
|
||||
m_dynamicsWorld->stepSimulation(deltaTime);
|
||||
m_physicsSetup->stepSimulation(deltaTime);
|
||||
|
||||
}
|
||||
|
||||
void Bullet2RigidBodyDemo::renderScene()
|
||||
{
|
||||
//sync graphics -> physics world transforms
|
||||
|
||||
MyGraphicsPhysicsBridge glBridge(m_glApp);
|
||||
m_physicsSetup->syncPhysicsToGraphics(glBridge);
|
||||
|
||||
m_glApp->m_instancingRenderer->renderScene();
|
||||
|
||||
}
|
||||
Bullet2RigidBodyDemo::~Bullet2RigidBodyDemo()
|
||||
{
|
||||
btAssert(m_config == 0);
|
||||
btAssert(m_dispatcher == 0);
|
||||
btAssert(m_bp == 0);
|
||||
btAssert(m_solver == 0);
|
||||
btAssert(m_dynamicsWorld == 0);
|
||||
}
|
||||
|
||||
btVector3 Bullet2RigidBodyDemo::getRayTo(int x,int y)
|
||||
@@ -115,28 +163,10 @@ btVector3 Bullet2RigidBodyDemo::getRayTo(int x,int y)
|
||||
|
||||
bool Bullet2RigidBodyDemo::mouseMoveCallback(float x,float y)
|
||||
{
|
||||
//if (m_data->m_altPressed!=0 || m_data->m_controlPressed!=0)
|
||||
//return false;
|
||||
|
||||
if (m_pickedBody && m_pickedConstraint)
|
||||
{
|
||||
btPoint2PointConstraint* pickCon = static_cast<btPoint2PointConstraint*>(m_pickedConstraint);
|
||||
if (pickCon)
|
||||
{
|
||||
//keep it at the same picking distance
|
||||
btVector3 newRayTo = getRayTo(x,y);
|
||||
btVector3 rayFrom;
|
||||
btVector3 oldPivotInB = pickCon->getPivotInB();
|
||||
btVector3 newPivotB;
|
||||
m_glApp->m_instancingRenderer->getCameraPosition(rayFrom);
|
||||
btVector3 dir = newRayTo-rayFrom;
|
||||
dir.normalize();
|
||||
dir *= m_oldPickingDist;
|
||||
|
||||
newPivotB = rayFrom + dir;
|
||||
pickCon->setPivotB(newPivotB);
|
||||
}
|
||||
}
|
||||
btVector3 rayTo = getRayTo(x, y);
|
||||
btVector3 rayFrom;
|
||||
m_glApp->m_instancingRenderer->getCameraPosition(rayFrom);
|
||||
m_physicsSetup->movePickedBody(rayFrom,rayTo);
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -153,53 +183,15 @@ bool Bullet2RigidBodyDemo::mouseButtonCallback(int button, int state, float x, f
|
||||
btVector3 rayFrom = camPos;
|
||||
btVector3 rayTo = getRayTo(x,y);
|
||||
|
||||
btCollisionWorld::ClosestRayResultCallback rayCallback(rayFrom,rayTo);
|
||||
m_dynamicsWorld->rayTest(rayFrom,rayTo,rayCallback);
|
||||
if (rayCallback.hasHit())
|
||||
{
|
||||
bool hasPicked = m_physicsSetup->pickBody(rayFrom, rayTo);
|
||||
|
||||
btVector3 pickPos = rayCallback.m_hitPointWorld;
|
||||
btRigidBody* body = (btRigidBody*)btRigidBody::upcast(rayCallback.m_collisionObject);
|
||||
if (body)
|
||||
{
|
||||
//other exclusions?
|
||||
if (!(body->isStaticObject() || body->isKinematicObject()))
|
||||
{
|
||||
m_pickedBody = body;
|
||||
m_pickedBody->setActivationState(DISABLE_DEACTIVATION);
|
||||
//printf("pickPos=%f,%f,%f\n",pickPos.getX(),pickPos.getY(),pickPos.getZ());
|
||||
btVector3 localPivot = body->getCenterOfMassTransform().inverse() * pickPos;
|
||||
btPoint2PointConstraint* p2p = new btPoint2PointConstraint(*body,localPivot);
|
||||
m_dynamicsWorld->addConstraint(p2p,true);
|
||||
m_pickedConstraint = p2p;
|
||||
btScalar mousePickClamping = 30.f;
|
||||
p2p->m_setting.m_impulseClamp = mousePickClamping;
|
||||
//very weak constraint for picking
|
||||
p2p->m_setting.m_tau = 0.001f;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// pickObject(pickPos, rayCallback.m_collisionObject);
|
||||
m_oldPickingPos = rayTo;
|
||||
m_hitPos = pickPos;
|
||||
m_oldPickingDist = (pickPos-rayFrom).length();
|
||||
// printf("hit !\n");
|
||||
//add p2p
|
||||
}
|
||||
|
||||
}
|
||||
} else
|
||||
{
|
||||
if (button==0)
|
||||
{
|
||||
if (m_pickedConstraint)
|
||||
{
|
||||
m_dynamicsWorld->removeConstraint(m_pickedConstraint);
|
||||
delete m_pickedConstraint;
|
||||
m_pickedConstraint=0;
|
||||
m_pickedBody = 0;
|
||||
}
|
||||
m_physicsSetup->removePickingConstraint();
|
||||
//remove p2p
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,31 +6,28 @@
|
||||
#include "../../AllBullet2Demos/BulletDemoInterface.h"
|
||||
|
||||
#include "OpenGLWindow/b3gWindowInterface.h"
|
||||
#include "../../../Demos/CommonPhysicsSetup.h"
|
||||
|
||||
|
||||
class Bullet2RigidBodyDemo : public BulletDemoInterface
|
||||
{
|
||||
public:
|
||||
class btDiscreteDynamicsWorld* m_dynamicsWorld;
|
||||
class btCollisionDispatcher* m_dispatcher;
|
||||
class btBroadphaseInterface* m_bp;
|
||||
class btCollisionConfiguration* m_config;
|
||||
class btConstraintSolver* m_solver;
|
||||
CommonPhysicsSetup* m_physicsSetup;
|
||||
|
||||
public:
|
||||
|
||||
class btRigidBody* m_pickedBody;
|
||||
class btTypedConstraint* m_pickedConstraint;
|
||||
btVector3 m_oldPickingPos;
|
||||
btVector3 m_hitPos;
|
||||
btScalar m_oldPickingDist;
|
||||
bool m_controlPressed;
|
||||
bool m_altPressed;
|
||||
|
||||
public:
|
||||
|
||||
class SimpleOpenGL3App* m_glApp;
|
||||
struct SimpleOpenGL3App* m_glApp;
|
||||
|
||||
Bullet2RigidBodyDemo(SimpleOpenGL3App* app);
|
||||
Bullet2RigidBodyDemo(SimpleOpenGL3App* app, CommonPhysicsSetup* physicsSetup);
|
||||
virtual void initPhysics();
|
||||
virtual void exitPhysics();
|
||||
virtual void renderScene();
|
||||
virtual void stepSimulation(float dt);
|
||||
|
||||
|
||||
virtual ~Bullet2RigidBodyDemo();
|
||||
btVector3 getRayTo(int x,int y);
|
||||
@@ -49,7 +46,6 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual void stepSimulation(float deltaTime);
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user