2015-05-13 16:17:59 +00:00
|
|
|
#include "LuaPhysicsSetup.h"
|
|
|
|
|
|
|
|
#include "../CommonInterfaces/CommonMultiBodyBase.h"
|
2016-03-11 05:15:23 +00:00
|
|
|
#include "../Importers/ImportURDFDemo/BulletURDFImporter.h"
|
2015-05-14 04:54:00 +00:00
|
|
|
#include "../Importers/ImportURDFDemo/MyMultiBodyCreator.h"
|
|
|
|
#include "../Importers/ImportURDFDemo/URDF2Bullet.h"
|
2015-05-13 16:17:59 +00:00
|
|
|
|
|
|
|
struct LuaPhysicsSetup : public CommonMultiBodyBase
|
|
|
|
{
|
|
|
|
LuaPhysicsSetup(GUIHelperInterface* helper);
|
|
|
|
virtual ~LuaPhysicsSetup();
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2015-05-13 16:17:59 +00:00
|
|
|
virtual void initPhysics();
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2015-05-13 16:17:59 +00:00
|
|
|
virtual void exitPhysics();
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2015-05-13 23:27:58 +00:00
|
|
|
virtual void stepSimulation(float deltaTime)
|
|
|
|
{
|
|
|
|
m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
|
|
|
|
CommonMultiBodyBase::stepSimulation(deltaTime);
|
|
|
|
}
|
2015-05-13 16:17:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#include "btBulletDynamicsCommon.h"
|
|
|
|
#include "LinearMath/btVector3.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include "lua.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
#include "lauxlib.h"
|
2015-05-13 16:17:59 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 05:15:23 +00:00
|
|
|
const char* sLuaFileName = "init_physics.lua";
|
|
|
|
static int upaxis = 1;
|
|
|
|
|
|
|
|
//const char* sLuaFileName = "init_urdf.lua";
|
|
|
|
//static int upaxis = 2;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
static const float scaling = 0.35f;
|
2015-05-13 16:17:59 +00:00
|
|
|
static LuaPhysicsSetup* sLuaDemo = 0;
|
|
|
|
|
|
|
|
static btVector4 colors[4] =
|
2018-09-23 21:17:31 +00:00
|
|
|
{
|
|
|
|
btVector4(1, 0, 0, 1),
|
|
|
|
btVector4(0, 1, 0, 1),
|
|
|
|
btVector4(0, 1, 1, 1),
|
|
|
|
btVector4(1, 1, 0, 1),
|
2015-05-13 16:17:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
LuaPhysicsSetup::LuaPhysicsSetup(GUIHelperInterface* helper)
|
2018-09-23 21:17:31 +00:00
|
|
|
: CommonMultiBodyBase(helper)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
sLuaDemo = this;
|
2015-05-13 16:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LuaPhysicsSetup::~LuaPhysicsSetup()
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
sLuaDemo = 0;
|
2015-05-13 16:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//todo: allow to create solver, broadphase, multiple worlds etc.
|
2018-09-23 21:17:31 +00:00
|
|
|
static int gCreateDefaultDynamicsWorld(lua_State* L)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
sLuaDemo->createEmptyDynamicsWorld();
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 grav(0, 0, 0);
|
|
|
|
grav[upaxis] = -10;
|
|
|
|
|
2016-03-11 05:15:23 +00:00
|
|
|
sLuaDemo->m_dynamicsWorld->setGravity(grav);
|
2015-05-14 04:54:00 +00:00
|
|
|
sLuaDemo->m_guiHelper->createPhysicsDebugDrawer(sLuaDemo->m_dynamicsWorld);
|
2018-09-23 21:17:31 +00:00
|
|
|
lua_pushlightuserdata(L, sLuaDemo->m_dynamicsWorld);
|
2015-05-13 16:17:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
static int gDeleteDynamicsWorld(lua_State* L)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
ATTRIBUTE_ALIGNED16(struct)
|
|
|
|
CustomRigidBodyData
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
int m_graphicsInstanceIndex;
|
|
|
|
};
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
static int gCreateCubeShape(lua_State* L)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
int argc = lua_gettop(L);
|
2018-09-23 21:17:31 +00:00
|
|
|
if (argc == 4)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 halfExtents(1, 1, 1);
|
|
|
|
if (!lua_isuserdata(L, 1))
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
std::cerr << "error: first argument to createCubeShape should be world";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
//expect userdata = sLuaDemo->m_dynamicsWorld
|
2018-09-23 21:17:31 +00:00
|
|
|
halfExtents = btVector3(lua_tonumber(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4));
|
2015-05-13 16:17:59 +00:00
|
|
|
btCollisionShape* colShape = new btBoxShape(halfExtents);
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
lua_pushlightuserdata(L, colShape);
|
2015-05-13 16:17:59 +00:00
|
|
|
return 1;
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
std::cerr << "Error: invalid number of arguments to createCubeShape, expected 4 (world,halfExtentsX,halfExtentsY,halfExtentsX) but got " << argc;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
static int gCreateSphereShape(lua_State* L)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
int argc = lua_gettop(L);
|
2018-09-23 21:17:31 +00:00
|
|
|
if (argc == 2)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 halfExtents(1, 1, 1);
|
|
|
|
if (!lua_isuserdata(L, 1))
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
std::cerr << "error: first argument to createSphereShape should be world";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
//expect userdata = sLuaDemo->m_dynamicsWorld
|
2018-09-23 21:17:31 +00:00
|
|
|
btScalar radius = lua_tonumber(L, 2);
|
2015-05-13 16:17:59 +00:00
|
|
|
btCollisionShape* colShape = new btSphereShape(radius);
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
lua_pushlightuserdata(L, colShape);
|
2015-05-13 16:17:59 +00:00
|
|
|
return 1;
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
std::cerr << "Error: invalid number of arguments to createSphereShape, expected 2 (world,radius) but got " << argc;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int luaL_returnlen(lua_State* L, int index)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
lua_len(L, index);
|
|
|
|
int len = lua_tointeger(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return len;
|
2015-05-13 16:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
btVector3 getLuaVectorArg(lua_State* L, int index)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 pos(0, 0, 0);
|
2015-05-13 16:17:59 +00:00
|
|
|
|
|
|
|
int sz = luaL_returnlen(L, index); // get size of table
|
|
|
|
{
|
|
|
|
lua_rawgeti(L, index, 1); // push t[i]
|
2018-09-23 21:17:31 +00:00
|
|
|
pos[0] = lua_tonumber(L, -1);
|
2015-05-13 16:17:59 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_rawgeti(L, index, 2); // push t[i]
|
2018-09-23 21:17:31 +00:00
|
|
|
pos[1] = lua_tonumber(L, -1);
|
2015-05-13 16:17:59 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_rawgeti(L, index, 3); // push t[i]
|
2018-09-23 21:17:31 +00:00
|
|
|
pos[2] = lua_tonumber(L, -1);
|
2015-05-13 16:17:59 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
btQuaternion getLuaQuaternionArg(lua_State* L, int index)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
btQuaternion orn(0, 0, 0, 1);
|
2015-05-13 16:17:59 +00:00
|
|
|
|
|
|
|
int sz = luaL_returnlen(L, index); // get size of table
|
|
|
|
{
|
|
|
|
lua_rawgeti(L, index, 1); // push t[i]
|
2018-09-23 21:17:31 +00:00
|
|
|
orn[0] = lua_tonumber(L, -1);
|
2015-05-13 16:17:59 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_rawgeti(L, index, 2); // push t[i]
|
2018-09-23 21:17:31 +00:00
|
|
|
orn[1] = lua_tonumber(L, -1);
|
2015-05-13 16:17:59 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_rawgeti(L, index, 3); // push t[i]
|
2018-09-23 21:17:31 +00:00
|
|
|
orn[2] = lua_tonumber(L, -1);
|
2015-05-13 16:17:59 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_rawgeti(L, index, 4); // push t[i]
|
2018-09-23 21:17:31 +00:00
|
|
|
orn[3] = lua_tonumber(L, -1);
|
2015-05-13 16:17:59 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
return orn;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
static int gLoadMultiBodyFromUrdf(lua_State* L)
|
2015-05-14 04:54:00 +00:00
|
|
|
{
|
|
|
|
int argc = lua_gettop(L);
|
2018-09-23 21:17:31 +00:00
|
|
|
if (argc == 4)
|
2015-05-14 04:54:00 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!lua_isuserdata(L, 1))
|
2015-05-14 04:54:00 +00:00
|
|
|
{
|
|
|
|
std::cerr << "error: first argument to b3CreateRigidbody should be world";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
luaL_checktype(L, 3, LUA_TTABLE);
|
2015-05-14 04:54:00 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 pos = getLuaVectorArg(L, 3);
|
2015-05-14 04:54:00 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btQuaternion orn = getLuaQuaternionArg(L, 4);
|
2015-05-13 16:17:59 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btDiscreteDynamicsWorld* world = (btDiscreteDynamicsWorld*)lua_touserdata(L, 1);
|
2015-05-14 04:54:00 +00:00
|
|
|
if (world != sLuaDemo->m_dynamicsWorld)
|
|
|
|
{
|
|
|
|
std::cerr << "error: first argument expected to be a world";
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
const char* fileName = lua_tostring(L, 2);
|
2016-03-11 05:15:23 +00:00
|
|
|
#if 1
|
2020-02-14 11:07:39 +00:00
|
|
|
BulletURDFImporter u2b(sLuaDemo->m_guiHelper, 0);
|
2018-09-23 21:17:31 +00:00
|
|
|
bool loadOk = u2b.loadURDF(fileName);
|
2015-05-14 04:54:00 +00:00
|
|
|
if (loadOk)
|
|
|
|
{
|
|
|
|
b3Printf("loaded %s OK!", fileName);
|
|
|
|
|
|
|
|
btTransform tr;
|
|
|
|
tr.setIdentity();
|
|
|
|
tr.setOrigin(pos);
|
|
|
|
tr.setRotation(orn);
|
|
|
|
int rootLinkIndex = u2b.getRootLinkIndex();
|
2018-09-23 21:17:31 +00:00
|
|
|
// printf("urdf root link index = %d\n",rootLinkIndex);
|
2015-05-14 04:54:00 +00:00
|
|
|
MyMultiBodyCreator creation(sLuaDemo->m_guiHelper);
|
|
|
|
bool m_useMultiBody = true;
|
2018-09-23 21:17:31 +00:00
|
|
|
ConvertURDF2Bullet(u2b, creation, tr, sLuaDemo->m_dynamicsWorld, m_useMultiBody, u2b.getPathPrefix());
|
2015-05-14 04:54:00 +00:00
|
|
|
btMultiBody* mb = creation.getBulletMultiBody();
|
|
|
|
|
|
|
|
if (mb)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
lua_pushlightuserdata(L, mb);
|
2015-05-14 04:54:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2015-05-14 04:54:00 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
b3Printf("can't find %s", fileName);
|
2015-05-14 04:54:00 +00:00
|
|
|
}
|
2016-03-11 05:15:23 +00:00
|
|
|
#endif
|
2015-05-14 04:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-13 16:17:59 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
static int gCreateRigidBody(lua_State* L)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
int argc = lua_gettop(L);
|
2018-09-23 21:17:31 +00:00
|
|
|
if (argc == 5)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
btTransform startTransform;
|
|
|
|
startTransform.setIdentity();
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!lua_isuserdata(L, 1))
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
std::cerr << "error: first argument to b3CreateRigidbody should be world";
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
btDiscreteDynamicsWorld* world = (btDiscreteDynamicsWorld*)lua_touserdata(L, 1);
|
2015-05-13 16:17:59 +00:00
|
|
|
if (world != sLuaDemo->m_dynamicsWorld)
|
|
|
|
{
|
|
|
|
std::cerr << "error: first argument expected to be a world";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!lua_isuserdata(L, 2))
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
2015-05-14 16:16:13 +00:00
|
|
|
std::cerr << "error: second argument to b3CreateRigidbody should be collision shape";
|
2015-05-13 16:17:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btScalar mass = lua_tonumber(L, 3);
|
2015-05-13 16:17:59 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
luaL_checktype(L, 4, LUA_TTABLE);
|
2015-05-13 16:17:59 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 pos = getLuaVectorArg(L, 4);
|
2015-05-13 16:17:59 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btQuaternion orn = getLuaQuaternionArg(L, 5);
|
2015-05-13 16:17:59 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btCollisionShape* colShape = (btCollisionShape*)lua_touserdata(L, 2);
|
2015-05-13 16:17:59 +00:00
|
|
|
//expect userdata = sLuaDemo->m_dynamicsWorld
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 inertia(0, 0, 0);
|
2015-05-13 16:17:59 +00:00
|
|
|
if (mass)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
colShape->calculateLocalInertia(mass, inertia);
|
2015-05-13 16:17:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btRigidBody* body = new btRigidBody(mass, 0, colShape, inertia);
|
2015-05-13 16:17:59 +00:00
|
|
|
body->getWorldTransform().setOrigin(pos);
|
|
|
|
body->getWorldTransform().setRotation(orn);
|
|
|
|
|
|
|
|
world->addRigidBody(body);
|
2018-09-23 21:17:31 +00:00
|
|
|
|
|
|
|
lua_pushlightuserdata(L, body);
|
2015-05-13 16:17:59 +00:00
|
|
|
return 1;
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
std::cerr << "Error: invalid number of arguments to createRigidBody, expected 5 (world,shape,mass,pos,orn) but got " << argc;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
static int gSetBodyPosition(lua_State* L)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
int argc = lua_gettop(L);
|
2018-09-23 21:17:31 +00:00
|
|
|
if (argc == 3)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!lua_isuserdata(L, 1))
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
std::cerr << "error: first argument needs to be a world";
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!lua_isuserdata(L, 2))
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
std::cerr << "error: second argument needs to be a body";
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
btRigidBody* body = (btRigidBody*)lua_touserdata(L, 2);
|
|
|
|
btVector3 pos = getLuaVectorArg(L, 3);
|
2015-05-13 16:17:59 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btTransform& tr = body->getWorldTransform();
|
2015-05-13 16:17:59 +00:00
|
|
|
tr.setOrigin(pos);
|
|
|
|
body->setWorldTransform(tr);
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
std::cerr << "error: setBodyPosition expects 6 arguments like setBodyPosition(world,body,0,1,0)";
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
static int gSetBodyOrientation(lua_State* L)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
int argc = lua_gettop(L);
|
2018-09-23 21:17:31 +00:00
|
|
|
if (argc == 3)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!lua_isuserdata(L, 1))
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
std::cerr << "error: first argument needs to be a world";
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!lua_isuserdata(L, 2))
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
std::cerr << "error: second argument needs to be a body";
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
btRigidBody* body = (btRigidBody*)lua_touserdata(L, 2);
|
|
|
|
btQuaternion orn = getLuaQuaternionArg(L, 3);
|
|
|
|
btTransform& tr = body->getWorldTransform();
|
2015-05-13 16:17:59 +00:00
|
|
|
tr.setRotation(orn);
|
|
|
|
body->setWorldTransform(tr);
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
|
|
|
std::cerr << "error: setBodyOrientation expects 3 arguments like setBodyOrientation(world,body,orn)";
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//b3CreateConvexShape(world, points)
|
|
|
|
|
|
|
|
//b3CreateHingeConstraint(world,bodyA,bodyB,...)
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
static void report_errors(lua_State* L, int status)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
std::cerr << "-- " << lua_tostring(L, -1) << std::endl;
|
|
|
|
lua_pop(L, 1); // remove error message
|
|
|
|
}
|
2015-05-13 16:17:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 23:27:58 +00:00
|
|
|
void LuaPhysicsSetup::initPhysics()
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
2016-03-11 05:15:23 +00:00
|
|
|
m_guiHelper->setUpAxis(upaxis);
|
2018-09-23 21:17:31 +00:00
|
|
|
const char* prefix[] = {"./", "./data/", "../data/", "../../data/", "../../../data/", "../../../../data/"};
|
|
|
|
int numPrefixes = sizeof(prefix) / sizeof(const char*);
|
2015-05-13 16:17:59 +00:00
|
|
|
char relativeFileName[1024];
|
2018-09-23 21:17:31 +00:00
|
|
|
FILE* f = 0;
|
2015-05-13 16:17:59 +00:00
|
|
|
int result = 0;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 0; !f && i < numPrefixes; i++)
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
sprintf(relativeFileName, "%s%s", prefix[i], sLuaFileName);
|
|
|
|
f = fopen(relativeFileName, "rb");
|
2015-05-13 16:17:59 +00:00
|
|
|
}
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
lua_State* L = luaL_newstate();
|
2015-05-13 16:17:59 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
luaopen_io(L); // provides io.*
|
2015-05-13 16:17:59 +00:00
|
|
|
luaopen_base(L);
|
|
|
|
luaopen_table(L);
|
|
|
|
luaopen_string(L);
|
|
|
|
luaopen_math(L);
|
|
|
|
//luaopen_package(L);
|
|
|
|
luaL_openlibs(L);
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
// make my_function() available to Lua programs
|
2015-05-13 16:17:59 +00:00
|
|
|
lua_register(L, "createDefaultDynamicsWorld", gCreateDefaultDynamicsWorld);
|
|
|
|
lua_register(L, "deleteDynamicsWorld", gDeleteDynamicsWorld);
|
|
|
|
lua_register(L, "createCubeShape", gCreateCubeShape);
|
|
|
|
lua_register(L, "createSphereShape", gCreateSphereShape);
|
2018-09-23 21:17:31 +00:00
|
|
|
lua_register(L, "loadMultiBodyFromUrdf", gLoadMultiBodyFromUrdf);
|
2015-05-14 16:16:13 +00:00
|
|
|
|
2015-05-13 16:17:59 +00:00
|
|
|
lua_register(L, "createRigidBody", gCreateRigidBody);
|
|
|
|
lua_register(L, "setBodyPosition", gSetBodyPosition);
|
|
|
|
lua_register(L, "setBodyOrientation", gSetBodyOrientation);
|
|
|
|
|
|
|
|
int s = luaL_loadfile(L, relativeFileName);
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (s == 0)
|
|
|
|
{
|
|
|
|
// execute Lua program
|
|
|
|
s = lua_pcall(L, 0, LUA_MULTRET, 0);
|
2015-05-13 16:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
report_errors(L, s);
|
|
|
|
lua_close(L);
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2015-05-13 16:17:59 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
b3Error("Cannot find Lua file%s\n", sLuaFileName);
|
2015-05-13 16:17:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaPhysicsSetup::exitPhysics()
|
|
|
|
{
|
|
|
|
CommonMultiBodyBase::exitPhysics();
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
class CommonExampleInterface* LuaDemoCreateFunc(struct CommonExampleOptions& options)
|
2015-05-13 23:27:58 +00:00
|
|
|
{
|
|
|
|
return new LuaPhysicsSetup(options.m_guiHelper);
|
|
|
|
}
|