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

@@ -11,9 +11,23 @@
#include "../bullet2/RagdollDemo/RagdollDemo.h"
#include "../bullet2/LuaDemo/LuaDemo.h"
#include "../bullet2/ChainDemo/ChainDemo.h"
#include "../../Demos/CcdPhysicsDemo/CcdPhysicsSetup.h"
static BulletDemoInterface* MyCcdPhysicsDemoCreateFunc(SimpleOpenGL3App* app)
{
CommonPhysicsSetup* physicsSetup = new CcdPhysicsSetup();
return new BasicDemo(app, physicsSetup);
}
static BulletDemoInterface* MyKinematicObjectCreateFunc(SimpleOpenGL3App* app)
{
CommonPhysicsSetup* physicsSetup = new KinematicObjectSetup();
return new BasicDemo(app, physicsSetup);
}
struct BulletDemoEntry
{
int m_menuLevel;
const char* m_name;
BulletDemoInterface::CreateFunc* m_createFunc;
};
@@ -23,23 +37,28 @@ static BulletDemoEntry allDemos[]=
{
//{"emptydemo",EmptyBulletDemo::MyCreateFunc},
{"BasicDemo",BasicDemo::MyCreateFunc},
{"ChainDemo",ChainDemo::MyCreateFunc},
{"SIHingeDemo",HingeDemo::SICreateFunc},
{"PGSHingeDemo",HingeDemo::PGSCreateFunc},
{"DantzigHingeDemo",HingeDemo::DantzigCreateFunc},
{"LemkeHingeDemo",HingeDemo::LemkeCreateFunc},
{"InertiaHingeDemo",HingeDemo::InertiaCreateFunc},
{"ABMHingeDemo",HingeDemo::FeatherstoneCreateFunc},
{"Ragdoll",RagDollDemo::MyCreateFunc},
{"MultiBody1",FeatherstoneDemo1::MyCreateFunc},
// {"MultiBody2",FeatherstoneDemo2::MyCreateFunc},
{0,"API Demos", 0},
{"MultiDofDemo",MultiDofDemo::MyCreateFunc},
{1,"BasicDemo",BasicDemo::MyCreateFunc},
{ 1, "CcdDemo", MyCcdPhysicsDemoCreateFunc },
{ 1, "Kinematic", MyKinematicObjectCreateFunc },
/* {1,"ChainDemo",ChainDemo::MyCreateFunc},
// {0, "Stress tests", 0 },
{1,"SIHingeDemo",HingeDemo::SICreateFunc},
{1,"PGSHingeDemo",HingeDemo::PGSCreateFunc},
{1,"DantzigHingeDemo",HingeDemo::DantzigCreateFunc},
{1,"LemkeHingeDemo",HingeDemo::LemkeCreateFunc},
{1,"InertiaHingeDemo",HingeDemo::InertiaCreateFunc},
{1,"ABMHingeDemo",HingeDemo::FeatherstoneCreateFunc},
{1,"Ragdoll",RagDollDemo::MyCreateFunc},
*/
{ 0, "Multibody" ,0},
{1,"MultiBody1",FeatherstoneDemo1::MyCreateFunc},
// {"MultiBody2",FeatherstoneDemo2::MyCreateFunc},
{1,"MultiDofDemo",MultiDofDemo::MyCreateFunc},
// {"LuaDemo",LuaDemo::MyCreateFunc}
};

View File

