diff options
Diffstat (limited to 'src/shared/lua_api/phys/bphysgeneric.cpp')
| -rw-r--r-- | src/shared/lua_api/phys/bphysgeneric.cpp | 146 |
1 files changed, 93 insertions, 53 deletions
diff --git a/src/shared/lua_api/phys/bphysgeneric.cpp b/src/shared/lua_api/phys/bphysgeneric.cpp index 5faef2d..56320dd 100644 --- a/src/shared/lua_api/phys/bphysgeneric.cpp +++ b/src/shared/lua_api/phys/bphysgeneric.cpp @@ -6,6 +6,7 @@ extern "C" { } #include <btBulletDynamicsCommon.h> #include <shared/lua_api/common.hpp> +#include <shared/phys/physcommon.hpp> /*** @module phys @@ -14,14 +15,34 @@ extern "C" { /*Physics things from lua have the form of: { - rigidbody = btRigidBody, - node = ISceneNode, + type = "rigidbody" + collider = btRigidBody, + node = ISceneNode, --optional, on client } */ btRigidBody* popRigidBody(lua_State *L){ - lua_getfield(L,-1,"rigidbody"); + lua_getfield(L,-1,"type");//{rigidbody},"rigidbody" + if(lua_isnil(L,-1)){ + printf("Could not get type\n"); + lua_pushstring(L,"Tried to call a rigidbody method with something that did not have a \"type\""); + lua_error(L); + } + const char *s = lua_tostring(L,-1); + if(strcmp(s,"rigidbody") != 0){ + printf("Tried to pop rigidbody when it was not a rigidbody!\n"); + lua_pushstring(L,"Tried to call a rigidbody method on a "); + lua_pushstring(L,s); + lua_concat(L,2); + lua_error(L); + } + lua_getfield(L,-2,"collider");//{rigidbody},"rigidbody",ud_rigidbody + if(lua_isnil(L,-1)){ + printf("Failed to get a \"collider\" field\n"); + lua_pushstring(L,"Rigid body object was not set up correctly\n"); + lua_error(L); + } btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + lua_pop(L,3); return r; } /*** @@ -34,12 +55,8 @@ 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); - + btRigidBody *r = popRigidBody(L); btVector3 v = btVector3(x,y,z); - r->setGravity(v); return 0; @@ -52,9 +69,7 @@ Gets the direction of gravity on this object. */ //rigidbody:getgravity() int getgravity(lua_State *L){ - lua_getfield(L,-1,"rigidbody");//{rigidbody},ud_rigidbody - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2);// + btRigidBody *r = popRigidBody(L); btVector3 v = r->getGravity(); pushvector3d(L,v.x(),v.y(),v.z()); @@ -81,9 +96,7 @@ int applyforce(lua_State *L){ 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); + btRigidBody *r = popRigidBody(L); btVector3 v = btVector3(x,y,z); btVector3 o = btVector3(rx,ry,rz); @@ -100,9 +113,7 @@ Gets the damping applied to this rigidbody */ //rigidbody:getldamping() int getlineardamping(lua_State *L){ - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + btRigidBody *r = popRigidBody(L); double damp = r->getLinearDamping(); lua_pushnumber(L,damp); @@ -119,9 +130,7 @@ TODO:What does this actually do? int setangfactor(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); + btRigidBody *r = popRigidBody(L); r->setAngularFactor(btVector3(x,y,z)); return 0; } @@ -133,9 +142,7 @@ Gets the angular damping applied to this rigidbody */ //rigidbody:getadamping() int getangulardamping(lua_State *L){ - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + btRigidBody *r = popRigidBody(L); double damp = r->getAngularDamping(); lua_pushnumber(L,damp); @@ -151,8 +158,7 @@ Gets the velocity of this object //rigidbody:getvelocity() int getvelocity(lua_State *L){ btVector3 vel; - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); + btRigidBody *r = popRigidBody(L); vel = r->getLinearVelocity(); pushvector3d(L,(double)vel.x(),(double)vel.y(),(double)vel.z()); @@ -171,32 +177,13 @@ int setvelocity(lua_State *L){ popvector3d(L,&x,&y,&z); btVector3 newvel = btVector3(x,y,z); - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + btRigidBody *r = popRigidBody(L); r->setLinearVelocity(newvel); return 0; } -/*** -Activates this object. -If this object was sleeping, it will move again. If you are using -applyforce or setvelocity, you will need to activate() the rigidbody for it -to move. -@function rigidbody:activate() -*/ -//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; -} /*** Sets the damping of this object. @@ -211,9 +198,7 @@ int setdamping(lua_State *L){ 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); + btRigidBody *r = popRigidBody(L); r->setDamping(adamp,ldamp); @@ -229,9 +214,7 @@ int setflags(lua_State *L){ int flags = lua_tonumber(L,-1); lua_pop(L,1); - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + btRigidBody *r = popRigidBody(L); r->setFlags(flags); @@ -252,6 +235,60 @@ int applyimpulse(lua_State *L){ return 0; } +//collider:setpos({x,y,z}) +int setpos(lua_State *L){ + double x,y,z; + popvector3d(L,&x,&y,&z); + lua_getfield(L,-1,"collider"); + btCollisionObject *c = (btCollisionObject*)lua_touserdata(L,-1); + lua_pop(L,1); + btTransform t = c->getWorldTransform(); + t.setOrigin(btVector3(x,y,z)); + c->setWorldTransform(t); + c->activate(); + return 0; +} + +//collider:getpos() :: {x,y,z} +int getpos(lua_State *L){ + lua_getfield(L,-1,"collider"); + btCollisionObject *c = (btCollisionObject*)lua_touserdata(L,-1); + btTransform t = c->getWorldTransform(); + btVector3 o = t.getOrigin(); + pushvector3d(L,o.x(), o.y(), o.z()); + return 1; +} + +/* +A callback used to detect collisions +*/ +//class BContactResult : public ContactResultCallback +//{ + //public: + //~BContactResult(){ + //printf("Contact result being destroyed\n"); + //} + //bool needsCollision(){ + //return true; + //} + //btScalar addSingleResult(btManifoldPoint point, + //btCollisionObjectWrapper *wrap1, int part1, int index1, + //btCollisionObjectWrapper *wrap2, int part2, int index2 + //){ + //printf("Got single result\n"); + //return 1; + //} +//}; + +//rigidbody:contacttest() +//int testcontact(lua_State *L){//{rigibody} + //printf("Testing contact\n"); + //btRigidBody *r = popRigidBody(L);// + //ContactResultCallback *cr = new BContactResult(); + //World->contactTest(r,cr); + //return 0; +//} + extern const luaL_reg brigidbody_m[] = { {"setgravity", setgravity}, {"applyforce", applyforce}, @@ -259,10 +296,13 @@ extern const luaL_reg brigidbody_m[] = { {"getldamping", getlineardamping}, {"getadamping", getangulardamping}, {"setdamping", setdamping}, - {"activate", activate}, + {"getpos", getpos}, + {"setpos", setpos}, + //{"activate", activate}, {"getvelocity", getvelocity}, {"setvelocity", setvelocity}, {"setangfactor", setangfactor}, {"setflags", setflags}, + //{"testcontact", testcontact}, {NULL, NULL} }; |
