#include #include #include extern "C" { #include #include #include } #include #include #include #include "bcharactercontroller.hpp" #include extern btDiscreteDynamicsWorld* World; extern std::list Objects; extern std::list Chars; /* 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){ 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); ghost->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT); btKinematicCharacterController *cc = new btKinematicCharacterController(ghost, cshape, 1, btVector3(0,1,0)); //cinfo.m_friction = 0; //btRigidBody *rigidbody = new btRigidBody(cinfo); // Add it to the world World->addAction(cc); //World->addVehicle(cc); //printf("Added rigid body to world: %p\n",World); Chars.push_back(cc); lua_pushlightuserdata(L,cc);//ud_cc } // 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); lua_pop(L,1); lua_newtable(L);//{} lua_pushlightuserdata(L,r);//ud_cc lua_setfield(L,-2,"character");//{} //Set it's metatable luaL_getmetatable(L, "phys.charactercontroller");//{},{phys.charactercontroller} lua_setmetatable(L, -2);//{} return 1; } //{phys.physbox}:delete() static int delbcharactercontroller(lua_State* L){//self //printf("Attempting to delete physbox\n"); lua_getfield(L,-1,"character");//self,ud_rigidbody btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1);//self,ud_rigidbody 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; } static const luaL_reg bcharactercontroller_m[] = { {"setwalkdir", bcharsetwalkdirection}, {"remove", delbcharactercontroller}, {0, 0}, }; void bcharactercontroller_register(lua_State* L){// //printf("Registered bphysbox\n"); luaL_newmetatable(L, "phys.charactercontroller");//{phys.physbox} lua_newtable(L);//{phys.physbox},{} luaL_register(L,NULL,bcharactercontroller_m);//{phys.physbox},{} lua_setfield(L,-2,"__index");//{phys.physbox} lua_pop(L,1);// lua_getglobal(L,"phys");//{} lua_pushcfunction(L,newbcharactercontroller);//{},newbcharactercontroller() lua_setfield(L,-2,"newcharactercontroller");//{} lua_pop(L,1); }