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:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user