extern "C" { #include #include #include } #include #include /*** @module phys */ /*Physics things from lua have the form of: { rigidbody = btRigidBody, node = ISceneNode, } */ btRigidBody* popRigidBody(lua_State *L){ lua_getfield(L,-1,"rigidbody"); btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); lua_pop(L,2); return r; } /*** 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; } /*** Sets the angular factor of the rigidbody TODO:What does this actually do? @function rigidbody:setangfactor(vec3 dir) @tparam vector3 dir The direction to set the angular factor */ 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); r->setAngularFactor(btVector3(x,y,z)); return 0; } /*** 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; } /*** Sets flags on this rigidbody @function rigidbody:setflags(flags) @tparam number flags */ 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); r->setFlags(flags); return 0; } /*** Apply an impulse to the rigidboy @function rigidbody:centralimpulse(vec3 impulse) @tparam vector3 impulse The direction to apply the impulse in */ int applyimpulse(lua_State *L){ double x,y,z,ox,oy,oz; popvector3d(L,&x,&y,&z); popvector3d(L,&ox,&oy,&oz); btRigidBody *r = popRigidBody(L); r->applyImpulse(btVector3(x,y,z),btVector3(ox,oy,oz)); return 0; } extern const luaL_reg brigidbody_m[] = { {"setgravity", setgravity}, {"applyforce", applyforce}, {"applyimpulse", applyimpulse}, {"getldamping", getlineardamping}, {"getadamping", getangulardamping}, {"setdamping", setdamping}, {"activate", activate}, {"getvelocity", getvelocity}, {"setvelocity", setvelocity}, {"setangfactor", setangfactor}, {"setflags", setflags}, {NULL, NULL} };