From 91252db59aad551a8621a7913eac220eb02e9228 Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Wed, 13 May 2015 16:27:58 -0700 Subject: [PATCH] fix Lua example --- examples/ExampleBrowser/ExampleEntries.cpp | 8 ++- examples/ExampleBrowser/OpenGLGuiHelper.cpp | 19 ++++--- examples/LuaDemo/LuaPhysicsSetup.cpp | 56 +++++---------------- 3 files changed, 30 insertions(+), 53 deletions(-) diff --git a/examples/ExampleBrowser/ExampleEntries.cpp b/examples/ExampleBrowser/ExampleEntries.cpp index fbca872b7..037d245d7 100644 --- a/examples/ExampleBrowser/ExampleEntries.cpp +++ b/examples/ExampleBrowser/ExampleEntries.cpp @@ -30,6 +30,9 @@ #include "../FractureDemo/FractureDemo.h" #include "../DynamicControlDemo/MotorDemo.h" #include "../RollingFrictionDemo/RollingFrictionDemo.h" +#ifdef ENABLE_LUA +#include "../LuaDemo/LuaPhysicsSetup.h" +#endif #ifdef B3_USE_CLEW #ifndef NO_OPENGL3 @@ -174,7 +177,10 @@ static ExampleEntry gDefaultExamples[]= ExampleEntry(0,"Experiments"), - +#ifdef ENABLE_LUA + ExampleEntry(1,"Lua Script", "Create the dynamics world, collision shapes and rigid bodies using Lua scripting", + LuaDemoCreateFunc), +#endif ExampleEntry(1,"Voronoi Fracture", "Automatically create a compound rigid body using voronoi tesselation. Individual parts are modeled as rigid bodies using a btConvexHullShape.", VoronoiFractureCreateFunc), diff --git a/examples/ExampleBrowser/OpenGLGuiHelper.cpp b/examples/ExampleBrowser/OpenGLGuiHelper.cpp index a2c93fc89..c5d7952ca 100644 --- a/examples/ExampleBrowser/OpenGLGuiHelper.cpp +++ b/examples/ExampleBrowser/OpenGLGuiHelper.cpp @@ -166,15 +166,18 @@ void OpenGLGuiHelper::createRigidBodyGraphicsObject(btRigidBody* body, const btV void OpenGLGuiHelper::createCollisionObjectGraphicsObject(btCollisionObject* body, const btVector3& color) { - btCollisionShape* shape = body->getCollisionShape(); - btTransform startTransform = body->getWorldTransform(); - int graphicsShapeId = shape->getUserIndex(); - if (graphicsShapeId>=0) + if (body->getUserIndex()<0) { - // btAssert(graphicsShapeId >= 0); - btVector3 localScaling = shape->getLocalScaling(); - int graphicsInstanceId = m_data->m_glApp->m_renderer->registerGraphicsInstance(graphicsShapeId, startTransform.getOrigin(), startTransform.getRotation(), color, localScaling); - body->setUserIndex(graphicsInstanceId); + btCollisionShape* shape = body->getCollisionShape(); + btTransform startTransform = body->getWorldTransform(); + int graphicsShapeId = shape->getUserIndex(); + if (graphicsShapeId>=0) + { + // btAssert(graphicsShapeId >= 0); + btVector3 localScaling = shape->getLocalScaling(); + int graphicsInstanceId = m_data->m_glApp->m_renderer->registerGraphicsInstance(graphicsShapeId, startTransform.getOrigin(), startTransform.getRotation(), color, localScaling); + body->setUserIndex(graphicsInstanceId); + } } } diff --git a/examples/LuaDemo/LuaPhysicsSetup.cpp b/examples/LuaDemo/LuaPhysicsSetup.cpp index cada91dc6..db27c788f 100644 --- a/examples/LuaDemo/LuaPhysicsSetup.cpp +++ b/examples/LuaDemo/LuaPhysicsSetup.cpp @@ -14,13 +14,12 @@ struct LuaPhysicsSetup : public CommonMultiBodyBase virtual void exitPhysics(); - virtual void stepSimulation(float deltaTime); + virtual void stepSimulation(float deltaTime) + { + m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld); + CommonMultiBodyBase::stepSimulation(deltaTime); + } - virtual void debugDraw(int debugDrawFlags); - - virtual btRigidBody* createRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape, const btVector4& color=btVector4(1,0,0,1)); - - virtual btBoxShape* createBoxShape(const btVector3& halfExtents); }; @@ -52,7 +51,7 @@ static btVector4 colors[4] = }; LuaPhysicsSetup::LuaPhysicsSetup(GUIHelperInterface* helper) -:CommonMultiBodyBase(helper), +:CommonMultiBodyBase(helper) { sLuaDemo = this; } @@ -78,13 +77,6 @@ static int gDeleteDynamicsWorld(lua_State *L) return 0; } -ATTRIBUTE_ALIGNED16(struct) CustomShapeData -{ - btVector3 m_localScaling; - int m_shapeIndex; - - -}; ATTRIBUTE_ALIGNED16(struct) CustomRigidBodyData @@ -107,11 +99,6 @@ static int gCreateCubeShape(lua_State *L) halfExtents = btVector3(lua_tonumber(L,2),lua_tonumber(L,3),lua_tonumber(L,4)); btCollisionShape* colShape = new btBoxShape(halfExtents); - CustomShapeData* shapeData = new CustomShapeData(); - shapeData->m_shapeIndex = sLuaDemo->m_glApp->registerCubeShape(1,1,1); - shapeData->m_localScaling = halfExtents; - - colShape->setUserPointer(shapeData); lua_pushlightuserdata (L, colShape); return 1; } else @@ -136,11 +123,6 @@ static int gCreateSphereShape(lua_State *L) btScalar radius = lua_tonumber(L,2); btCollisionShape* colShape = new btSphereShape(radius); - CustomShapeData* shapeData = new CustomShapeData(); - shapeData->m_shapeIndex = sLuaDemo->m_glApp->registerGraphicsSphereShape(radius,false,100,0.5); - shapeData->m_localScaling = halfExtents; - - colShape->setUserPointer(shapeData); lua_pushlightuserdata (L, colShape); return 1; } else @@ -253,26 +235,8 @@ static int gCreateRigidBody (lua_State *L) body->getWorldTransform().setOrigin(pos); body->getWorldTransform().setRotation(orn); - - CustomShapeData* shapeData = (CustomShapeData*)colShape->getUserPointer(); - if (shapeData) - { - CustomRigidBodyData* rbd = new CustomRigidBodyData; - static int curColor = 0; - btVector4 color = colors[curColor]; - curColor++; - curColor&=3; - - CustomShapeData* shapeData = (CustomShapeData*)body->getCollisionShape()->getUserPointer(); - if (shapeData) - { - - rbd ->m_graphicsInstanceIndex = sLuaDemo->m_glApp->m_renderer->registerGraphicsInstance(shapeData->m_shapeIndex,startTransform.getOrigin(),startTransform.getRotation(),color,shapeData->m_localScaling); - body->setUserIndex(rbd->m_graphicsInstanceIndex); - } - } - world->addRigidBody(body); + lua_pushlightuserdata (L, body); return 1; } else @@ -351,7 +315,7 @@ static void report_errors(lua_State *L, int status) -void LuaPhysicsSetup::initPhysics(GraphicsPhysicsBridge& gfxBridge) +void LuaPhysicsSetup::initPhysics() { const char* prefix[]={"./","./data/","../data/","../../data/","../../../data/","../../../../data/"}; int numPrefixes = sizeof(prefix)/sizeof(const char*); @@ -413,3 +377,7 @@ void LuaPhysicsSetup::exitPhysics() } +class CommonExampleInterface* LuaDemoCreateFunc(struct CommonExampleOptions& options) +{ + return new LuaPhysicsSetup(options.m_guiHelper); +}