diff options
Diffstat (limited to 'src/shared/lua_api')
| -rw-r--r-- | src/shared/lua_api/load_common.cpp | 60 | ||||
| -rw-r--r-- | src/shared/lua_api/load_common.hpp | 8 | ||||
| -rw-r--r-- | src/shared/lua_api/load_phys.cpp | 4 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bcharactercontroller.cpp | 600 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bcharactercontroller.hpp | 24 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bcollider.cpp | 178 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bcollider.hpp | 14 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bghostobject.cpp | 421 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bghostobject.hpp | 26 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bhingeconstraint.cpp | 122 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bhingeconstraint.hpp | 4 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bphysbox.cpp | 4 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bphysmodel.cpp | 9 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bshape.cpp | 104 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bshape.hpp | 22 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/btaction.cpp | 125 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/btaction.hpp | 22 |
17 files changed, 902 insertions, 845 deletions
diff --git a/src/shared/lua_api/load_common.cpp b/src/shared/lua_api/load_common.cpp index dc61ef1..1eca598 100644 --- a/src/shared/lua_api/load_common.cpp +++ b/src/shared/lua_api/load_common.cpp @@ -1,30 +1,30 @@ -#include <chrono> -#include <shared/lua_api/load_common.hpp> -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} -using namespace std::chrono; - -//Gets the time -int get_time(lua_State* L){ - std::chrono::high_resolution_clock::time_point now = high_resolution_clock::now(); - std::chrono::high_resolution_clock::duration since_epoch = now.time_since_epoch(); - double dc = std::chrono::duration_cast<std::chrono::milliseconds>(since_epoch).count(); - lua_pushnumber(L,dc); - return 1; -} - -void loadCommonLibs(lua_State* L){ - lua_getglobal(L,"GAME"); - lua_pushcfunction(L,make_crashy); - lua_setfield(L,-2,"crashy"); - lua_pop(L,1); - lua_pushcfunction(L,get_time); - lua_setglobal(L,"get_time"); -} - -void gameloop_common(lua_State* L){ - -} +#include <chrono>
+#include <shared/lua_api/load_common.hpp>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+using namespace std::chrono;
+
+//Gets the time
+int get_time(lua_State* L){
+ std::chrono::high_resolution_clock::time_point now = high_resolution_clock::now();
+ std::chrono::high_resolution_clock::duration since_epoch = now.time_since_epoch();
+ double dc = std::chrono::duration_cast<std::chrono::milliseconds>(since_epoch).count();
+ lua_pushnumber(L,dc);
+ return 1;
+}
+
+void loadCommonLibs(lua_State* L){
+ lua_getglobal(L,"GAME");
+ lua_pushcfunction(L,make_crashy);
+ lua_setfield(L,-2,"crashy");
+ lua_pop(L,1);
+ lua_pushcfunction(L,get_time);
+ lua_setglobal(L,"get_time");
+}
+
+void gameloop_common(lua_State* L){
+
+}
diff --git a/src/shared/lua_api/load_common.hpp b/src/shared/lua_api/load_common.hpp index 759ba85..c8741c5 100644 --- a/src/shared/lua_api/load_common.hpp +++ b/src/shared/lua_api/load_common.hpp @@ -1,4 +1,4 @@ -#include <shared/lua_api/common.hpp> - -void loadCommonLibs(lua_State* L); -void gameloop_common(lua_State* L); +#include <shared/lua_api/common.hpp>
+
+void loadCommonLibs(lua_State* L);
+void gameloop_common(lua_State* L);
diff --git a/src/shared/lua_api/load_phys.cpp b/src/shared/lua_api/load_phys.cpp index 33a59fe..d20545e 100644 --- a/src/shared/lua_api/load_phys.cpp +++ b/src/shared/lua_api/load_phys.cpp @@ -31,7 +31,7 @@ int shapecast(lua_State *L){ //hw = cb->m_hitPointWorld; //hn = cb->m_hitNormalWorld; //printf("before getting results\n\tHasHit:%d\n\tHit:%f,%f,%f\n\tNor:%f,%f,%f\n",cb->hasHit() ? 1 : 0,hw.x(),hw.y(),hw.z(),hn.x(),hn.y(),hn.z()); - World->convexSweepTest(cs,ft,tt,*cb,0.f); + World->convexSweepTest(cs,ft,tt,*cb,1.f); hw = cb->m_hitPointWorld; hn = cb->m_hitNormalWorld; //printf("Got sweep results\n\tHasHit:%d\n\tHit:%f,%f,%f\n\tNor:%f,%f,%f\n",cb->hasHit() ? 1 : 0,hw.x(),hw.y(),hw.z(),hn.x(),hn.y(),hn.z()); @@ -42,7 +42,7 @@ int shapecast(lua_State *L){ lua_setfield(L,-2,"pos"); pushvector3d(L,hn.x(),hn.y(),hn.z()); lua_setfield(L,-2,"normal"); - + delete cb; return 1; } diff --git a/src/shared/lua_api/phys/bcharactercontroller.cpp b/src/shared/lua_api/phys/bcharactercontroller.cpp index f61aca5..09941c8 100644 --- a/src/shared/lua_api/phys/bcharactercontroller.cpp +++ b/src/shared/lua_api/phys/bcharactercontroller.cpp @@ -1,300 +1,300 @@ - - -#include <stdio.h> -#include <stdlib.h> -#include <list> -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} -#include <btBulletDynamicsCommon.h> -#include <BulletDynamics/Character/btKinematicCharacterController.h> -#include <BulletCollision/CollisionDispatch/btGhostObject.h> -#include "bcharactercontroller.hpp" -#include <shared/lua_api/common.hpp> - -extern btDiscreteDynamicsWorld* World; -extern std::list<btCollisionObject*> Objects; -extern std::list<btKinematicCharacterController*> Chars; - -//{character} :: btKinematicCharacterController* -btKinematicCharacterController *popCharacter(lua_State *L){ - lua_getfield(L,-1,"type");//{char},"type" - if(lua_isnil(L,-1)){ - lua_pushstring(L,"Tried to call a character method on something that had not 'type'"); - lua_error(L); - } - const char *s = lua_tostring(L,-1);//{char},"type" - if(strcmp(s,"character")!= 0){ - printf("Tried to pop character when it was not a character!\n"); - lua_pushstring(L,"Tried to call a character method on a "); - lua_pushstring(L,s); - lua_concat(L,2); - lua_error(L); - } - lua_getfield(L,-2,"character");//{char},"type",ud_character - if(lua_isnil(L,-1)){ - printf("Failed to get a \"character\" field\n"); - lua_pushstring(L,"Character object was not set up correctly\n"); - lua_error(L); - } - btKinematicCharacterController *c = (btKinematicCharacterController*)lua_touserdata(L,-1); - lua_pop(L,3); - return c; - -} -/* -static LBPhysNode* checkisbphysbox(lua_State* L, int index){ - void* ud = luaL_checkudata(L,index,"phys.physbox"); - luaL_argcheck(L,ud != NULL, index, "'phys.physbox' expected"); - return (LBPhysNode*) ud; -} -*/ - -/* -static LISceneNode* checkismesh(lua_State* L){ - return checkismesh(L,1); -} -*/ -// ud_character :: ({v3 size}, {v3 origin}) -void makenewbcharactercontroller(lua_State* L){ - printf("Creating new character controller\n"); - //lua_pushstring(L,"Character controller is totally fucking broken for now\n"); - //lua_error(L); - double px,py,pz; //position - double sx,sy,sz; //size - //double mass; - - //mass = lua_tonumber(L,-1);//{v3_size},{v3_origin},mass - //lua_pop(L,1);//{v3_size},{v3_origin} - //printf("Got mass: %f\n",mass); - - popvector3d(L,&px,&py,&pz);//{v3_size} - printf("Got position: (%f,%f,%f)\n",px,py,pz); - popvector3d(L,&sx,&sy,&sz);// - - btVector3 vshape = btVector3(sx * 0.5f, sy * 0.5f, sz * 0.5f); - printf("Got size: (%f,%f,%f)\n",sx,sy,sz); - btVector3 pos = btVector3(px,py,pz); - btTransform transform = btTransform(btQuaternion(0,0,0,1),pos); - - // Create the shape - btConvexShape* cshape = new btBoxShape(vshape); - - - - // Add mass - //btVector3 localinertia = btVector3(0,0,0); - //shape->calculateLocalInertia(mass, localinertia); - - // Create the rigid body object - //btRigidBody::btRigidBodyConstructionInfo cinfo = btRigidBody::btRigidBodyConstructionInfo( - //mass, - //motionstate, - //shape, - //localinertia - //); - btPairCachingGhostObject *ghost = new btPairCachingGhostObject(); - ghost->setWorldTransform(transform); - ghost->setCollisionShape(cshape); - World->addCollisionObject(ghost,btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter); - //ghost->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT); - printf("Character controller created\n"); - btKinematicCharacterController *cc = new btKinematicCharacterController(ghost, cshape, 1, btVector3(0,1,0)); - //cc->setMaxSlope(3.14 / 4.0); - //cinfo.m_friction = 0; - - // Add it to the world - printf("About to add action\n"); - World->addAction(cc); - printf("Finished adding action\n"); - //printf("Added rigid body to world: %p\n",World); - printf("Added to Chars\n"); - //Chars.push_back(cc); - //Objects.push_back(ghost); - - lua_pushlightuserdata(L,cc);//ud_cc -} - -// char:getvelocity() -int bcharactergetvelocity(lua_State *L){ - btKinematicCharacterController *r = popCharacter(L); - btVector3 v = r->getLinearVelocity(); - pushvector3d(L,v.x(),v.y(),v.z()); - return 1; -} - -// char:setvelocity(v3 vel) -int bcharactersetvelocity(lua_State *L){ - double x,y,z; - popvector3d(L,&x,&y,&z); - btKinematicCharacterController *r = popCharacter(L); - r->setLinearVelocity(btVector3(x,y,z)); - return 0; -} - -// phys.newphysbox(vector3 size, vector3 origin, double mass) -int newbcharactercontroller(lua_State* L){ - //printf("Createing bphysbox!\n"); - //Create it's lua representation - makenewbcharactercontroller(L);//ud_cc - btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1);//ud_cc - lua_pop(L,1);// - lua_newtable(L);//{} - lua_pushlightuserdata(L,r);//{},ud_cc - lua_setfield(L,-2,"character");//{character=ud_cc} - - lua_pushstring(L,"character"); - lua_setfield(L,-2,"type");//{character=ud_cc,type="character"} - - //Set it's metatable - luaL_getmetatable(L, "phys.charactercontroller");//{},{phys.charactercontroller} - lua_setmetatable(L, -2);//{cc} - - return 1; -} - -// char:setgravity(v3 gravity) -int bcharactersetgravity(lua_State *L){ - double x,y,z; - popvector3d(L,&x,&y,&z); - btKinematicCharacterController *c = popCharacter(L); - c->setGravity(btVector3(x,y,z)); - return 0; -} - -// char:getpos() :: v3 -int bcharactergetpos(lua_State *L){ - btKinematicCharacterController *c = popCharacter(L); - btVector3 pos = c->getGhostObject()->getWorldTransform().getOrigin(); - pushvector3d(L,pos.x(),pos.y(),pos.z()); - return 1; -} - -// char:setpos(v3 pos) -int bcharactersetpos(lua_State *L){ - double x,y,z; - popvector3d(L,&x,&y,&z); - btKinematicCharacterController *c = popCharacter(L); - c->warp(btVector3(x,y,z)); - //btTransform t = c->getGhostObject()->getWorldTransform(); - //t.setOrigin(btVector3(x,y,z)); - //c->getGhostObject()->setWorldTransform(t); - return 0; -} - -// char:onground() -int bcharacteronground(lua_State *L){ - btKinematicCharacterController *c = popCharacter(L); - lua_pushboolean(L,c->onGround() == true ? 1 : 0); - return 1; -} - -// char:jump(v3 jump) -int bcharacterjump(lua_State *L){ - //printf("Jump called\n"); - double x,y,z; - popvector3d(L,&x,&y,&z); - btKinematicCharacterController *c = popCharacter(L); - //printf("About to jump\n"); - c->jump(btVector3(x,y,z)); - //printf("Done jumping\n"); - return 0; -} - -//{phys.physbox}:delete() -static int delbcharactercontroller(lua_State* L){//self - //printf("Attempting to delete physbox\n"); - lua_getfield(L,-1,"character");//self,ud_character - btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1);//self,ud_character - lua_pop(L,2); - delete r->getGhostObject(); - delete r; - return 0; -} - -//{char},{v3_dir} :: -int bcharsetwalkdirection(lua_State *L){ - double x,y,z; - popvector3d(L,&x,&y,&z);//{char} - lua_getfield(L,-1,"character");//{char},ud_cc - btKinematicCharacterController* cc = (btKinematicCharacterController*)lua_touserdata(L,-1); - lua_pop(L,2); - cc->setWalkDirection(btVector3(x,y,z)); - return 0; -} - -// char:setfallspeed(n) -int bcharactersetfallspeed(lua_State *L){ - double speed = luaL_optnumber(L,-1,1); - printf("Got number: %f\n",speed); - lua_pop(L,1); - btKinematicCharacterController *c = popCharacter(L); - printf("About to set speed\n"); - c->setFallSpeed(speed); - printf("Done setting speed\n"); - return 0; -} - -// char:getmaxslope() -int bcharactergetmaxslope(lua_State *L){ - btKinematicCharacterController *c = popCharacter(L); - btScalar s = c->getMaxSlope(); - lua_pushnumber(L,s); - return 1; -} - -// char:setmaxslope(slope) -int bcharactersetmaxslope(lua_State *L){ - btScalar s = lua_tonumber(L,-1); - lua_pop(L,1); - btKinematicCharacterController *c = popCharacter(L); - c->setMaxSlope(s); - return 0; -} - -// char:setvelocityforinterval(vec3 {velocity},number interval) -int bcharactersetvelocityforinterval(lua_State *L){ - double interval = lua_tonumber(L,-1); - lua_pop(L,1); - double x,y,z; - popvector3d(L,&x,&y,&z); - btKinematicCharacterController *c = popCharacter(L); - c->setVelocityForTimeInterval(btVector3(x,y,z),interval); - return 0; -} - -extern const luaL_reg bcharactercontroller_m[] = { - {"setwalkdir", bcharsetwalkdirection}, - {"remove", delbcharactercontroller}, - {"getvelocity", bcharactergetvelocity}, - {"setvelocity", bcharactersetvelocity}, - {"setgravity", bcharactersetgravity}, - {"getpos", bcharactergetpos}, - {"setpos", bcharactersetpos}, - {"onground", bcharacteronground}, - {"jump", bcharacterjump}, - {"setfallspeed", bcharactersetfallspeed}, - {"getmaxslope", bcharactergetmaxslope}, - {"setmaxslope", bcharactersetmaxslope}, - {"setvelocityforinterval",bcharactersetvelocityforinterval}, - {0, 0}, -}; - -void bcharactercontroller_register(lua_State* L){// - //printf("Registered bphysbox\n"); - - luaL_newmetatable(L, "phys.charactercontroller");//{phys.characontroller} - lua_newtable(L);//{phys.charcontroller},{} - luaL_register(L,NULL,bcharactercontroller_m);//{phys.charcontroller},{} - lua_setfield(L,-2,"__index");//{phys.charcontroller} - - lua_pop(L,1);// - - lua_getglobal(L,"phys");//{} - lua_pushcfunction(L,newbcharactercontroller);//{},newbcharactercontroller() - lua_setfield(L,-2,"newbcharactercontroller");//{} - - lua_pop(L,1); -} +
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <list>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <btBulletDynamicsCommon.h>
+#include <BulletDynamics/Character/btKinematicCharacterController.h>
+#include <BulletCollision/CollisionDispatch/btGhostObject.h>
+#include "bcharactercontroller.hpp"
+#include <shared/lua_api/common.hpp>
+
+extern btDiscreteDynamicsWorld* World;
+extern std::list<btCollisionObject*> Objects;
+extern std::list<btKinematicCharacterController*> Chars;
+
+//{character} :: btKinematicCharacterController*
+btKinematicCharacterController *popCharacter(lua_State *L){
+ lua_getfield(L,-1,"type");//{char},"type"
+ if(lua_isnil(L,-1)){
+ lua_pushstring(L,"Tried to call a character method on something that had not 'type'");
+ lua_error(L);
+ }
+ const char *s = lua_tostring(L,-1);//{char},"type"
+ if(strcmp(s,"character")!= 0){
+ printf("Tried to pop character when it was not a character!\n");
+ lua_pushstring(L,"Tried to call a character method on a ");
+ lua_pushstring(L,s);
+ lua_concat(L,2);
+ lua_error(L);
+ }
+ lua_getfield(L,-2,"character");//{char},"type",ud_character
+ if(lua_isnil(L,-1)){
+ printf("Failed to get a \"character\" field\n");
+ lua_pushstring(L,"Character object was not set up correctly\n");
+ lua_error(L);
+ }
+ btKinematicCharacterController *c = (btKinematicCharacterController*)lua_touserdata(L,-1);
+ lua_pop(L,3);
+ return c;
+
+}
+/*
+static LBPhysNode* checkisbphysbox(lua_State* L, int index){
+ void* ud = luaL_checkudata(L,index,"phys.physbox");
+ luaL_argcheck(L,ud != NULL, index, "'phys.physbox' expected");
+ return (LBPhysNode*) ud;
+}
+*/
+
+/*
+static LISceneNode* checkismesh(lua_State* L){
+ return checkismesh(L,1);
+}
+*/
+// ud_character :: ({v3 size}, {v3 origin})
+void makenewbcharactercontroller(lua_State* L){
+ printf("Creating new character controller\n");
+ //lua_pushstring(L,"Character controller is totally fucking broken for now\n");
+ //lua_error(L);
+ double px,py,pz; //position
+ double sx,sy,sz; //size
+ //double mass;
+
+ //mass = lua_tonumber(L,-1);//{v3_size},{v3_origin},mass
+ //lua_pop(L,1);//{v3_size},{v3_origin}
+ //printf("Got mass: %f\n",mass);
+
+ popvector3d(L,&px,&py,&pz);//{v3_size}
+ printf("Got position: (%f,%f,%f)\n",px,py,pz);
+ popvector3d(L,&sx,&sy,&sz);//
+
+ btVector3 vshape = btVector3(sx * 0.5f, sy * 0.5f, sz * 0.5f);
+ printf("Got size: (%f,%f,%f)\n",sx,sy,sz);
+ btVector3 pos = btVector3(px,py,pz);
+ btTransform transform = btTransform(btQuaternion(0,0,0,1),pos);
+
+ // Create the shape
+ btConvexShape* cshape = new btBoxShape(vshape);
+
+
+
+ // Add mass
+ //btVector3 localinertia = btVector3(0,0,0);
+ //shape->calculateLocalInertia(mass, localinertia);
+
+ // Create the rigid body object
+ //btRigidBody::btRigidBodyConstructionInfo cinfo = btRigidBody::btRigidBodyConstructionInfo(
+ //mass,
+ //motionstate,
+ //shape,
+ //localinertia
+ //);
+ btPairCachingGhostObject *ghost = new btPairCachingGhostObject();
+ ghost->setWorldTransform(transform);
+ ghost->setCollisionShape(cshape);
+ World->addCollisionObject(ghost,btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
+ //ghost->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
+ printf("Character controller created\n");
+ btKinematicCharacterController *cc = new btKinematicCharacterController(ghost, cshape, 1, btVector3(0,1,0));
+ //cc->setMaxSlope(3.14 / 4.0);
+ //cinfo.m_friction = 0;
+
+ // Add it to the world
+ printf("About to add action\n");
+ World->addAction(cc);
+ printf("Finished adding action\n");
+ //printf("Added rigid body to world: %p\n",World);
+ printf("Added to Chars\n");
+ //Chars.push_back(cc);
+ //Objects.push_back(ghost);
+
+ lua_pushlightuserdata(L,cc);//ud_cc
+}
+
+// char:getvelocity()
+int bcharactergetvelocity(lua_State *L){
+ btKinematicCharacterController *r = popCharacter(L);
+ btVector3 v = r->getLinearVelocity();
+ pushvector3d(L,v.x(),v.y(),v.z());
+ return 1;
+}
+
+// char:setvelocity(v3 vel)
+int bcharactersetvelocity(lua_State *L){
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);
+ btKinematicCharacterController *r = popCharacter(L);
+ r->setLinearVelocity(btVector3(x,y,z));
+ return 0;
+}
+
+// phys.newphysbox(vector3 size, vector3 origin, double mass)
+int newbcharactercontroller(lua_State* L){
+ //printf("Createing bphysbox!\n");
+ //Create it's lua representation
+ makenewbcharactercontroller(L);//ud_cc
+ btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1);//ud_cc
+ lua_pop(L,1);//
+ lua_newtable(L);//{}
+ lua_pushlightuserdata(L,r);//{},ud_cc
+ lua_setfield(L,-2,"character");//{character=ud_cc}
+
+ lua_pushstring(L,"character");
+ lua_setfield(L,-2,"type");//{character=ud_cc,type="character"}
+
+ //Set it's metatable
+ luaL_getmetatable(L, "phys.charactercontroller");//{},{phys.charactercontroller}
+ lua_setmetatable(L, -2);//{cc}
+
+ return 1;
+}
+
+// char:setgravity(v3 gravity)
+int bcharactersetgravity(lua_State *L){
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);
+ btKinematicCharacterController *c = popCharacter(L);
+ c->setGravity(btVector3(x,y,z));
+ return 0;
+}
+
+// char:getpos() :: v3
+int bcharactergetpos(lua_State *L){
+ btKinematicCharacterController *c = popCharacter(L);
+ btVector3 pos = c->getGhostObject()->getWorldTransform().getOrigin();
+ pushvector3d(L,pos.x(),pos.y(),pos.z());
+ return 1;
+}
+
+// char:setpos(v3 pos)
+int bcharactersetpos(lua_State *L){
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);
+ btKinematicCharacterController *c = popCharacter(L);
+ c->warp(btVector3(x,y,z));
+ //btTransform t = c->getGhostObject()->getWorldTransform();
+ //t.setOrigin(btVector3(x,y,z));
+ //c->getGhostObject()->setWorldTransform(t);
+ return 0;
+}
+
+// char:onground()
+int bcharacteronground(lua_State *L){
+ btKinematicCharacterController *c = popCharacter(L);
+ lua_pushboolean(L,c->onGround() == true ? 1 : 0);
+ return 1;
+}
+
+// char:jump(v3 jump)
+int bcharacterjump(lua_State *L){
+ //printf("Jump called\n");
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);
+ btKinematicCharacterController *c = popCharacter(L);
+ //printf("About to jump\n");
+ c->jump(btVector3(x,y,z));
+ //printf("Done jumping\n");
+ return 0;
+}
+
+//{phys.physbox}:delete()
+static int delbcharactercontroller(lua_State* L){//self
+ //printf("Attempting to delete physbox\n");
+ lua_getfield(L,-1,"character");//self,ud_character
+ btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1);//self,ud_character
+ lua_pop(L,2);
+ delete r->getGhostObject();
+ delete r;
+ return 0;
+}
+
+//{char},{v3_dir} ::
+int bcharsetwalkdirection(lua_State *L){
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);//{char}
+ lua_getfield(L,-1,"character");//{char},ud_cc
+ btKinematicCharacterController* cc = (btKinematicCharacterController*)lua_touserdata(L,-1);
+ lua_pop(L,2);
+ cc->setWalkDirection(btVector3(x,y,z));
+ return 0;
+}
+
+// char:setfallspeed(n)
+int bcharactersetfallspeed(lua_State *L){
+ double speed = luaL_optnumber(L,-1,1);
+ printf("Got number: %f\n",speed);
+ lua_pop(L,1);
+ btKinematicCharacterController *c = popCharacter(L);
+ printf("About to set speed\n");
+ c->setFallSpeed(speed);
+ printf("Done setting speed\n");
+ return 0;
+}
+
+// char:getmaxslope()
+int bcharactergetmaxslope(lua_State *L){
+ btKinematicCharacterController *c = popCharacter(L);
+ btScalar s = c->getMaxSlope();
+ lua_pushnumber(L,s);
+ return 1;
+}
+
+// char:setmaxslope(slope)
+int bcharactersetmaxslope(lua_State *L){
+ btScalar s = lua_tonumber(L,-1);
+ lua_pop(L,1);
+ btKinematicCharacterController *c = popCharacter(L);
+ c->setMaxSlope(s);
+ return 0;
+}
+
+// char:setvelocityforinterval(vec3 {velocity},number interval)
+int bcharactersetvelocityforinterval(lua_State *L){
+ double interval = lua_tonumber(L,-1);
+ lua_pop(L,1);
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);
+ btKinematicCharacterController *c = popCharacter(L);
+ c->setVelocityForTimeInterval(btVector3(x,y,z),interval);
+ return 0;
+}
+
+extern const luaL_reg bcharactercontroller_m[] = {
+ {"setwalkdir", bcharsetwalkdirection},
+ {"remove", delbcharactercontroller},
+ {"getvelocity", bcharactergetvelocity},
+ {"setvelocity", bcharactersetvelocity},
+ {"setgravity", bcharactersetgravity},
+ {"getpos", bcharactergetpos},
+ {"setpos", bcharactersetpos},
+ {"onground", bcharacteronground},
+ {"jump", bcharacterjump},
+ {"setfallspeed", bcharactersetfallspeed},
+ {"getmaxslope", bcharactergetmaxslope},
+ {"setmaxslope", bcharactersetmaxslope},
+ {"setvelocityforinterval",bcharactersetvelocityforinterval},
+ {0, 0},
+};
+
+void bcharactercontroller_register(lua_State* L){//
+ //printf("Registered bphysbox\n");
+
+ luaL_newmetatable(L, "phys.charactercontroller");//{phys.characontroller}
+ lua_newtable(L);//{phys.charcontroller},{}
+ luaL_register(L,NULL,bcharactercontroller_m);//{phys.charcontroller},{}
+ lua_setfield(L,-2,"__index");//{phys.charcontroller}
+
+ lua_pop(L,1);//
+
+ lua_getglobal(L,"phys");//{}
+ lua_pushcfunction(L,newbcharactercontroller);//{},newbcharactercontroller()
+ lua_setfield(L,-2,"newbcharactercontroller");//{}
+
+ lua_pop(L,1);
+}
diff --git a/src/shared/lua_api/phys/bcharactercontroller.hpp b/src/shared/lua_api/phys/bcharactercontroller.hpp index 1c920de..02e5afb 100644 --- a/src/shared/lua_api/phys/bcharactercontroller.hpp +++ b/src/shared/lua_api/phys/bcharactercontroller.hpp @@ -1,12 +1,12 @@ -#include <stdio.h> -#include <stdlib.h> -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} -#include <irrlicht.h> - -void bcharactercontroller_register(lua_State* L); -void makenewbcharactercontroller(lua_State* L); -extern const luaL_reg bcharactercontroller_m[]; +#include <stdio.h>
+#include <stdlib.h>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+
+void bcharactercontroller_register(lua_State* L);
+void makenewbcharactercontroller(lua_State* L);
+extern const luaL_reg bcharactercontroller_m[];
diff --git a/src/shared/lua_api/phys/bcollider.cpp b/src/shared/lua_api/phys/bcollider.cpp index 0a87af3..91c34fd 100644 --- a/src/shared/lua_api/phys/bcollider.cpp +++ b/src/shared/lua_api/phys/bcollider.cpp @@ -1,89 +1,89 @@ -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} -#include <btBulletDynamicsCommon.h> -#include <shared/lua_api/common.hpp> -#include "bcollider.hpp" - - -/*Collider things from lua have the form of: -{ - type = "ghost" | "multi" | "rigidbody" | "softbody" - collider = ud_btCollisionObject, - node = ud_ISceneNode, --Optional, on client -} -*/ -btCollisionObject* popCollider(lua_State *L){ - lua_getfield(L,-1,"collider"); - btCollisionObject *r = (btCollisionObject*)lua_touserdata(L,-1); - lua_pop(L,2); - return r; -} - -/*** -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 collider:activate() -*/ -//collider:activate() -int activate(lua_State *L){ - btCollisionObject *r = popCollider(L); - - r->activate(true); - - return 0; -} - -//collider:getfriction() -int getfriction(lua_State *L){ - btCollisionObject *r = popCollider(L); - double fric = r->getFriction(); - lua_pushnumber(L, fric); - return 1; -} - -//collider:setfriction(number) -int setfriction(lua_State *L){ - double friction = lua_tonumber(L,-1); - lua_pop(L,1); - btCollisionObject *r = popCollider(L); - r->setFriction(friction); - 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; -} - -extern const luaL_reg bcollider_m[] = { - {"activate", activate}, - {"getpos", getpos}, - {"setpos", setpos}, - {"getfriction", getfriction}, - {"setfriction", setfriction}, - {NULL, NULL} -}; +extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <btBulletDynamicsCommon.h>
+#include <shared/lua_api/common.hpp>
+#include "bcollider.hpp"
+
+
+/*Collider things from lua have the form of:
+{
+ type = "ghost" | "multi" | "rigidbody" | "softbody"
+ collider = ud_btCollisionObject,
+ node = ud_ISceneNode, --Optional, on client
+}
+*/
+btCollisionObject* popCollider(lua_State *L){
+ lua_getfield(L,-1,"collider");
+ btCollisionObject *r = (btCollisionObject*)lua_touserdata(L,-1);
+ lua_pop(L,2);
+ return r;
+}
+
+/***
+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 collider:activate()
+*/
+//collider:activate()
+int activate(lua_State *L){
+ btCollisionObject *r = popCollider(L);
+
+ r->activate(true);
+
+ return 0;
+}
+
+//collider:getfriction()
+int getfriction(lua_State *L){
+ btCollisionObject *r = popCollider(L);
+ double fric = r->getFriction();
+ lua_pushnumber(L, fric);
+ return 1;
+}
+
+//collider:setfriction(number)
+int setfriction(lua_State *L){
+ double friction = lua_tonumber(L,-1);
+ lua_pop(L,1);
+ btCollisionObject *r = popCollider(L);
+ r->setFriction(friction);
+ 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;
+}
+
+extern const luaL_reg bcollider_m[] = {
+ {"activate", activate},
+ {"getpos", getpos},
+ {"setpos", setpos},
+ {"getfriction", getfriction},
+ {"setfriction", setfriction},
+ {NULL, NULL}
+};
diff --git a/src/shared/lua_api/phys/bcollider.hpp b/src/shared/lua_api/phys/bcollider.hpp index 3882df6..f5164b7 100644 --- a/src/shared/lua_api/phys/bcollider.hpp +++ b/src/shared/lua_api/phys/bcollider.hpp @@ -1,7 +1,7 @@ -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} - -extern const luaL_reg bcollider_m[]; +extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+
+extern const luaL_reg bcollider_m[];
diff --git a/src/shared/lua_api/phys/bghostobject.cpp b/src/shared/lua_api/phys/bghostobject.cpp index 8174b21..63f790c 100644 --- a/src/shared/lua_api/phys/bghostobject.cpp +++ b/src/shared/lua_api/phys/bghostobject.cpp @@ -1,185 +1,236 @@ -#include <stdio.h> -#include <stdlib.h> -#include <list> -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} -#include <btBulletDynamicsCommon.h> -#include "bghostobject.hpp" -#include <shared/lua_api/common.hpp> - -extern btDiscreteDynamicsWorld* World; -extern std::list<btCollisionObject*> Objects; -//extern std::list<btGhostObject*> Ghosts; - -/* -static LBPhysNode* checkisbphysbox(lua_State* L, int index){ - void* ud = luaL_checkudata(L,index,"phys.physbox"); - luaL_argcheck(L,ud != NULL, index, "'phys.physbox' expected"); - return (LBPhysNode*) ud; -} -*/ - -/* -static LISceneNode* checkismesh(lua_State* L){ - return checkismesh(L,1); -} -*/ -// ud_btGhostObject :: ({v3 size}, {v3 origin}) -void makeghostobject(lua_State* L){ - double px,py,pz; //position - double sx,sy,sz; //size - - popvector3d(L,&px,&py,&pz);//{v3_size} - //printf("Got position: (%f,%f,%f)\n",px,py,pz); - popvector3d(L,&sx,&sy,&sz);// - - btVector3 vshape = btVector3(sx * 0.5f, sy * 0.5f, sz * 0.5f); - //printf("Got size: (%f,%f,%f)\n",sx,sy,sz); - btVector3 pos = btVector3(px,py,pz); - - // Set the initial position of the object - btTransform transform = btTransform(btQuaternion(0,0,0,1),pos); - //transform.setIdentity(); - //transform.setOrigin(pos); - - // Create the shape - btCollisionShape* shape = new btBoxShape(vshape); - if(!shape){ - //printf("no shape\n"); - } - - // Add mass - btVector3 localinertia = btVector3(0,0,0); - shape->calculateLocalInertia(1, localinertia); - - //cinfo.m_friction = 0; - btGhostObject *ghost = new btGhostObject(); - ghost->setCollisionShape(shape); - ghost->setWorldTransform(transform); - //ghost->setCollisionFlags( - //btCollisionObject::CollisionFlags::CF_NO_CONTACT_RESPONSE | - //btCollisionObject::CollisionFlags::CF_KINEMATIC_OBJECT - //); - World->addCollisionObject(ghost, btBroadphaseProxy::SensorTrigger, btBroadphaseProxy::AllFilter & ~btBroadphaseProxy::SensorTrigger); - - //printf("Added rigid body to world: %p\n",World); - //Objects.push_back(ghost); - - lua_pushlightuserdata(L,ghost);//ud_ghost -} - -// phys.newghostobject(vector3 size, vector3 origin) -int newghostobject(lua_State* L){ - //printf("Createing bphysbox!\n"); - //Create it's lua representation - makeghostobject(L);//ud_btGhostObject - btGhostObject* ghost = (btGhostObject*)lua_touserdata(L,-1); - lua_pop(L,1); - lua_newtable(L);//{} - lua_pushlightuserdata(L,ghost);//ud_btGhostObject - lua_setfield(L,-2,"collider");//{} - - //Set it's metatable - luaL_getmetatable(L, "phys.ghost");//{},{phys.ghost} - lua_setmetatable(L, -2);//{} - - return 1; -} - -//{phys.physbox}:delete() -static int delbghostobject(lua_State* L){//self - //printf("Attempting to delete physbox\n"); - lua_getfield(L,-1,"collider");//self,ud_rigidbody - btGhostObject* r = (btGhostObject*)lua_touserdata(L,-1);//self,ud_rigidbody - delete r->getCollisionShape(); - delete r; - - return 0; -} - -// physbox:setpos({v3 pos}) -static int bghostsetpos(lua_State *L){//self,{v3 pos} - double nx,ny,nz; - popvector3d(L,&nx,&ny,&nz);//self - - lua_getfield(L,-1,"collider");//self,ud_ghost - btGhostObject *ghost = (btGhostObject*)lua_touserdata(L,-1);//self - btTransform bt = ghost->getWorldTransform(); - - btVector3 to = btVector3(nx,ny,nz); - bt.setOrigin(to); - ghost->setWorldTransform(bt); - ghost->activate(); - - lua_pop(L,1);// - return 0; -} - -// {v3 pos} :: physbox:getpos() -static int bghostgetpos(lua_State *L){//self - //printf("Physics box set pos called\n"); - lua_getfield(L,-1,"collider");//self,ud_ghost - btGhostObject* i = (btGhostObject*)lua_touserdata(L,-1);//self,ud_ghost - btTransform bt = i->getWorldTransform(); - btVector3 bv = bt.getOrigin(); - lua_pop(L,2);// - pushvector3d(L,bv.x(),bv.y(),bv.z());//{} - - return 1; -} - -//ghost:getoverlapping() -int bghostoverlapping(lua_State *L){ - lua_getfield(L,-1,"collider");//{ghost} - btGhostObject *ghost = (btGhostObject*)lua_touserdata(L,-1);//{ghost},ud_ghost - lua_pop(L,2);// - lua_newtable(L);//{} - btAlignedObjectArray<btCollisionObject *> ob = ghost->getOverlappingPairs(); - printf("Getting %d overlapping object\n",ob.size()); - for(int i = 0; i < ob.size(); i++){ - printf("Looking at object %d\n",i); - btCollisionObject *co = ob[i]; - lua_getglobal(L,"phys");//{},{phys} - lua_getfield(L,-1,"colliders");//{},{phys},{phys.colliders} - lua_pushnumber(L,i+1);//}{},{phys},{phys.colliders},i - lua_pushlightuserdata(L,co);//{},{phys},{phys.colliders},i,ud_co - lua_gettable(L,-3);//{},{phys},{phys.colliders},i,{collider=ud_co} - if(lua_isnil(L,-1)){ - printf("Failed to find object of collider %p\n", (void*)co); - lua_pushstring(L,"Failed to find collider we are overlapping"); - lua_error(L); - } - lua_settable(L,-5);//{i={collider=co}},{phys},{phys.colliders} - lua_pop(L,2);//{i={...}} - } - printf("Finished adding %d overlapping objects to array...\n",(int)lua_objlen(L,-1)); - return 1; -} - -static const luaL_reg bghost_m[] = { - {"getpos", bghostgetpos}, - {"setpos", bghostsetpos}, - {"getoverlapping", bghostoverlapping}, - {"delete", delbghostobject}, - {0, 0}, -}; - -void bghostobject_register(lua_State* L){// - //printf("Registered bphysbox\n"); - - luaL_newmetatable(L, "phys.ghost");//{phys.physbox} - lua_newtable(L);//{phys.physbox},{} - luaL_register(L,NULL,bghost_m);//{phys.physbox},{} - lua_setfield(L,-2,"__index");//{phys.physbox} - - lua_pop(L,1);// - - lua_getglobal(L,"phys");//{} - lua_pushcfunction(L,newghostobject);//{},newghostobject() - lua_setfield(L,-2,"newghostbox");//{} - - lua_pop(L,1); -} +#include <stdio.h>
+#include <stdlib.h>
+#include <list>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <btBulletDynamicsCommon.h>
+#include "bghostobject.hpp"
+#include <shared/lua_api/common.hpp>
+
+extern btDiscreteDynamicsWorld* World;
+extern std::list<btCollisionObject*> Objects;
+//extern std::list<btGhostObject*> Ghosts;
+
+/*
+static LBPhysNode* checkisbphysbox(lua_State* L, int index){
+ void* ud = luaL_checkudata(L,index,"phys.physbox");
+ luaL_argcheck(L,ud != NULL, index, "'phys.physbox' expected");
+ return (LBPhysNode*) ud;
+}
+*/
+
+/*
+static LISceneNode* checkismesh(lua_State* L){
+ return checkismesh(L,1);
+}
+*/
+// ud_btGhostObject :: ({v3 size}, {v3 origin})
+void makeghostobject(lua_State* L){
+ double px,py,pz; //position
+ double sx,sy,sz; //size
+
+ popvector3d(L,&px,&py,&pz);//{v3_size}
+ //printf("Got position: (%f,%f,%f)\n",px,py,pz);
+ popvector3d(L,&sx,&sy,&sz);//
+
+ btVector3 vshape = btVector3(sx * 0.5f, sy * 0.5f, sz * 0.5f);
+ //printf("Got size: (%f,%f,%f)\n",sx,sy,sz);
+ btVector3 pos = btVector3(px,py,pz);
+
+ // Set the initial position of the object
+ btTransform transform = btTransform(btQuaternion(0,0,0,1),pos);
+ //transform.setIdentity();
+ //transform.setOrigin(pos);
+
+ // Create the shape
+ btCollisionShape* shape = new btBoxShape(vshape);
+ if(!shape){
+ //printf("no shape\n");
+ }
+
+ // Add mass
+ btVector3 localinertia = btVector3(0,0,0);
+ shape->calculateLocalInertia(1, localinertia);
+
+ //cinfo.m_friction = 0;
+ btGhostObject *ghost = new btGhostObject();
+ ghost->setCollisionShape(shape);
+ ghost->setWorldTransform(transform);
+ //ghost->setCollisionFlags(
+ //btCollisionObject::CollisionFlags::CF_NO_CONTACT_RESPONSE |
+ //btCollisionObject::CollisionFlags::CF_KINEMATIC_OBJECT
+ //);
+ World->addCollisionObject(ghost, btBroadphaseProxy::SensorTrigger, btBroadphaseProxy::AllFilter & ~btBroadphaseProxy::SensorTrigger);
+
+ //printf("Added rigid body to world: %p\n",World);
+ //Objects.push_back(ghost);
+
+ lua_pushlightuserdata(L,ghost);//ud_ghost
+}
+
+// phys.newghostbox(vector3 size, vector3 origin)
+int newghostobject(lua_State* L){
+ //printf("Createing bphysbox!\n");
+ //Create it's lua representation
+ makeghostobject(L);//ud_btGhostObject
+ btGhostObject* ghost = (btGhostObject*)lua_touserdata(L,-1);
+ lua_pop(L,1);
+ lua_newtable(L);//{}
+ lua_pushlightuserdata(L,ghost);//ud_btGhostObject
+ lua_setfield(L,-2,"collider");//{}
+
+ //Set it's metatable
+ luaL_getmetatable(L, "phys.ghost");//{},{phys.ghost}
+ lua_setmetatable(L, -2);//{}
+
+ return 1;
+}
+
+//ghost:sweep(shape, v3 start, v3 end)
+int bghostconvexsweep(lua_State *L){
+ double sx,sy,sz,ex,ey,ez;
+ popvector3d(L,&ex,&ey,&ez);//self,shape,v3start
+ popvector3d(L,&sx,&sy,&sz);//self,shape
+ lua_getfield(L,-1,"shape");//self,shape,ud_shape
+ btBoxShape *cs = (btBoxShape*)lua_touserdata(L,-1);//self,shape,ud_shape
+ lua_pop(L,2);//self
+ lua_getfield(L,-1,"collider");
+ btGhostObject* r = (btGhostObject*)lua_touserdata(L,-1);//self,ud_rigidbody
+ lua_pop(L,2);//
+ //btCollisionShape *cs = r->getCollisionShape();
+ btTransform ft,tt;
+ ft = btTransform(btQuaternion(0,0,0),btVector3(sx,sy,sz));
+ tt = btTransform(btQuaternion(0,0,0),btVector3(ex,ey,ez));
+ btCollisionWorld::ClosestConvexResultCallback *cb = new btCollisionWorld::ClosestConvexResultCallback(ft.getOrigin(),tt.getOrigin());
+ r->convexSweepTest(cs,ft,tt,*cb,0.f);
+ btVector3 hw, hn;
+ hw = cb->m_hitPointWorld;
+ hn = cb->m_hitNormalWorld;
+ btCollisionObject *co = cb->m_hitCollisionObject;
+
+ lua_newtable(L);//{}
+ lua_pushboolean(L,cb->hasHit() ? 1 : 0);
+ lua_setfield(L,-2,"hit");
+ pushvector3d(L,hw.x(),hw.y(),hw.z());
+ lua_setfield(L,-2,"pos");
+ pushvector3d(L,hn.x(),hn.y(),hn.z());
+ lua_setfield(L,-2,"normal");
+ lua_getglobal(L,"phys");//{},{phys}
+ lua_getfield(L,-1,"colliders");//{},{phys},{phys.colliders}
+ lua_pushlightuserdata(L,co);//{},{phys},{phys.colliders},ud_collisionobject
+ lua_gettable(L,-2);//{},{phys},{phys.colliders},ud_collisionobject,{rb} or nil
+ lua_setfield(L,-5,"what");//{},{phys},{phys.colliders},ud_collisionobject
+ lua_pop(L,3);//{}
+
+ delete cb;
+ return 1;
+}
+
+//{phys.physbox}:delete()
+static int delbghostobject(lua_State* L){//self
+ //printf("Attempting to delete physbox\n");
+ lua_getfield(L,-1,"collider");//self,ud_rigidbody
+ btGhostObject* r = (btGhostObject*)lua_touserdata(L,-1);//self,ud_rigidbody
+ delete r->getCollisionShape();
+ delete r;
+
+ return 0;
+}
+
+// physbox:setpos({v3 pos})
+static int bghostsetpos(lua_State *L){//self,{v3 pos}
+ double nx,ny,nz;
+ popvector3d(L,&nx,&ny,&nz);//self
+
+ lua_getfield(L,-1,"collider");//self,ud_ghost
+ btGhostObject *ghost = (btGhostObject*)lua_touserdata(L,-1);//self
+ btTransform bt = ghost->getWorldTransform();
+
+ btVector3 to = btVector3(nx,ny,nz);
+ bt.setOrigin(to);
+ ghost->setWorldTransform(bt);
+ ghost->activate();
+
+ lua_pop(L,1);//
+ return 0;
+}
+
+// {v3 pos} :: physbox:getpos()
+static int bghostgetpos(lua_State *L){//self
+ //printf("Physics box set pos called\n");
+ lua_getfield(L,-1,"collider");//self,ud_ghost
+ btGhostObject* i = (btGhostObject*)lua_touserdata(L,-1);//self,ud_ghost
+ btTransform bt = i->getWorldTransform();
+ btVector3 bv = bt.getOrigin();
+ lua_pop(L,2);//
+ pushvector3d(L,bv.x(),bv.y(),bv.z());//{}
+
+ return 1;
+}
+
+//ghost:getoverlapping()
+int bghostoverlapping(lua_State *L){
+ lua_getfield(L,-1,"collider");//{ghost}
+ btGhostObject *ghost = (btGhostObject*)lua_touserdata(L,-1);//{ghost},ud_ghost
+ lua_pop(L,2);//
+ lua_newtable(L);//{}
+ btAlignedObjectArray<btCollisionObject *> ob = ghost->getOverlappingPairs();
+ //printf("Getting %d overlapping object\n",ob.size());
+ for(int i = 0; i < ob.size(); i++){
+ //printf("Looking at object %d\n",i);
+ btCollisionObject *co = ob[i];
+ lua_getglobal(L,"phys");//{},{phys}
+ lua_getfield(L,-1,"colliders");//{},{phys},{phys.colliders}
+ lua_pushnumber(L,i+1);//}{},{phys},{phys.colliders},i
+ lua_pushlightuserdata(L,co);//{},{phys},{phys.colliders},i,ud_co
+ lua_gettable(L,-3);//{},{phys},{phys.colliders},i,{collider=ud_co}
+ if(lua_isnil(L,-1)){
+ printf("Failed to find object of collider %p\n", (void*)co);
+ lua_pushstring(L,"Failed to find collider we are overlapping");
+ lua_error(L);
+ }
+ lua_settable(L,-5);//{i={collider=co}},{phys},{phys.colliders}
+ lua_pop(L,2);//{i={...}}
+ }
+ //printf("Finished adding %d overlapping objects to array...\n",(int)lua_objlen(L,-1));
+ return 1;
+}
+
+int bghostnumoverlapping(lua_State *L){
+ lua_getfield(L,-1,"collider");//{ghost}
+ btGhostObject *ghost = (btGhostObject*)lua_touserdata(L,-1);//{ghost},ud_ghost
+ lua_pop(L,2);//
+ btAlignedObjectArray<btCollisionObject *> ob = ghost->getOverlappingPairs();
+ lua_pushnumber(L,ob.size());
+ return 1;
+}
+
+static const luaL_reg bghost_m[] = {
+ {"getpos", bghostgetpos},
+ {"setpos", bghostsetpos},
+ {"getoverlapping", bghostoverlapping},
+ {"getnumoverlapping", bghostnumoverlapping},
+ {"shapecast", bghostconvexsweep},
+ {"delete", delbghostobject},
+ {0, 0},
+};
+
+void bghostobject_register(lua_State* L){//
+ //printf("Registered bphysbox\n");
+
+ luaL_newmetatable(L, "phys.ghost");//{phys.physbox}
+ lua_newtable(L);//{phys.physbox},{}
+ luaL_register(L,NULL,bghost_m);//{phys.physbox},{}
+ lua_setfield(L,-2,"__index");//{phys.physbox}
+
+ lua_pop(L,1);//
+
+ lua_getglobal(L,"phys");//{}
+ lua_pushcfunction(L,newghostobject);//{},newghostobject()
+ lua_setfield(L,-2,"newghostbox");//{}
+
+ lua_pop(L,1);
+}
diff --git a/src/shared/lua_api/phys/bghostobject.hpp b/src/shared/lua_api/phys/bghostobject.hpp index 5c96f7e..547aacb 100644 --- a/src/shared/lua_api/phys/bghostobject.hpp +++ b/src/shared/lua_api/phys/bghostobject.hpp @@ -1,13 +1,13 @@ - -#include <stdio.h> -#include <stdlib.h> -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} -#include <irrlicht.h> -#include <BulletCollision/CollisionDispatch/btGhostObject.h> - -void bghostobject_register(lua_State* L); -void makeghostobject(lua_State* L); +
+#include <stdio.h>
+#include <stdlib.h>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+#include <BulletCollision/CollisionDispatch/btGhostObject.h>
+
+void bghostobject_register(lua_State* L);
+void makeghostobject(lua_State* L);
diff --git a/src/shared/lua_api/phys/bhingeconstraint.cpp b/src/shared/lua_api/phys/bhingeconstraint.cpp index a8c6567..5045cb7 100644 --- a/src/shared/lua_api/phys/bhingeconstraint.cpp +++ b/src/shared/lua_api/phys/bhingeconstraint.cpp @@ -1,61 +1,61 @@ - -#include <stdio.h> -#include <stdlib.h> -#include <list> -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} -#include <shared/lua_api/common.hpp> -#include <btBulletDynamicsCommon.h> -#include <shared/lua_api/phys/bhingeconstraint.hpp> - -extern btDiscreteDynamicsWorld* World; -extern std::list<btRigidBody*> Objects; - -//newhingeconstraint(phys1,v3 axis, refrencephys1) -int newbhingeconstraint(lua_State *L){ - bool phys1 = lua_toboolean(L,-1) == 1; - lua_pop(L,1); - - double x,y,z; - popvector3d(L,&x,&y,&z); - - lua_getfield(L,-1,"rigidbody"); - btRigidBody *p1 = (btRigidBody*)lua_touserdata(L,-1); - btTransform frame = p1->getCenterOfMassTransform(); - frame.setRotation(btQuaternion(x,y,z,0)); - lua_pop(L,2); - - btHingeConstraint(*p1,frame,phys1); - printf("Done makeing new hinge constraint\n"); - - return 0; -} - -static const luaL_reg hingeconstraint_m[] = { -// {"delete", delbphysbox},//client side delete needs to delete the visual representation - {0, 0}, -}; - -void bhingeconstraint_register(lua_State* L){ - lua_getglobal(L,"phys");//{} - lua_pushcfunction(L,newbhingeconstraint);//{},newhingeconstraint() - lua_setfield(L,-2,"newhingeconstraint");//{} - - lua_pop(L,1);// - - luaL_newmetatable(L,"phys.hingeconstraint"); - lua_newtable(L);//phys.hingeconstraint,{} - luaL_register(L,NULL,hingeconstraint_m); - //luaL_register(L,NULL,cbphysbox_m);//phys.hingeconstraint,{} - lua_setfield(L,-2,"__index");//phys.physbox - - lua_pop(L,1); - - //printf("When registering physbox, new() is %p\n",newcbphysbox); - //printf("setpos is %p\n",cbphyssetpos); - - lua_pop(L,1); -} +
+#include <stdio.h>
+#include <stdlib.h>
+#include <list>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <shared/lua_api/common.hpp>
+#include <btBulletDynamicsCommon.h>
+#include <shared/lua_api/phys/bhingeconstraint.hpp>
+
+extern btDiscreteDynamicsWorld* World;
+extern std::list<btRigidBody*> Objects;
+
+//newhingeconstraint(phys1,v3 axis, refrencephys1)
+int newbhingeconstraint(lua_State *L){
+ bool phys1 = lua_toboolean(L,-1) == 1;
+ lua_pop(L,1);
+
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);
+
+ lua_getfield(L,-1,"rigidbody");
+ btRigidBody *p1 = (btRigidBody*)lua_touserdata(L,-1);
+ btTransform frame = p1->getCenterOfMassTransform();
+ frame.setRotation(btQuaternion(x,y,z,0));
+ lua_pop(L,2);
+
+ btHingeConstraint(*p1,frame,phys1);
+ printf("Done makeing new hinge constraint\n");
+
+ return 0;
+}
+
+static const luaL_reg hingeconstraint_m[] = {
+// {"delete", delbphysbox},//client side delete needs to delete the visual representation
+ {0, 0},
+};
+
+void bhingeconstraint_register(lua_State* L){
+ lua_getglobal(L,"phys");//{}
+ lua_pushcfunction(L,newbhingeconstraint);//{},newhingeconstraint()
+ lua_setfield(L,-2,"newhingeconstraint");//{}
+
+ lua_pop(L,1);//
+
+ luaL_newmetatable(L,"phys.hingeconstraint");
+ lua_newtable(L);//phys.hingeconstraint,{}
+ luaL_register(L,NULL,hingeconstraint_m);
+ //luaL_register(L,NULL,cbphysbox_m);//phys.hingeconstraint,{}
+ lua_setfield(L,-2,"__index");//phys.physbox
+
+ lua_pop(L,1);
+
+ //printf("When registering physbox, new() is %p\n",newcbphysbox);
+ //printf("setpos is %p\n",cbphyssetpos);
+
+ lua_pop(L,1);
+}
diff --git a/src/shared/lua_api/phys/bhingeconstraint.hpp b/src/shared/lua_api/phys/bhingeconstraint.hpp index 4e8d72c..88b33cb 100644 --- a/src/shared/lua_api/phys/bhingeconstraint.hpp +++ b/src/shared/lua_api/phys/bhingeconstraint.hpp @@ -1,2 +1,2 @@ - -void bhingeconstraint_register(lua_State* L); +
+void bhingeconstraint_register(lua_State* L);
diff --git a/src/shared/lua_api/phys/bphysbox.cpp b/src/shared/lua_api/phys/bphysbox.cpp index 4564c6e..569f3f9 100644 --- a/src/shared/lua_api/phys/bphysbox.cpp +++ b/src/shared/lua_api/phys/bphysbox.cpp @@ -9,6 +9,7 @@ extern "C" { } #include <btBulletDynamicsCommon.h> #include "bphysbox.hpp" +#include "bphysgeneric.hpp" #include <shared/lua_api/common.hpp> extern btDiscreteDynamicsWorld* World; @@ -40,7 +41,7 @@ void makenewbphysbox(lua_State* L){ //printf("Got position: (%f,%f,%f)\n",px,py,pz); popvector3d(L,&sx,&sy,&sz);// - btVector3 vshape = btVector3(sx * 0.5f, sy * 0.5f, sz * 0.5f); + btVector3 vshape = btVector3(sx, sy, sz); //printf("Got size: (%f,%f,%f)\n",sx,sy,sz); btVector3 pos = btVector3(px,py,pz); @@ -168,6 +169,7 @@ void bphysbox_register(lua_State* L){// luaL_newmetatable(L, "phys.physbox");//{phys.physbox} lua_newtable(L);//{phys.physbox},{} luaL_register(L,NULL,bphysbox_m);//{phys.physbox},{} + luaL_register(L,NULL,brigidbody_m); lua_setfield(L,-2,"__index");//{phys.physbox} lua_pop(L,1);// diff --git a/src/shared/lua_api/phys/bphysmodel.cpp b/src/shared/lua_api/phys/bphysmodel.cpp index d56adb5..0c8e3bf 100644 --- a/src/shared/lua_api/phys/bphysmodel.cpp +++ b/src/shared/lua_api/phys/bphysmodel.cpp @@ -81,6 +81,7 @@ void makebphysmodel(lua_State *L){ float v2 = vs[1]; float v3 = vs[2]; vertexes[i] = btVector3(v1,v2,v3); + printf("Adding vertex %lld at (%f,%f,%f)\n",i,v1,v2,v3); } //printf("Finished finding or adding vertexes\n"); for(size_t i = 0; i < attrib.num_faces - 1; i+= 3){ //0 - y to num_faces - 1 @@ -92,13 +93,15 @@ void makebphysmodel(lua_State *L){ v1 = vertexes[i1.v_idx]; v2 = vertexes[i2.v_idx]; v3 = vertexes[i3.v_idx]; - trimesh->addTriangle(vertexes[i1.v_idx],vertexes[i2.v_idx],vertexes[i3.v_idx],true);//Some triangles are "the wrong way round", + trimesh->addTriangle(vertexes[i1.v_idx],vertexes[i2.v_idx],vertexes[i3.v_idx],false);//Some triangles are "the wrong way round", + printf("Adding triangle:(%d,%d,%d)\n",i1.v_idx,i2.v_idx,i3.v_idx); //trimesh->addTriangle(vertexes[i3.v_idx],vertexes[i2.v_idx],vertexes[i1.v_idx],true);//double-side all triangles } //printf("Finished adding triangle indicies\n"); //printf("Done building trimesh\n"); - btGImpactShapeInterface *shape = new btGImpactMeshShape(trimesh); - shape->updateBound(); + //btGImpactShapeInterface *shape = new btGImpactMeshShape(trimesh); + btConvexTriangleMeshShape *shape = new btConvexTriangleMeshShape(trimesh); + //shape->updateBound(); //btCollisionShape *shape = new btBvhTriangleMeshShape(trimesh,true); btTransform tr; tr.setIdentity(); diff --git a/src/shared/lua_api/phys/bshape.cpp b/src/shared/lua_api/phys/bshape.cpp index 5533b42..f7d73d3 100644 --- a/src/shared/lua_api/phys/bshape.cpp +++ b/src/shared/lua_api/phys/bshape.cpp @@ -1,52 +1,52 @@ -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} -#include <btBulletDynamicsCommon.h> -#include <btBulletCollisionCommon.h> -#include <shared/lua_api/common.hpp> - -extern btDiscreteDynamicsWorld* World; - -//phys.newboxshape({ax,ay,az}) -static int newboxshape(lua_State* L){ - double ax,ay,az; - popvector3d(L,&ax,&ay,&az); - ax *= 0.5; - ay *= 0.5; - az *= 0.5; - btBoxShape *bs = new btBoxShape(btVector3(ax,ay,az)); - printf("Created shape: %p\n",(void*)bs); - lua_newtable(L);//{} - lua_pushlightuserdata(L,bs);//{},ud_bs - lua_setfield(L,-2,"shape");//{} - luaL_getmetatable(L,"phys.shape");//{},{m_shape} - lua_setmetatable(L,-2);//{} - - return 1; -} - -static const luaL_reg bshape_f[] = { - {"newboxshape", newboxshape}, - {0,0}, -}; - -static const luaL_reg bshape_m[] = { - {0,0}, -}; - -int bshape_register(lua_State* L){ - - luaL_newmetatable(L, "phys.shape");//{m_physshape} - lua_newtable(L);//{m_physshape},{} - luaL_register(L,NULL,bshape_m); - lua_setfield(L,-2,"__index");//{m_physshape} - lua_pop(L,1);// - - lua_getglobal(L,"phys");//{} - luaL_register(L,NULL,bshape_f);//{} - lua_pop(L,1);// - - return 0; -} +extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <btBulletDynamicsCommon.h>
+#include <btBulletCollisionCommon.h>
+#include <shared/lua_api/common.hpp>
+
+extern btDiscreteDynamicsWorld* World;
+
+//phys.newboxshape({ax,ay,az})
+static int newboxshape(lua_State* L){
+ double ax,ay,az;
+ popvector3d(L,&ax,&ay,&az);
+ ax *= 0.5;
+ ay *= 0.5;
+ az *= 0.5;
+ btBoxShape *bs = new btBoxShape(btVector3(ax,ay,az));
+ printf("Created shape: %p\n",(void*)bs);
+ lua_newtable(L);//{}
+ lua_pushlightuserdata(L,bs);//{},ud_bs
+ lua_setfield(L,-2,"shape");//{}
+ luaL_getmetatable(L,"phys.shape");//{},{m_shape}
+ lua_setmetatable(L,-2);//{}
+
+ return 1;
+}
+
+static const luaL_reg bshape_f[] = {
+ {"newboxshape", newboxshape},
+ {0,0},
+};
+
+static const luaL_reg bshape_m[] = {
+ {0,0},
+};
+
+int bshape_register(lua_State* L){
+
+ luaL_newmetatable(L, "phys.shape");//{m_physshape}
+ lua_newtable(L);//{m_physshape},{}
+ luaL_register(L,NULL,bshape_m);
+ lua_setfield(L,-2,"__index");//{m_physshape}
+ lua_pop(L,1);//
+
+ lua_getglobal(L,"phys");//{}
+ luaL_register(L,NULL,bshape_f);//{}
+ lua_pop(L,1);//
+
+ return 0;
+}
diff --git a/src/shared/lua_api/phys/bshape.hpp b/src/shared/lua_api/phys/bshape.hpp index ef3c48a..633e7a4 100644 --- a/src/shared/lua_api/phys/bshape.hpp +++ b/src/shared/lua_api/phys/bshape.hpp @@ -1,11 +1,11 @@ -#ifndef _BSHAPE_HPP_ -#include <stdio.h> -#include <stdlib.h> -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} - -int bshape_register(lua_State* L); -#endif +#ifndef _BSHAPE_HPP_
+#include <stdio.h>
+#include <stdlib.h>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+
+int bshape_register(lua_State* L);
+#endif
diff --git a/src/shared/lua_api/phys/btaction.cpp b/src/shared/lua_api/phys/btaction.cpp index afd2371..4f6a184 100644 --- a/src/shared/lua_api/phys/btaction.cpp +++ b/src/shared/lua_api/phys/btaction.cpp @@ -1,62 +1,63 @@ -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} -#include <btBulletDynamicsCommon.h> -#include <btBulletCollisionCommon.h> - -#include <shared/lua_api/common.hpp> - -extern btDiscreteDynamicsWorld* World; - -struct BActionItem: public btActionInterface{ - int ref; - lua_State *L; - void updateAction(btCollisionWorld *world, btScalar delta){ - lua_rawgeti(this->L,LUA_REGISTRYINDEX,this->ref);//{} - pusherrorfunc(this->L);//{},errfunc() - lua_getfield(this->L,-2,"action");//{},errfunc(),action() - if(lua_isnil(this->L,-1)){//no .action method - lua_pop(this->L,3); - return; - } - lua_pushvalue(this->L,-3);//{},errfunc(),action(),{} - lua_pushlightuserdata(this->L,world);//{},errfunc(),action(),{},ud_world - lua_pushnumber(this->L,delta);//{},errfunc(),action(),{},ud_world,delta - lua_pcall(this->L,3,0,-4);//{},errfunc() - //printf("error:%d\n",err); - lua_pop(this->L,2); - return; - } - void debugDraw(btIDebugDraw *d){ - //no debug draw I guess - } -}; - -int makeaction(lua_State *L){ - lua_newtable(L);//{} - int r = luaL_ref(L,LUA_REGISTRYINDEX); - lua_rawgeti(L,LUA_REGISTRYINDEX,r); - BActionItem *a = new BActionItem(); - a->ref = r; - a->L = L; - World->addAction(a); - lua_pushlightuserdata(L,a);//{},ud_action - lua_setfield(L,-2,"action");//{} - return 1; -} - -int newaction(lua_State *L){ - lua_newtable(L);//{} - return 0; -} - -int baction_register(lua_State *L){ - lua_getglobal(L,"phys"); - lua_pushcfunction(L,makeaction); - lua_setfield(L,-2,"makeaction"); - - lua_pop(L,1); - return 0; -} +extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <btBulletDynamicsCommon.h>
+#include <btBulletCollisionCommon.h>
+
+#include <shared/lua_api/common.hpp>
+
+extern btDiscreteDynamicsWorld* World;
+
+struct BActionItem: public btActionInterface{
+ int ref;
+ lua_State *L;
+ void updateAction(btCollisionWorld *world, btScalar delta){
+ lua_rawgeti(this->L,LUA_REGISTRYINDEX,this->ref);//{}
+ pusherrorfunc(this->L);//{},errfunc()
+ lua_getfield(this->L,-2,"action");//{},errfunc(),action()
+ if(lua_isnil(this->L,-1)){//no .action method
+ lua_pop(this->L,3);
+ return;
+ }
+ lua_pushvalue(this->L,-3);//{},errfunc(),action(),{}
+ lua_pushlightuserdata(this->L,world);//{},errfunc(),action(),{},ud_world
+ lua_pushnumber(this->L,delta);//{},errfunc(),action(),{},ud_world,delta
+ lua_pcall(this->L,3,0,-5);//{},errfunc()
+
+ //printf("error:%d\n",err);
+ lua_pop(this->L,2);
+ return;
+ }
+ void debugDraw(btIDebugDraw *d){
+ //no debug draw I guess
+ }
+};
+
+int makeaction(lua_State *L){
+ lua_newtable(L);//{}
+ int r = luaL_ref(L,LUA_REGISTRYINDEX);
+ lua_rawgeti(L,LUA_REGISTRYINDEX,r);
+ BActionItem *a = new BActionItem();
+ a->ref = r;
+ a->L = L;
+ World->addAction(a);
+ lua_pushlightuserdata(L,a);//{},ud_action
+ lua_setfield(L,-2,"action");//{}
+ return 1;
+}
+
+int newaction(lua_State *L){
+ lua_newtable(L);//{}
+ return 0;
+}
+
+int baction_register(lua_State *L){
+ lua_getglobal(L,"phys");
+ lua_pushcfunction(L,makeaction);
+ lua_setfield(L,-2,"makeaction");
+
+ lua_pop(L,1);
+ return 0;
+}
diff --git a/src/shared/lua_api/phys/btaction.hpp b/src/shared/lua_api/phys/btaction.hpp index e295b77..9593fda 100644 --- a/src/shared/lua_api/phys/btaction.hpp +++ b/src/shared/lua_api/phys/btaction.hpp @@ -1,11 +1,11 @@ -#ifndef _BTACTION_HPP_ -#include <stdio.h> -#include <stdlib.h> -extern "C" { - #include <lua.h> - #include <lauxlib.h> - #include <lualib.h> -} -int baction_register(lua_State* L); -void makeaction(lua_State *L); -#endif +#ifndef _BTACTION_HPP_
+#include <stdio.h>
+#include <stdlib.h>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+int baction_register(lua_State* L);
+void makeaction(lua_State *L);
+#endif
|
