diff options
| author | Alexander Pickering <alex@cogarr.net> | 2018-11-07 14:49:35 -0500 |
|---|---|---|
| committer | Alexander Pickering <alex@cogarr.net> | 2018-11-07 14:50:58 -0500 |
| commit | 2009501214f3c2e3f8d8b1a06432afdf39276bd5 (patch) | |
| tree | c6c091a4b6f3f714657930cda4fd485ee1dd3d95 | |
| parent | 112517494847f0c86f58544cbf4c35c9b7712ab1 (diff) | |
| download | brokengine-2009501214f3c2e3f8d8b1a06432afdf39276bd5.tar.gz brokengine-2009501214f3c2e3f8d8b1a06432afdf39276bd5.tar.bz2 brokengine-2009501214f3c2e3f8d8b1a06432afdf39276bd5.zip | |
Added Kinematic Character Controllers
| -rw-r--r-- | src/client/lua_api/load_cphys.cpp | 4 | ||||
| -rw-r--r-- | src/client/lua_api/load_cphys.hpp | 2 | ||||
| -rw-r--r-- | src/client/main.cpp | 14 | ||||
| -rw-r--r-- | src/server/main.cpp | 10 | ||||
| -rw-r--r-- | src/shared/lua_api/load_phys.cpp | 2 | ||||
| -rw-r--r-- | src/shared/lua_api/load_phys.hpp | 2 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bcharactercontroller.cpp | 175 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bcharactercontroller.hpp | 10 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bhingeconstraint.cpp | 17 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bphysgeneric.cpp | 17 | ||||
| -rw-r--r-- | src/shared/phys/physcommon.cpp | 46 | ||||
| -rw-r--r-- | src/shared/phys/physcommon.hpp | 4 |
12 files changed, 280 insertions, 23 deletions
diff --git a/src/client/lua_api/load_cphys.cpp b/src/client/lua_api/load_cphys.cpp index b38b84d..ed81408 100644 --- a/src/client/lua_api/load_cphys.cpp +++ b/src/client/lua_api/load_cphys.cpp @@ -40,9 +40,7 @@ int raytest(lua_State *L){ return 1; } -void load_physfuncs(lua_State* L){ - lua_newtable(L);//{} - lua_setglobal(L,"phys");// +void load_cphysfuncs(lua_State* L){ //phys things cbphysbox_register(L); diff --git a/src/client/lua_api/load_cphys.hpp b/src/client/lua_api/load_cphys.hpp index 7ee07fe..5afd102 100644 --- a/src/client/lua_api/load_cphys.hpp +++ b/src/client/lua_api/load_cphys.hpp @@ -10,5 +10,5 @@ extern "C" { } #include <irrlicht.h> -void load_physfuncs(lua_State* L); +void load_cphysfuncs(lua_State* L); #endif diff --git a/src/client/main.cpp b/src/client/main.cpp index bc509c7..e8533d8 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -10,6 +10,7 @@ extern "C" { #include <chrono> #include <btBulletDynamicsCommon.h> +#include <btBulletCollisionCommon.h> #include <cstdlib> #include "initdevice.hpp" @@ -20,6 +21,7 @@ extern "C" { #include "lua_api/load_cphys.hpp" #include "lua_api/load_video.hpp" #include "lua_api/load_io.hpp" +#include <shared/lua_api/load_phys.hpp> #include "callbackhandeler.hpp" #include <shared/lua_api/common.hpp> @@ -49,6 +51,7 @@ void loadIrrLibs(lua_State* L, IrrlichtDevice* device){ printf("[OK]\n"); printf("Loading physfuncs..."); load_physfuncs(L); + load_cphysfuncs(L); printf("[OK]\n"); printf("Loading videofuncs..."); load_videofuncs(L); @@ -56,12 +59,18 @@ void loadIrrLibs(lua_State* L, IrrlichtDevice* device){ load_iofuncs(L); } -void RemoveISceneNode(btRigidBody* rb){ +void dropRigidBody(btRigidBody* rb){ ISceneNode *Node = static_cast<ISceneNode*>(rb->getUserPointer()); if(Node) Node->remove(); } +void dropChar(btKinematicCharacterController *a){ + ISceneNode *node = (ISceneNode*)a->getGhostObject()->getUserPointer(); + if(node) + node->remove(); +} + // Converts a quaternion to an euler angle void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler) { btScalar W = TQuat.getW(); @@ -202,7 +211,8 @@ int main(int argc, char *argv[]){ lua_pop(state,2); } } - phys_shutdown(RemoveISceneNode); + //phys_shutdown(RemoveISceneNode); + phys_shutdown(); device->drop(); printf("Goodbye\n"); diff --git a/src/server/main.cpp b/src/server/main.cpp index 9fb28c8..2464899 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -26,6 +26,12 @@ extern "C" { using namespace std; using namespace chrono; +void dropRigidBody(btRigidBody* rb){ +} + +void dropChar(btKinematicCharacterController *a){ +} + lua_State* L; void gameloop(){ gameloop_phys(NULL); @@ -47,7 +53,7 @@ int main (){ luaL_openlibs(L); loadLLibs(L); loadNetLibs(L); - loadPhysLibs(L); + load_physfuncs(L); int iErr = luaL_dofile(L,"../data/init.lua"); if(iErr != 0){ printf("Failed to open lua file:../data/init.lua\n"); @@ -70,7 +76,7 @@ int main (){ } printf("End of server gameloop\n"); }while(true); - phys_shutdown(NULL); + phys_shutdown(); return 0; } diff --git a/src/shared/lua_api/load_phys.cpp b/src/shared/lua_api/load_phys.cpp index a846945..6875970 100644 --- a/src/shared/lua_api/load_phys.cpp +++ b/src/shared/lua_api/load_phys.cpp @@ -2,7 +2,7 @@ #include "phys/bphysbox.hpp" #include "phys/bhingeconstraint.hpp" -void loadPhysLibs(lua_State* L){ +void load_physfuncs(lua_State* L){ lua_newtable(L);//{} lua_setglobal(L,"phys"); bphysbox_register(L); diff --git a/src/shared/lua_api/load_phys.hpp b/src/shared/lua_api/load_phys.hpp index 6f9e9a9..fefa86b 100644 --- a/src/shared/lua_api/load_phys.hpp +++ b/src/shared/lua_api/load_phys.hpp @@ -4,4 +4,4 @@ extern "C" { #include <lualib.h> } -void loadPhysLibs(lua_State* L); +void load_physfuncs(lua_State* L); diff --git a/src/shared/lua_api/phys/bcharactercontroller.cpp b/src/shared/lua_api/phys/bcharactercontroller.cpp new file mode 100644 index 0000000..8030846 --- /dev/null +++ b/src/shared/lua_api/phys/bcharactercontroller.cpp @@ -0,0 +1,175 @@ + + +#include <stdio.h> +#include <stdlib.h> +#include <list> +extern "C" { + #include <lua.h> + #include <lauxlib.h> + #include <lualib.h> +} +#include <btBulletDynamicsCommon.h> +#include "bcharactercontroller.hpp" +#include <shared/lua_api/common.hpp> + +extern btDiscreteDynamicsWorld* World; +extern std::list<btRigidBody*> Objects; +/* +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_btRigidBody :: ({v3 size}, {v3 origin}, double mass) +void makenewbphysbox(lua_State* 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); + + // Set the initial position of the object + btTransform transform = btTransform(btQuaternion(0,0,0,1),pos); + //transform.setIdentity(); + //transform.setOrigin(pos); + + // Give it a default MotionState + btDefaultMotionState* motionstate = new btDefaultMotionState(transform); + if(!motionstate){ + //printf("No motionstate\n"); + } + // Create the shape + btCollisionShape* shape = new btBoxShape(vshape); + if(!shape){ + //printf("no shape\n"); + } + + // 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 + ); + btKinematicCharacterController cc = btKinematicCharacterController(btPairCacheingGhostObject(), shape, 0.8, btVector3(0,1,0)); + //cinfo.m_friction = 0; + //btRigidBody *rigidbody = new btRigidBody(cinfo); + if(!rigidbody){ + //printf("No rigidbody\n"); + } + + // Add it to the world + World->addVehicle(cc); + //printf("Added rigid body to world: %p\n",World); + Objects.push_back(cc); + + lua_pushlightuserdata(L,rigidbody);//ud_rigidbody +} + +// phys.newphysbox(vector3 size, vector3 origin, double mass) +int newbphysbox(lua_State* L){ + //printf("Createing bphysbox!\n"); + //Create it's lua representation + makenewbphysbox(L);//ud_btRigidBody + btRigidBody* r = (btRigidBody*)lua_touserdata(L,-1); + lua_pop(L,1); + lua_newtable(L);//{} + lua_pushlightuserdata(L,r);//ud_btRigidBody + lua_setfield(L,-2,"rigidbody");//{} + + //Set it's metatable + luaL_getmetatable(L, "phys.physbox");//{},{phys.physbox} + lua_setmetatable(L, -2);//{} + + return 1; +} + +//{phys.physbox}:delete() +static int delbphysbox(lua_State* L){//self + //printf("Attempting to delete physbox\n"); + lua_getfield(L,-1,"rigidbody");//self,ud_rigidbody + btRigidBody* r = (btRigidBody*)lua_touserdata(L,-1);//self,ud_rigidbody + delete r->getCollisionShape(); + delete r->getMotionState(); + delete r; + + return 0; +} + +// physbox:setpos({v3 pos}) +static int bphyssetpos(lua_State *L){//self,{v3 pos} + double nx,ny,nz; + popvector3d(L,&nx,&ny,&nz);//self + + lua_getfield(L,-1,"rigidbody");//self,ud_rigidbody + btRigidBody* i = (btRigidBody*)lua_touserdata(L,-1);//self + btMotionState* ms = i->getMotionState(); + btTransform bt; + ms->getWorldTransform(bt); + + btVector3 to = btVector3(nx,ny,nz); + bt.setOrigin(to); + ms->setWorldTransform(bt); + i->activate(); + + lua_pop(L,1);// + return 0; +} + +// {v3 pos} :: physbox:getpos() +static int bphysgetpos(lua_State *L){//self + //printf("Physics box set pos called\n"); + lua_getfield(L,-1,"rigidbody");//self,ud_rigidbody + btRigidBody* i = (btRigidBody*)lua_touserdata(L,-1);//self,ud_rigidbody + btTransform bt = i->getWorldTransform(); + btVector3 bv = bt.getOrigin(); + lua_pop(L,2);// + pushvector3d(L,bv.x(),bv.y(),bv.z());//{} + + return 1; +} + +static const luaL_reg bphysbox_m[] = { + {"getpos", bphysgetpos}, + {"setpos", bphyssetpos}, + {"delete", delbphysbox}, + {0, 0}, +}; + +void bphysbox_register(lua_State* L){// + //printf("Registered bphysbox\n"); + + luaL_newmetatable(L, "phys.physbox");//{phys.physbox} + lua_newtable(L);//{phys.physbox},{} + luaL_register(L,NULL,bphysbox_m);//{phys.physbox},{} + lua_setfield(L,-2,"__index");//{phys.physbox} + + lua_pop(L,1);// + + lua_getglobal(L,"phys");//{} + lua_pushcfunction(L,newbphysbox);//{},newbphysbox() + lua_setfield(L,-2,"newphysbox");//{} + + lua_pop(L,1); +} diff --git a/src/shared/lua_api/phys/bcharactercontroller.hpp b/src/shared/lua_api/phys/bcharactercontroller.hpp new file mode 100644 index 0000000..10d71df --- /dev/null +++ b/src/shared/lua_api/phys/bcharactercontroller.hpp @@ -0,0 +1,10 @@ +#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); diff --git a/src/shared/lua_api/phys/bhingeconstraint.cpp b/src/shared/lua_api/phys/bhingeconstraint.cpp index 168d1b6..ab6fb50 100644 --- a/src/shared/lua_api/phys/bhingeconstraint.cpp +++ b/src/shared/lua_api/phys/bhingeconstraint.cpp @@ -7,6 +7,7 @@ extern "C" { #include <lauxlib.h> #include <lualib.h> } +#include <shared/lua_api/common.hpp> #include <Irrlicht.h> #include <btBulletDynamicsCommon.h> #include <shared/lua_api/phys/bhingeconstraint.hpp> @@ -19,7 +20,23 @@ using namespace video; 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; } diff --git a/src/shared/lua_api/phys/bphysgeneric.cpp b/src/shared/lua_api/phys/bphysgeneric.cpp index e8ef2b5..44c789d 100644 --- a/src/shared/lua_api/phys/bphysgeneric.cpp +++ b/src/shared/lua_api/phys/bphysgeneric.cpp @@ -106,6 +106,22 @@ int getlineardamping(lua_State *L){ } /*** +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 @@ -208,5 +224,6 @@ extern const luaL_reg brigidbody_m[] = { {"activate", activate}, {"getvelocity", getvelocity}, {"setvelocity", setvelocity}, + {"setangfactor", setangfactor}, {NULL, NULL} }; diff --git a/src/shared/phys/physcommon.cpp b/src/shared/phys/physcommon.cpp index 11f18c9..783f282 100644 --- a/src/shared/phys/physcommon.cpp +++ b/src/shared/phys/physcommon.cpp @@ -10,22 +10,43 @@ using namespace std::chrono; btDiscreteDynamicsWorld* World; std::list<btRigidBody*> Objects; +std::list<btKinematicCharacterController*> Chars; + +extern void dropRigidBody(btRigidBody* b); +extern void dropChar(btKinematicCharacterController* a); // Removes all objects from the world -void ClearObjects(btDiscreteDynamicsWorld* wr, std::list<btRigidBody*> objs, void(*f)(btRigidBody*)) { +//void ClearObjects(btDiscreteDynamicsWorld* wr, std::list<btRigidBody*> objs, void(*f)(btRigidBody*)) { - for(std::list<btRigidBody *>::iterator Iterator = objs.begin(); Iterator != objs.end(); ++Iterator) { - btRigidBody *Object = *Iterator; + //for(std::list<btRigidBody *>::iterator Iterator = objs.begin(); Iterator != objs.end(); ++Iterator) { + //btRigidBody *Object = *Iterator; - if(f){ - (*f)(Object); - } + //if(f){ + //(*f)(Object); + //} + + //// Remove the object from the world + //wr->removeRigidBody(Object); + //delete Object; + //} + //objs.clear(); +//} +void ClearObjects(){ + for(std::list<btRigidBody *>::iterator itr = Objects.begin(); itr != Objects.end(); ++itr){ + dropRigidBody(*itr); + World->removeRigidBody(*itr); + delete *itr; + } + Objects.clear(); +} - // Remove the object from the world - wr->removeRigidBody(Object); - delete Object; +void ClearChars(){ + for(std::list<btKinematicCharacterController *>::iterator itr = Chars.begin(); itr != Chars.end(); ++itr){ + dropChar(*itr); + World->removeVehicle(*itr); + delete *itr; } - objs.clear(); + Chars.clear(); } btBroadphaseInterface* BroadPhase; @@ -48,8 +69,9 @@ void phys_genesis(){ //printf("Created physics world: %p\n",World); } -void phys_shutdown(void(*f)(btRigidBody*)){ - ClearObjects(World,Objects,f); +void phys_shutdown(){ + ClearObjects(); + ClearChars(); printf("cleared objects\n"); delete BroadPhase; printf("deleted broadphase\n"); diff --git a/src/shared/phys/physcommon.hpp b/src/shared/phys/physcommon.hpp index 00061ec..dd5fe14 100644 --- a/src/shared/phys/physcommon.hpp +++ b/src/shared/phys/physcommon.hpp @@ -1,5 +1,7 @@ #ifndef _shared_physcommon_ +#include <BulletDynamics/Character/btKinematicCharacterController.h> +#include <BulletCollision/CollisionDispatch/btGhostObject.h> void gameloop_phys(void(*f)(btRigidBody*)); void phys_genesis(); -void phys_shutdown(void(*f)(btRigidBody*)); +void phys_shutdown(); #endif |
