From 921cfb8d147aea751bb494b4a88efe4cb75daa68 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sun, 23 Sep 2018 16:07:21 -0400 Subject: Started moving generic physics into it's own file Created bphysgeneric that holds all the shared code for bullet rigidbodies. It needs to be included in any structs that have a rigidbody field. --- src/client/lua_api/phys/bphysgeneric.cpp | 115 +++++++++++++++++++++++++++++++ src/client/lua_api/phys/bphysgeneric.hpp | 24 +++++++ 2 files changed, 139 insertions(+) create mode 100644 src/client/lua_api/phys/bphysgeneric.cpp create mode 100644 src/client/lua_api/phys/bphysgeneric.hpp (limited to 'src/client/lua_api/phys') diff --git a/src/client/lua_api/phys/bphysgeneric.cpp b/src/client/lua_api/phys/bphysgeneric.cpp new file mode 100644 index 0000000..0d882d6 --- /dev/null +++ b/src/client/lua_api/phys/bphysgeneric.cpp @@ -0,0 +1,115 @@ + +extern "C" { + #include + #include + #include +} +#include +//#include +#include + +//extern IrrlichtDevice* device; + +//extern btDiscreteDynamicsWorld* World; +//extern std::list Objects; + +/*Physics things have the form of: +{ + rigidbody = btRigidBody, + node = ISceneNode, +} +*/ + +//rigidbody:setgravity({x,y,z}) +int setgravity(lua_State *L){ + double x,y,z; + popvector3d(L,&x,&y,&z); + + lua_getfield(L,-1,"rigidbody"); + btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); + lua_pop(L,2); + + btVector3 v = btVector3(x,y,z); + + r->setGravity(v); + + return 0; +} + +//apply force at a reletive offset +//rigidbody:applyforce({x,y,z}[,{rx,ry,rz}]) +int applyforce(lua_State *L){ + double rx,ry,rz; + rx = 0; + ry = 0; + rz = 0; + if(lua_gettop(L) > 2){ + popvector3d(L,&rx,&ry,&rz);//{phys},{x,y,z} + } + + double x,y,z; + popvector3d(L,&x,&y,&z);//{phys} + + lua_getfield(L,-1,"rigidbody"); + btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); + lua_pop(L,2); + + btVector3 v = btVector3(x,y,z); + btVector3 o = btVector3(rx,ry,rz); + + r->applyForce(v,o); + + return 0; +} + +//rigidbody:getldamping() +int getlineardamping(lua_State *L){ + lua_getfield(L,-1,"rigidbody"); + btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); + lua_pop(L,2); + + double damp = r->getLinearDamping(); + lua_pushnumber(L,damp); + + return 1; +} + + +//rigidbody:getadamping() +int getangulardamping(lua_State *L){ + lua_getfield(L,-1,"rigidbody"); + btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); + lua_pop(L,2); + + double damp = r->getAngularDamping(); + lua_pushnumber(L,damp); + + return 1; +} + +//rigidbody:activate() +int activate(lua_State *L){ + lua_getfield(L,-1,"rigidbody"); + btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); + lua_pop(L,2); + + r->activate(); + + return 0; +} + +//rigidbody:setdamping(lineardamping, angulardamping) +int setdamping(lua_State *L){ + double adamp,ldamp; + adamp = lua_tonumber(L,-1); + ldamp = lua_tonumber(L,-2); + lua_pop(L,2); + + lua_getfield(L,-1,"rigidbody"); + btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); + lua_pop(L,2); + + r->setDamping(adamp,ldamp); + + return 0; +} diff --git a/src/client/lua_api/phys/bphysgeneric.hpp b/src/client/lua_api/phys/bphysgeneric.hpp new file mode 100644 index 0000000..56bfaed --- /dev/null +++ b/src/client/lua_api/phys/bphysgeneric.hpp @@ -0,0 +1,24 @@ + +extern "C" { + #include + #include + #include +} + + +int setgravity(lua_State *L); +int applyforce(lua_State *L); +int getlineardamping(lua_State *L); +int getangulardamping(lua_State *L); +int setdamping(lua_State *L); +int activate(lua_State *L); + +static const luaL_reg brigidbody_m[] = { + {"setgravity", setgravity}, + {"applyforce", applyforce}, + {"getldamping", getlineardamping}, + {"getadamping", getangulardamping}, + {"setdamping", setdamping}, + {"activate", activate}, + {NULL, NULL} +}; -- cgit v1.2.3-70-g09d2