From 0d2de2ba9c616862d7881f089382db772d034f89 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 27 Oct 2019 17:25:16 -0400 Subject: Various updates --- src/shared/lua_api/common.cpp | 2 ++ src/shared/lua_api/load_common.cpp | 26 +++++++++++++++-- src/shared/lua_api/load_common.hpp | 11 ++------ src/shared/lua_api/load_net.cpp | 49 +++++++++++++++++---------------- src/shared/lua_api/load_phys.cpp | 1 + src/shared/lua_api/load_scene.cpp | 1 - src/shared/lua_api/phys/bphysbuffer.cpp | 10 +++---- src/shared/lua_api/phys/bphysmodel.cpp | 19 +++++++++---- src/shared/phys/physcommon.cpp | 49 +++++++++++++++++++++++++++++++-- 9 files changed, 120 insertions(+), 48 deletions(-) (limited to 'src/shared') diff --git a/src/shared/lua_api/common.cpp b/src/shared/lua_api/common.cpp index 9b833fe..b26c3cc 100644 --- a/src/shared/lua_api/common.cpp +++ b/src/shared/lua_api/common.cpp @@ -8,6 +8,8 @@ extern "C" { #include #include "common.hpp" +#define set_const(l,x) lua_pushstring(l,#x);lua_pushinteger(l,x);lua_settable(l,-3); + //Expose things to the lua state void loadLLibs(lua_State* L){ diff --git a/src/shared/lua_api/load_common.cpp b/src/shared/lua_api/load_common.cpp index 66afe67..dc61ef1 100644 --- a/src/shared/lua_api/load_common.cpp +++ b/src/shared/lua_api/load_common.cpp @@ -1,8 +1,30 @@ +#include +#include extern "C" { #include #include #include } +using namespace std::chrono; -void loadCommonLibs(lua_State* L); -void gameloop_common(lua_State* L); +//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(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 5be5de1..759ba85 100644 --- a/src/shared/lua_api/load_common.hpp +++ b/src/shared/lua_api/load_common.hpp @@ -1,11 +1,4 @@ #include -void loadCommonLibs(lua_State* L){ - lua_getglobal(L,"GAME"); - lua_pushcfunction(L,make_crashy); - lua_setfield(L,-2,"crashy"); -} - -void gameloop_common(lua_State* L){ - -} +void loadCommonLibs(lua_State* L); +void gameloop_common(lua_State* L); diff --git a/src/shared/lua_api/load_net.cpp b/src/shared/lua_api/load_net.cpp index d8d321a..3452aa2 100644 --- a/src/shared/lua_api/load_net.cpp +++ b/src/shared/lua_api/load_net.cpp @@ -30,34 +30,35 @@ ect. */ extern "C" { - #include - #include - #include +# include +# include +# include } #include - +#include #include #include -#include - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - +extern "C" { +# include + +# include +# include +# include +# include +# include + +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +} #include "load_net.hpp" #include @@ -336,6 +337,7 @@ int block_recv(lua_State *L){//{socket} } void gameloop_net(lua_State* L){ + assert(lua_gettop(L) == 0); //printf("Doing net of gameloop,starting with %d args\n",lua_gettop(L)); //printf("Got net\n"); lua_getglobal(L,"net");//{net} @@ -423,6 +425,7 @@ void gameloop_net(lua_State* L){ //printf("There are %d items left on the lua stack\n",lua_gettop(L)); lua_pop(L,2); //printf("Done with net game loop\n"); + assert(lua_gettop(L) == 0); } /*** diff --git a/src/shared/lua_api/load_phys.cpp b/src/shared/lua_api/load_phys.cpp index be5529c..7002a09 100644 --- a/src/shared/lua_api/load_phys.cpp +++ b/src/shared/lua_api/load_phys.cpp @@ -6,6 +6,7 @@ #include void load_physfuncs(lua_State* L){ + printf("Loading phys things\n"); lua_newtable(L);//{} lua_setglobal(L,"phys");// lua_getglobal(L,"phys");//{phys} diff --git a/src/shared/lua_api/load_scene.cpp b/src/shared/lua_api/load_scene.cpp index 633fcb1..ef22653 100644 --- a/src/shared/lua_api/load_scene.cpp +++ b/src/shared/lua_api/load_scene.cpp @@ -7,5 +7,4 @@ extern "C" { void register_scene(lua_State* L){ - } diff --git a/src/shared/lua_api/phys/bphysbuffer.cpp b/src/shared/lua_api/phys/bphysbuffer.cpp index 111b159..074f506 100644 --- a/src/shared/lua_api/phys/bphysbuffer.cpp +++ b/src/shared/lua_api/phys/bphysbuffer.cpp @@ -27,11 +27,11 @@ using namespace video; extern btDiscreteDynamicsWorld* World; extern core::list Objects; -static LBPhysNode* checkisbphysmodel(lua_State* L, int index){ - void* ud = luaL_checkudata(L,index,"phys.physmodel"); - luaL_argcheck(L,ud != NULL, index, "'phys.physmodel' expected"); - return (LBPhysNode*) ud; -} +//static LBPhysNode* checkisbphysmodel(lua_State* L, int index){ + //void* ud = luaL_checkudata(L,index,"phys.physmodel"); + //luaL_argcheck(L,ud != NULL, index, "'phys.physmodel' expected"); + //return (LBPhysNode*) ud; +//} //iscenecamera.new(Vector position, Vector lookat, parrent) // {} {} 0 1 diff --git a/src/shared/lua_api/phys/bphysmodel.cpp b/src/shared/lua_api/phys/bphysmodel.cpp index 0cff0ed..2c9a02d 100644 --- a/src/shared/lua_api/phys/bphysmodel.cpp +++ b/src/shared/lua_api/phys/bphysmodel.cpp @@ -12,6 +12,8 @@ extern "C" { #include } #include +#include +#include #include "bphysmodel.hpp" #include #include @@ -69,19 +71,22 @@ void makebphysmodel(lua_State *L){ //__mingw_printf("attrib.num_vertices: %u\n",attrib.num_vertices); //__mingw_printf("attrib.num_faces: %u\n",attrib.num_faces); btTriangleMesh* trimesh = new btTriangleMesh(); + btVector3 vertexes[attrib.num_vertices]; for(size_t i = 0; i < attrib.num_vertices; i++){ //0 - x, so num_vertices - 1 - //__mingw_printf("Looking at vertice %llu/%u\n",i,attrib.num_vertices); - //DO NOT MULTIPLY THIS BY SIEZEOF(FLOAT) float *vs = attrib.vertices + (3*i);//3 floats per vertex //printf("Got start\n"); - float v1 = vs[0]; + //For some reason irrlicht and bullet disagree with which direction +x is, + //negate the x in the physics engine so the graphical stuff lines up + //with the physics stuff + float v1 = -vs[0]; //printf("Got 1\n"); float v2 = vs[1]; //printf("Got 2\n"); float v3 = vs[2]; //printf("Got all 3 vertexees\n"); //printf("Adding vertex: (%f %f %f)\n",v1, v2, v3); - trimesh->findOrAddVertex(btVector3(v1,v2,v3),true); + //trimesh->findOrAddVertex(btVector3(v1,v2,v3),true); + vertexes[i] = btVector3(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 @@ -89,7 +94,8 @@ void makebphysmodel(lua_State *L){ i1 = attrib.faces[i]; i2 = attrib.faces[i+1]; i3 = attrib.faces[i+2]; - trimesh->addTriangleIndices(i1.v_idx,i2.v_idx,i3.v_idx); + trimesh->addTriangle(vertexes[i1.v_idx],vertexes[i2.v_idx],vertexes[i3.v_idx],true); + //trimesh->addTriangleIndices(i1.v_idx,i2.v_idx,i3.v_idx); } printf("Finished adding triangle indicies\n"); //size_t numverts = attrib.num_face_num_verts; @@ -120,7 +126,8 @@ void makebphysmodel(lua_State *L){ //face_offset += (size_t)attrib.face_num_verts[i]; //} printf("Done building trimesh\n"); - btCollisionShape *shape = new btBvhTriangleMeshShape(trimesh,true); + //btCollisionShape *shape = new btGImpactMeshShape(trimesh); + btCollisionShape *shape = new btConvexTriangleMeshShape(trimesh); btTransform tr; tr.setIdentity(); tr.setOrigin(btVector3(x,y,z)); diff --git a/src/shared/phys/physcommon.cpp b/src/shared/phys/physcommon.cpp index 379ad55..45374d2 100644 --- a/src/shared/phys/physcommon.cpp +++ b/src/shared/phys/physcommon.cpp @@ -2,8 +2,14 @@ #define __shared_physcommon_h #include #include +#include #include - +extern "C" { + #include + #include + #include +} +#include #include "physcommon.hpp" using namespace std::chrono; @@ -11,6 +17,7 @@ using namespace std::chrono; btDiscreteDynamicsWorld* World; std::list Objects; std::list Chars; +extern lua_State *L; extern void dropCollisionObject(btCollisionObject* b); @@ -45,7 +52,7 @@ btCollisionDispatcher* Dispatcher; btSequentialImpulseConstraintSolver* Solver; void phys_genesis(){ - broadphase = new btAxisSweep3(btVector3(-1000,-1000,-1000),btVector3(1000,1000,1000)); + broadphase = new btAxisSweep3(btVector3(-100000,-100000,-100000),btVector3(100000,100000,100000)); broadphase ->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback()); //printf("Broadphase\n"); CollisionConfiguration = new btDefaultCollisionConfiguration(); @@ -84,6 +91,7 @@ void UpdatePhysics(double TDeltaTime, void(*f)(btCollisionObject *)) { //printf("Pre step simulation\n"); World->stepSimulation(TDeltaTime * 0.2f, 60); //printf("Done step simulation\n"); + assert(lua_gettop(L) == 0); if(f){ //printf("Updating the position of %llu objects\n", Objects.size()); // Relay the object's orientation to irrlicht @@ -91,15 +99,52 @@ void UpdatePhysics(double TDeltaTime, void(*f)(btCollisionObject *)) { (*f)(*it); } } + assert(lua_gettop(L) == 0); + + //Call phy.oncollide(obj1, obj2, point1, point2, normal2) + lua_getglobal(L,"phys");//phys + lua_getfield(L,-1,"colliders");//phys,colliders + int nummanifolds = World->getDispatcher()->getNumManifolds(); + pusherrorfunc(L);//{phys},{colliders},errfunc() + for(int i = 0; i < nummanifolds; i++){ + //printf("Looking at manifold %d, top is %d\n", i, lua_gettop(L)); + + lua_getfield(L,-3,"oncollide");//{phys},{colliders},errfunc(),oncollide() + if(lua_isnil(L,-1)){ + lua_pop(L,4); + return; + } + btPersistentManifold *mf = World->getDispatcher()->getManifoldByIndexInternal(i); + btCollisionObject *oa = (btCollisionObject*)mf->getBody0(); + btCollisionObject *ob = (btCollisionObject*)mf->getBody1(); + btManifoldPoint mp = mf->getContactPoint(i); + btVector3 pa = mp.getPositionWorldOnA(); + btVector3 pb = mp.getPositionWorldOnB(); + btVector3 pn = mp.m_normalWorldOnB; + lua_pushlightuserdata(L,oa);//{phys},{colliders},errfunc(),oncollide(),ud_oa + lua_gettable(L,-4);//{phys},{colliders},errfun(),concollide(),{oa} + lua_pushlightuserdata(L,ob);//{phys},{colliders},errfunc(),oncollide(),{oa},ud_ob + lua_gettable(L,-5);//{phys},{colliders},errfunc(),oncollide(),{oa},{ob} + pushvector3d(L,pa.x(),pa.y(),pa.z()); + pushvector3d(L,pb.x(),pb.y(),pb.z()); + pushvector3d(L,pn.x(),pn.y(),pn.z());//{phys},{colliders},errfunc(),oncollide(),{oa},{ob},{pa},{pb},{normal} + int err = lua_pcall(L,5,0,-7);//{phys},{colliders},errfunc() + if(err) + printf("Failed to call oncollide\n"); + } + lua_pop(L,3); + assert(lua_gettop(L) == 0); } high_resolution_clock::time_point t1 = high_resolution_clock::now(); void gameloop_phys(void(*f)(btCollisionObject *)){ + assert(lua_gettop(L) == 0); //printf("Doing phys gameloop\n"); high_resolution_clock::time_point now = high_resolution_clock::now(); duration delta = now-t1; double steps = delta.count() * 10; UpdatePhysics(steps,f); t1 = now; + assert(lua_gettop(L) == 0); } #endif -- cgit v1.2.3-70-g09d2