Now passing the Lua State to the physics, so that Lua can directly access physics functions.

This commit is contained in:
Steffen Fündgens 2015-01-13 17:50:15 +01:00
parent d936d1f269
commit 72d1009007
4 changed files with 30 additions and 11 deletions

View File

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

View File

@ -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>("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);

View File

@ -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>("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)

View File

@ -7,6 +7,13 @@
#include <string>
#include <stdio.h>
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);