aboutsummaryrefslogtreecommitdiff
path: root/src/shared/lua_api/phys/bphysgeneric.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/lua_api/phys/bphysgeneric.cpp')
-rw-r--r--src/shared/lua_api/phys/bphysgeneric.cpp212
1 files changed, 212 insertions, 0 deletions
diff --git a/src/shared/lua_api/phys/bphysgeneric.cpp b/src/shared/lua_api/phys/bphysgeneric.cpp
new file mode 100644
index 0000000..e8ef2b5
--- /dev/null
+++ b/src/shared/lua_api/phys/bphysgeneric.cpp
@@ -0,0 +1,212 @@
+
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <btBulletDynamicsCommon.h>
+#include <shared/lua_api/common.hpp>
+
+/***
+@module phys
+*/
+
+
+/*Physics things from lua have the form of:
+{
+ rigidbody = btRigidBody,
+ node = ISceneNode,
+}
+*/
+
+/***
+Sets the direction of gravity on this object.
+@function rigidbody:setgravity({x,y,z})
+@tparam vector3d direction The direction to make gravity point
+*/
+//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;
+}
+
+/***
+Gets the direction of gravity on this object.
+@function rigidbody:getgravity()
+@treturn vector3d 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);//
+
+ btVector3 v = r->getGravity();
+ pushvector3d(L,v.x(),v.y(),v.z());
+
+ return 0;
+}
+
+/***
+Apply force at a reletive offset.
+@function rigidbody:applyforce(direction, offset = {0,0,0})
+@tparam vector3d direction The direction of the force to apply
+@tparam vector3d offset The offset from the center of gravity to apply the force
+*/
+//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;
+}
+
+/***
+Gets the damping applied to this rigidbody
+@function rigidbody:getldamping()
+@treturn number damping The ammount of damping applied to the object's momentum
+*/
+//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;
+}
+
+/***
+Gets the angular damping applied to this rigidbody
+@function rigidbody:getadamping()
+@treturn number damping The ammount of damping applied to angular momentum
+*/
+//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;
+}
+
+/***
+Gets the velocity of this object
+@function rigidbody:getvelocity()
+@treturn vector3 The velocity in each direction
+*/
+//rigidbody:getvelocity()
+int getvelocity(lua_State *L){
+ btVector3 vel;
+ lua_getfield(L,-1,"rigidbody");
+ btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1);
+
+ vel = r->getLinearVelocity();
+ pushvector3d(L,(double)vel.x(),(double)vel.y(),(double)vel.z());
+
+ return 1;
+}
+
+/***
+Sets the velocity of this object
+@function rigidbody:setvelocity()
+@tparam vector3d direction The ammount on each axis to set the velocity of this object.
+*/
+//rigidbody:setvelocity({x,y,z})
+int setvelocity(lua_State *L){
+ double x,y,z;
+ 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);
+
+ 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.
+@function rigidbody:setdamping(damping,angular_damping)
+@tparam number damping The ammount of damping the object should put on it's movement.
+@tparam number angular_damping The ammount of damping the object should put on it's angular momentum
+*/
+//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;
+}
+
+extern const luaL_reg brigidbody_m[] = {
+ {"setgravity", setgravity},
+ {"applyforce", applyforce},
+ {"getldamping", getlineardamping},
+ {"getadamping", getangulardamping},
+ {"setdamping", setdamping},
+ {"activate", activate},
+ {"getvelocity", getvelocity},
+ {"setvelocity", setvelocity},
+ {NULL, NULL}
+};