fix Lua example

This commit is contained in:
Erwin Coumans
2015-05-13 16:27:58 -07:00
parent dbae21ae5a
commit 91252db59a
3 changed files with 30 additions and 53 deletions

View File

@@ -30,6 +30,9 @@
#include "../FractureDemo/FractureDemo.h" #include "../FractureDemo/FractureDemo.h"
#include "../DynamicControlDemo/MotorDemo.h" #include "../DynamicControlDemo/MotorDemo.h"
#include "../RollingFrictionDemo/RollingFrictionDemo.h" #include "../RollingFrictionDemo/RollingFrictionDemo.h"
#ifdef ENABLE_LUA
#include "../LuaDemo/LuaPhysicsSetup.h"
#endif
#ifdef B3_USE_CLEW #ifdef B3_USE_CLEW
#ifndef NO_OPENGL3 #ifndef NO_OPENGL3
@@ -174,7 +177,10 @@ static ExampleEntry gDefaultExamples[]=
ExampleEntry(0,"Experiments"), 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.", ExampleEntry(1,"Voronoi Fracture", "Automatically create a compound rigid body using voronoi tesselation. Individual parts are modeled as rigid bodies using a btConvexHullShape.",
VoronoiFractureCreateFunc), VoronoiFractureCreateFunc),

View File

@@ -166,15 +166,18 @@ void OpenGLGuiHelper::createRigidBodyGraphicsObject(btRigidBody* body, const btV
void OpenGLGuiHelper::createCollisionObjectGraphicsObject(btCollisionObject* body, const btVector3& color) void OpenGLGuiHelper::createCollisionObjectGraphicsObject(btCollisionObject* body, const btVector3& color)
{ {
btCollisionShape* shape = body->getCollisionShape(); if (body->getUserIndex()<0)
btTransform startTransform = body->getWorldTransform();
int graphicsShapeId = shape->getUserIndex();
if (graphicsShapeId>=0)
{ {
// btAssert(graphicsShapeId >= 0); btCollisionShape* shape = body->getCollisionShape();
btVector3 localScaling = shape->getLocalScaling(); btTransform startTransform = body->getWorldTransform();
int graphicsInstanceId = m_data->m_glApp->m_renderer->registerGraphicsInstance(graphicsShapeId, startTransform.getOrigin(), startTransform.getRotation(), color, localScaling); int graphicsShapeId = shape->getUserIndex();
body->setUserIndex(graphicsInstanceId); 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);
}
} }
} }

View File

@@ -14,13 +14,12 @@ struct LuaPhysicsSetup : public CommonMultiBodyBase
virtual void exitPhysics(); 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) LuaPhysicsSetup::LuaPhysicsSetup(GUIHelperInterface* helper)
:CommonMultiBodyBase(helper), :CommonMultiBodyBase(helper)
{ {
sLuaDemo = this; sLuaDemo = this;
} }
@@ -78,13 +77,6 @@ static int gDeleteDynamicsWorld(lua_State *L)
return 0; return 0;
} }
ATTRIBUTE_ALIGNED16(struct) CustomShapeData
{
btVector3 m_localScaling;
int m_shapeIndex;
};
ATTRIBUTE_ALIGNED16(struct) CustomRigidBodyData 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)); halfExtents = btVector3(lua_tonumber(L,2),lua_tonumber(L,3),lua_tonumber(L,4));
btCollisionShape* colShape = new btBoxShape(halfExtents); 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); lua_pushlightuserdata (L, colShape);
return 1; return 1;
} else } else
@@ -136,11 +123,6 @@ static int gCreateSphereShape(lua_State *L)
btScalar radius = lua_tonumber(L,2); btScalar radius = lua_tonumber(L,2);
btCollisionShape* colShape = new btSphereShape(radius); 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); lua_pushlightuserdata (L, colShape);
return 1; return 1;
} else } else
@@ -253,26 +235,8 @@ static int gCreateRigidBody (lua_State *L)
body->getWorldTransform().setOrigin(pos); body->getWorldTransform().setOrigin(pos);
body->getWorldTransform().setRotation(orn); 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); world->addRigidBody(body);
lua_pushlightuserdata (L, body); lua_pushlightuserdata (L, body);
return 1; return 1;
} else } 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/"}; const char* prefix[]={"./","./data/","../data/","../../data/","../../../data/","../../../../data/"};
int numPrefixes = sizeof(prefix)/sizeof(const char*); 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);
}