From b483cb2eb9c2952e83b804359b1ee6ed39d5c8b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20F=C3=BCndgens?= Date: Tue, 13 Jan 2015 17:50:15 +0100 Subject: [PATCH] Now passing the Lua State to the physics, so that Lua can directly access physics functions. --- Levels/scripts/deleteRandomObject.lua | 12 ++++++++---- level.cc | 8 ++++---- physics.cc | 12 ++++++++++-- physics.hh | 9 ++++++++- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Levels/scripts/deleteRandomObject.lua b/Levels/scripts/deleteRandomObject.lua index 365e5b3..25fbbc1 100644 --- a/Levels/scripts/deleteRandomObject.lua +++ b/Levels/scripts/deleteRandomObject.lua @@ -1,10 +1,14 @@ function trigger(objectToChange) - if(not this_level) then - print("No level found!") + if(not level) then + print("No level found in Lua!") return end - a = this_level:getObjectCount() + if(not physics) then + print("No physics found in Lua!") + return + end + a = level:getObjectCount() rand = math.random(0, a - 1) - this_level:deleteObject(rand) + level:deleteObject(rand) print("Triggered from Lua!") end diff --git a/level.cc b/level.cc index 369b8b3..1a64eb0 100644 --- a/level.cc +++ b/level.cc @@ -57,19 +57,19 @@ void Level::load() { // Create a new lua state L = luaL_newstate(); luaL_openlibs(L); - //Expose the class Level and its functions + //Expose the class Level and its functions to Lua luabridge::getGlobalNamespace(L) .beginClass("Level") .addFunction("deleteObject", &Level::deleteObject) .addFunction("getObjectCount", &Level::getObjectCount) .endClass(); - //Push the level as a global variable + //Push the level to Lua as a global variable luabridge::push(L, this); - lua_setglobal(L, "this_level"); + lua_setglobal(L, "level"); this->physics = Physics(); - this->physics.init(); + this->physics.init(L); // currently hard coded should later read this stuff out of a file this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f); diff --git a/physics.cc b/physics.cc index 95e46f3..6970a57 100644 --- a/physics.cc +++ b/physics.cc @@ -8,15 +8,23 @@ Physics::Physics() { Physics::~Physics() { } -void Physics::init() +void Physics::init(lua_State* L) { colConfig = new btDefaultCollisionConfiguration(); dispatcher = new btCollisionDispatcher(colConfig); broadphase = new btDbvtBroadphase(); solver = new btSequentialImpulseConstraintSolver(); world = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,colConfig); + world->setGravity(btVector3(0,-10,-0)); - world->setGravity(btVector3(0,-10,-0)); + //Expose the class Physics and its functions + luabridge::getGlobalNamespace(L) + .beginClass("Physics") + //.addFunction("", &Physics::) + .endClass(); + //Push the physics to Lua as a global variable + luabridge::push(L, this); + lua_setglobal(L, "physics"); } void Physics::takeUpdateStep(float timeDiff) diff --git a/physics.hh b/physics.hh index c7104ff..55a42ae 100644 --- a/physics.hh +++ b/physics.hh @@ -7,6 +7,13 @@ #include #include +extern "C" { +#include "extern/lua/src/lua.h" +#include "extern/lua/src/lualib.h" +#include "extern/lua/src/lauxlib.h" +} +#include "extern/luabridge/LuaBridge.h" + #include "entity.hh" #include "extern/bullet/src/BulletDynamics/Dynamics/btRigidBody.h" @@ -44,7 +51,7 @@ class Physics { public: Physics(); ~Physics(); - void init(); + void init(lua_State* L); void takeUpdateStep(float timeDiff); //must be used in level.update to proagate the physics void rollForward(glm::vec3 camPos, float strength); //self explainitory void rollLeft(glm::vec3 camPos, float strength);