@@ -14,18 +14,22 @@ SET(App_AllBullet2Demos_SRCS
../bullet2/BasicDemo/Bullet2RigidBodyDemo.h
../../Demos/BasicDemo/BasicDemoPhysicsSetup.cpp
../../Demos/BasicDemo/BasicDemoPhysicsSetup.h
../../Demos/CcdPhysicsDemo/CcdPhysicsSetup.cpp
../../Demos/CcdPhysicsDemo/CcdPhysicsSetup.h
../bullet2/FeatherstoneMultiBodyDemo/BulletMultiBodyDemos.cpp
../bullet2/FeatherstoneMultiBodyDemo/BulletMultiBodyDemos.h
../bullet2/FeatherstoneMultiBodyDemo/MultiDofDemo.cpp
../bullet2/FeatherstoneMultiBodyDemo/MultiDofDemo.h
../bullet2/BasicDemo/BasicDemo.cpp
../bullet2/BasicDemo/BasicDemo.h
../bullet2/BasicDemo/HingeDemo.cpp
../bullet2/BasicDemo/HingeDemo.h
../bullet2/ChainDemo/ChainDemo.cpp
../bullet2/ChainDemo/ChainDemo.h
../bullet2/RagdollDemo/RagdollDemo.cpp
../bullet2/RagdollDemo/RagdollDemo.h
# the next few demos are not converted to 'newer' structure yet
# target is to convert all Bullet 2 demos in new structure, but need to settle down on features
# ../bullet2/BasicDemo/HingeDemo.cpp
# ../bullet2/BasicDemo/HingeDemo.h
# ../bullet2/ChainDemo/ChainDemo.cpp
# ../bullet2/ChainDemo/ChainDemo.h
# ../bullet2/RagdollDemo/RagdollDemo.cpp
# ../bullet2/RagdollDemo/RagdollDemo.h
# ../bullet2/LuaDemo/LuaDemo.cpp
# ../bullet2/LuaDemo/LuaDemo.h
../GpuDemos/gwenUserInterface.cpp

View File

@@ -2,7 +2,7 @@
#include "Bullet3Common/b3Vector3.h"
#include "assert.h"
#include <stdio.h>
#include "../GpuDemos/gwenInternalData.h"
#include "../GpuDemos/gwenUserInterface.h"
#include "BulletDemoEntries.h"
#include "../../btgui/Timing/b3Clock.h"
@@ -11,6 +11,7 @@ const char* startFileName = "bulletDemo.txt";
static SimpleOpenGL3App* app=0;
static GwenUserInterface* gui = 0;
static int sCurrentDemoIndex = 0;
static int sCurrentHightlighted = 0;
static BulletDemoInterface* sCurrentDemo = 0;
static b3AlignedObjectArray<const char*> allNames;
@@ -22,11 +23,18 @@ static bool pauseSimulation=false;
void MyKeyboardCallback(int key, int state)
{
//printf("key=%d, state=%d\n", key, state);
bool handled = false;
if (sCurrentDemo)
if (gui)
{
handled = gui->keyboardCallback(key, state);
}
if (!handled && sCurrentDemo)
{
handled = sCurrentDemo->keyboardCallback(key,state);
}
//checkout: is it desired to ignore keys, if the demo already handles them?
//if (handled)
// return;
@@ -130,6 +138,88 @@ void MyComboBoxCallback(int comboId, const char* item)
}
struct MyMenuItemHander :public Gwen::Event::Handler
{
int m_buttonId;
MyMenuItemHander( int buttonId)
:m_buttonId(buttonId)
{
}
void onButtonA(Gwen::Controls::Base* pControl)
{
const Gwen::String& name = pControl->GetName();
Gwen::Controls::TreeNode* node = (Gwen::Controls::TreeNode*)pControl;
Gwen::Controls::Label* l = node->GetButton();
Gwen::UnicodeString la = node->GetButton()->GetText();// node->GetButton()->GetName();// GetText();
Gwen::String laa = Gwen::Utility::UnicodeToString(la);
const char* ha = laa.c_str();
//printf("selected %s\n", ha);
//int dep = but->IsDepressed();
//int tog = but->GetToggleState();
// if (m_data->m_toggleButtonCallback)
// (*m_data->m_toggleButtonCallback)(m_buttonId, tog);
}
void onButtonB(Gwen::Controls::Base* pControl)
{
Gwen::Controls::Label* label = (Gwen::Controls::Label*) pControl;
Gwen::UnicodeString la = label->GetText();// node->GetButton()->GetName();// GetText();
Gwen::String laa = Gwen::Utility::UnicodeToString(la);
const char* ha = laa.c_str();
selectDemo(sCurrentHightlighted);
saveCurrentDemoEntry(sCurrentDemoIndex, startFileName);
}
void onButtonC(Gwen::Controls::Base* pControl)
{
Gwen::Controls::Label* label = (Gwen::Controls::Label*) pControl;
Gwen::UnicodeString la = label->GetText();// node->GetButton()->GetName();// GetText();
Gwen::String laa = Gwen::Utility::UnicodeToString(la);
const char* ha = laa.c_str();
// printf("onButtonC ! %s\n", ha);
}
void onButtonD(Gwen::Controls::Base* pControl)
{
/* Gwen::Controls::Label* label = (Gwen::Controls::Label*) pControl;
Gwen::UnicodeString la = label->GetText();// node->GetButton()->GetName();// GetText();
Gwen::String laa = Gwen::Utility::UnicodeToString(la);
const char* ha = laa.c_str();
*/
// printf("onKeyReturn ! \n");
selectDemo(sCurrentHightlighted);
saveCurrentDemoEntry(sCurrentDemoIndex, startFileName);
}
void onButtonE(Gwen::Controls::Base* pControl)
{
// printf("select %d\n",m_buttonId);
sCurrentHightlighted = m_buttonId;
}
void onButtonF(Gwen::Controls::Base* pControl)
{
//printf("selection changed!\n");
}
void onButtonG(Gwen::Controls::Base* pControl)
{
//printf("onButtonG !\n");
}
};
int main(int argc, char* argv[])
{
b3Clock clock;
@@ -152,22 +242,66 @@ int main(int argc, char* argv[])
sth_stash* fontstash=app->getFontStash();
gui = new GwenUserInterface;
gui->init(width,height,fontstash,app->m_window->getRetinaScale());
// gui->getInternalData()->m_explorerPage
Gwen::Controls::TreeControl* tree = gui->getInternalData()->m_explorerTreeCtrl;
int numDemos = sizeof(allDemos)/sizeof(BulletDemoEntry);
for (int i=0;i<numDemos;i++)
char nodeText[1024];
int curDemo = 0;
int selectedDemo = loadCurrentDemoEntry(startFileName);
Gwen::Controls::TreeNode* curNode = tree;
MyMenuItemHander* handler2 = new MyMenuItemHander(-1);
tree->onReturnKeyDown.Add(handler2, &MyMenuItemHander::onButtonD);
for (int d = 0; d<numDemos; d++)
{
// sprintf(nodeText, "Node %d", i);
Gwen::UnicodeString nodeUText = Gwen::Utility::StringToUnicode(allDemos[d].m_name);
if (allDemos[d].m_menuLevel==1)
{
Gwen::Controls::TreeNode* pNode = curNode->AddNode(nodeUText);
if (d == selectedDemo)
{
pNode->SetSelected(true);
tree->ExpandAll();
selectDemo(d);
}
MyMenuItemHander* handler = new MyMenuItemHander(d);
pNode->onNamePress.Add(handler, &MyMenuItemHander::onButtonA);
pNode->GetButton()->onDoubleClick.Add(handler, &MyMenuItemHander::onButtonB);
pNode->GetButton()->onDown.Add(handler, &MyMenuItemHander::onButtonC);
pNode->onSelect.Add(handler, &MyMenuItemHander::onButtonE);
pNode->onReturnKeyDown.Add(handler, &MyMenuItemHander::onButtonG);
pNode->onSelectChange.Add(handler, &MyMenuItemHander::onButtonF);
// pNode->onKeyReturn.Add(handler, &MyMenuItemHander::onButtonD);
// pNode->GetButton()->onKeyboardReturn.Add(handler, &MyMenuItemHander::onButtonD);
// pNode->onNamePress.Add(handler, &MyMenuItemHander::onButtonD);
// pNode->onKeyboardPressed.Add(handler, &MyMenuItemHander::onButtonD);
// pNode->OnKeyPress
}
else
{
curNode = tree->AddNode(nodeUText);
}
}
/* for (int i=0;i<numDemos;i++)
{
allNames.push_back(allDemos[i].m_name);
}
selectDemo(loadCurrentDemoEntry(startFileName));
*/
//selectDemo(loadCurrentDemoEntry(startFileName));
/*
gui->registerComboBox(DEMO_SELECTION_COMBOBOX,allNames.size(),&allNames[0],sCurrentDemoIndex);
//const char* names2[] = {"comboF", "comboG","comboH"};
//gui->registerComboBox(2,3,&names2[0],0);
gui->setComboBoxCallback(MyComboBoxCallback);
*/
unsigned long int prevTimeInMicroseconds = clock.getTimeMicroseconds();
do

View File

@@ -25,21 +25,23 @@
"../bullet2/BasicDemo/Bullet2RigidBodyDemo.h",
"../../Demos/BasicDemo/BasicDemoPhysicsSetup.cpp",
"../../Demos/BasicDemo/BasicDemoPhysicsSetup.h",
"../../Demos/CcdPhysicsDemo/CcdPhysicsSetup.cpp",
"../../Demos/CcdPhysicsDemo/CcdPhysicsSetup.h",
"../bullet2/FeatherstoneMultiBodyDemo/BulletMultiBodyDemos.cpp",
"../bullet2/FeatherstoneMultiBodyDemo/BulletMultiBodyDemos.h",
"../bullet2/FeatherstoneMultiBodyDemo/MultiDofDemo.cpp",
"../bullet2/FeatherstoneMultiBodyDemo/MultiDofDemo.h",
"../bullet2/BasicDemo/BasicDemo.cpp",
"../bullet2/BasicDemo/BasicDemo.h",
"../bullet2/BasicDemo/HingeDemo.cpp",
"../bullet2/BasicDemo/HingeDemo.h",
"../bullet2/ChainDemo/ChainDemo.cpp",
"../bullet2/ChainDemo/ChainDemo.h",
-- "../bullet2/BasicDemo/HingeDemo.cpp",
-- "../bullet2/BasicDemo/HingeDemo.h",
-- "../bullet2/ChainDemo/ChainDemo.cpp",
-- "../bullet2/ChainDemo/ChainDemo.h",
"../bullet2/RagdollDemo/RagdollDemo.cpp",
"../bullet2/RagdollDemo/RagdollDemo.h",
"../bullet2/LuaDemo/LuaDemo.cpp",
"../bullet2/LuaDemo/LuaDemo.h",
-- "../bullet2/RagdollDemo/RagdollDemo.cpp",
-- "../bullet2/RagdollDemo/RagdollDemo.h",
-- "../bullet2/LuaDemo/LuaDemo.cpp",
-- "../bullet2/LuaDemo/LuaDemo.h",
"../../src/Bullet3Common/**.cpp",

View File

@@ -26,6 +26,7 @@
#include "Gwen/Controls/ListBox.h"
#include "Gwen/Skins/Simple.h"
//#include "Gwen/Skins/TexturedBase.h"
#include "gwenUserInterface.h"
struct GwenInternalData
@@ -36,12 +37,14 @@ struct GwenInternalData
Gwen::Controls::Canvas* pCanvas;
GLPrimitiveRenderer* m_primRenderer;
Gwen::Controls::TabButton* m_demoPage;
Gwen::Controls::TabButton* m_explorerPage;
Gwen::Controls::TreeControl* m_explorerTreeCtrl;
int m_curYposition;
Gwen::Controls::Label* m_rightStatusBar;
Gwen::Controls::Label* m_leftStatusBar;
b3AlignedObjectArray<struct Gwen::Event::Handler*> m_handlers;
b3AlignedObjectArray<class Gwen::Event::Handler*> m_handlers;
b3ToggleButtonCallback m_toggleButtonCallback;
b3ComboBoxCallback m_comboBoxCallback;

View File

@@ -151,29 +151,22 @@ void GwenUserInterface::init(int width, int height,struct sth_stash* stash,float
//m_data->m_leftStatusBar->SetText( L"Label Added to Left" );
m_data->m_leftStatusBar->SetWidth(width/2);
bar->AddControl( m_data->m_leftStatusBar,false);
//Gwen::KeyboardFocus
/*Gwen::Controls::GroupBox* box = new Gwen::Controls::GroupBox(m_data->pCanvas);
box->SetText("text");
box->SetName("name");
box->SetHeight(500);
*/
Gwen::Controls::ScrollControl* windowLeft= new Gwen::Controls::ScrollControl(m_data->pCanvas);
windowLeft->Dock(Gwen::Pos::Right);
windowLeft->SetWidth(150);
windowLeft->SetHeight(250);
windowLeft->SetScroll(false,true);
Gwen::Controls::ScrollControl* windowRight= new Gwen::Controls::ScrollControl(m_data->pCanvas);
windowRight->Dock(Gwen::Pos::Right);
windowRight->SetWidth(150);
windowRight->SetHeight(250);
windowRight->SetScroll(false,true);
/*Gwen::Controls::WindowControl* windowLeft = new Gwen::Controls::WindowControl(m_data->pCanvas);
windowLeft->Dock(Gwen::Pos::Left);
windowLeft->SetTitle("title");
windowLeft->SetWidth(150);
windowLeft->SetClosable(false);
windowLeft->SetShouldDrawBackground(true);
windowLeft->SetTabable(true);
*/
//windowLeft->SetSkin(
Gwen::Controls::TabControl* tab = new Gwen::Controls::TabControl(windowLeft);
Gwen::Controls::TabControl* tab = new Gwen::Controls::TabControl(windowRight);
//tab->SetHeight(300);
tab->SetWidth(140);
@@ -217,6 +210,39 @@ void GwenUserInterface::init(int width, int height,struct sth_stash* stash,float
*/
Gwen::Controls::ScrollControl* windowLeft = new Gwen::Controls::ScrollControl(m_data->pCanvas);
windowLeft->Dock(Gwen::Pos::Left);
// windowLeft->SetTitle("title");
windowLeft->SetScroll(false, false);
windowLeft->SetWidth(250);
windowLeft->SetPos(50, 50);
windowLeft->SetHeight(500);
//windowLeft->SetClosable(false);
// windowLeft->SetShouldDrawBackground(true);
windowLeft->SetTabable(true);
Gwen::Controls::TabControl* explorerTab = new Gwen::Controls::TabControl(windowLeft);
//tab->SetHeight(300);
// explorerTab->SetWidth(230);
explorerTab->SetHeight(250);
//tab->Dock(Gwen::Pos::Left);
explorerTab->Dock(Gwen::Pos::Fill);
Gwen::UnicodeString explorerStr1(L"Explorer");
m_data->m_explorerPage = explorerTab->AddPage(explorerStr1);
Gwen::UnicodeString shapesStr1(L"Shapes");
explorerTab->AddPage(shapesStr1);
Gwen::UnicodeString testStr1(L"Test");
explorerTab->AddPage(testStr1);
Gwen::Controls::TreeControl* ctrl = new Gwen::Controls::TreeControl(m_data->m_explorerPage->GetPage());
m_data->m_explorerTreeCtrl = ctrl;
ctrl->SetKeyboardInputEnabled(true);
ctrl->Focus();
ctrl->SetBounds(2, 10, 236, 400);
}
b3ToggleButtonCallback GwenUserInterface::getToggleButtonCallback()
@@ -318,6 +344,47 @@ bool GwenUserInterface::mouseMoveCallback( float x, float y)
return handled;
}
#include "OpenGLWindow/b3gWindowInterface.h"
bool GwenUserInterface::keyboardCallback(int bulletKey, int state)
{
int key = -1;
if (m_data->pCanvas)
{
//convert 'Bullet' keys into 'Gwen' keys
switch (bulletKey)
{
case B3G_RETURN:
{
key = Gwen::Key::Return;
break;
}
case B3G_LEFT_ARROW:
key = Gwen::Key::Left;
break;
case B3G_RIGHT_ARROW:
key = Gwen::Key::Right;
break;
case B3G_UP_ARROW:
key = Gwen::Key::Up;
break;
case B3G_DOWN_ARROW:
key = Gwen::Key::Down;
break;
default:
{
}
};
bool bDown = (state == 1);
return m_data->pCanvas->InputKey(key, bDown);
}
return false;
}
bool GwenUserInterface::mouseButtonCallback(int button, int state, float x, float y)
{
bool handled = false;
@@ -327,7 +394,7 @@ bool GwenUserInterface::mouseButtonCallback(int button, int state, float x, floa
if (button>=0)
{
handled = m_data->pCanvas->InputMouseButton(button,state);
handled = m_data->pCanvas->InputMouseButton(button,(bool)state);
if (handled)
{
//if (!state)

View File

@@ -6,6 +6,8 @@ struct GwenInternalData;
typedef void (*b3ComboBoxCallback) (int combobox, const char* item);
typedef void (*b3ToggleButtonCallback)(int button, int state);
class GwenUserInterface
{
GwenInternalData* m_data;
@@ -24,6 +26,8 @@ class GwenUserInterface
bool mouseMoveCallback( float x, float y);
bool mouseButtonCallback(int button, int state, float x, float y);
bool keyboardCallback(int key, int state);
void setToggleButtonCallback(b3ToggleButtonCallback callback);
b3ToggleButtonCallback getToggleButtonCallback();
@@ -33,7 +37,7 @@ class GwenUserInterface
void setComboBoxCallback(b3ComboBoxCallback callback);
b3ComboBoxCallback getComboBoxCallback();
void registerComboBox(int buttonId, int numItems, const char** items, int startItem = 0);
void setStatusBarMessage(const char* message, bool isLeft=true);
GwenInternalData* getInternalData()

View File

@@ -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");
}
}
}
*/
}

View File

@@ -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);
};

View File

@@ -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
}
}

View File

@@ -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);
};

View File

@@ -343,17 +343,6 @@ public:
RagDollDemo::RagDollDemo(SimpleOpenGL3App* app)
:BasicDemo(app)
{
}
RagDollDemo::~RagDollDemo()
{
}
void RagDollDemo::initPhysics()

View File

@@ -2,21 +2,24 @@
#define RAGDOLL_DEMO_H
#include "../../Demos/CommonRigidBodySetup.h"
#include "../BasicDemo/BasicDemo.h"
class RagDollDemo : public BasicDemo
struct BulletDemoInterface;
struct SimpleOpenGL3App;
class RagDollSetup : public CommonRigidBodySetup
{
public:
RagDollDemo(SimpleOpenGL3App* app);
virtual ~RagDollDemo();
static BulletDemoInterface* MyCreateFunc(SimpleOpenGL3App* app)
{
return new RagDollDemo(app);
CommonPhysicsSetup* physicsSetup = new RagDollSetup();
return new BasicDemo(app, physicsSetup);
}
void initPhysics();
void initPhysics(GraphicsPhysicsBridge& gfxBridge);
};