From d5cd0c7b4425e25b11a1ceec154a5c752d508a42 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 26 Jun 2019 16:14:00 -0400 Subject: Major refactor of physics code Move all the physics code into the shared directory, and fix the ghost objects (aabb only) --- src/shared/lua_api/common.cpp | 15 ++++++++- src/shared/lua_api/common.hpp | 3 +- src/shared/lua_api/load_common.cpp | 8 +++++ src/shared/lua_api/load_common.hpp | 11 +++++++ src/shared/lua_api/load_net.cpp | 13 ++++---- src/shared/lua_api/phys/bcollider.cpp | 47 +++++++++++++++++++++++++++- src/shared/lua_api/phys/bghostobject.cpp | 12 +++---- src/shared/lua_api/phys/bhingeconstraint.cpp | 6 ---- src/shared/lua_api/phys/bphysbox.cpp | 1 - src/shared/lua_api/phys/bphysgeneric.cpp | 41 +++++++++--------------- src/shared/lua_api/phys/bphysmodel.cpp | 32 ++++++++++++++++--- src/shared/phys/physcommon.cpp | 1 + 12 files changed, 135 insertions(+), 55 deletions(-) create mode 100644 src/shared/lua_api/load_common.cpp create mode 100644 src/shared/lua_api/load_common.hpp (limited to 'src/shared') diff --git a/src/shared/lua_api/common.cpp b/src/shared/lua_api/common.cpp index 1ee4b72..9b833fe 100644 --- a/src/shared/lua_api/common.cpp +++ b/src/shared/lua_api/common.cpp @@ -5,6 +5,7 @@ extern "C" { #include } +#include #include "common.hpp" //Expose things to the lua state @@ -236,6 +237,9 @@ int popvector2i(lua_State* L, long* a, long* b){ return 0; } +//When crashy is enabled, errors that usually get caught by errfunc crash instead +//Useful for testing +bool crashy = false; //errfunc("mssg",false) int errfunc(lua_State *L){ printf("Error function called\n"); @@ -268,9 +272,18 @@ int errfunc(lua_State *L){ printf("--------------------------\n"); lua_call(L,1,0);//error,{debug} lua_pop(L,1); - printf("Returning"); + printf("Returning:"); + printf("crashy is %d", crashy); + if(crashy){ + exit(-1); + } return 1; } void pusherrorfunc(lua_State *L){ lua_pushcfunction(L,errfunc); } +int make_crashy(lua_State *L){ + crashy = true; + printf("Setting crashy to %d\n",crashy); + return 0; +} diff --git a/src/shared/lua_api/common.hpp b/src/shared/lua_api/common.hpp index ba89521..ef8ae58 100644 --- a/src/shared/lua_api/common.hpp +++ b/src/shared/lua_api/common.hpp @@ -24,5 +24,6 @@ int popvector2i(lua_State*,long*,long*); int poprecti(lua_State* L,long*,long*,long*,long*); int pushrecti(lua_State* L,long,long,long,long); -void pusherrorfunc(lua_State *L); +void pusherrorfunc(lua_State* L); +int make_crashy(lua_State* L); #endif diff --git a/src/shared/lua_api/load_common.cpp b/src/shared/lua_api/load_common.cpp new file mode 100644 index 0000000..66afe67 --- /dev/null +++ b/src/shared/lua_api/load_common.cpp @@ -0,0 +1,8 @@ +extern "C" { + #include + #include + #include +} + +void loadCommonLibs(lua_State* L); +void gameloop_common(lua_State* L); diff --git a/src/shared/lua_api/load_common.hpp b/src/shared/lua_api/load_common.hpp new file mode 100644 index 0000000..5be5de1 --- /dev/null +++ b/src/shared/lua_api/load_common.hpp @@ -0,0 +1,11 @@ +#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){ + +} diff --git a/src/shared/lua_api/load_net.cpp b/src/shared/lua_api/load_net.cpp index 04a9239..d8d321a 100644 --- a/src/shared/lua_api/load_net.cpp +++ b/src/shared/lua_api/load_net.cpp @@ -10,20 +10,20 @@ ect. --Server local s = net.newsocket(net.REP) s:bind("tcp://127.0.0.1:8765") - s:receive(function(stream) + function s:receive(stream) local message = stream:readstring() s:send("pong",function(stream) stream:writestring(message .. " world!") end) - end) + end @usage --Client local c = net.newsocket(net.REQ) c:connect("tcp://127.0.0.1:8765") - c:receive(function(stream) + c:receive(stream) print(stream:readstring()) - end) + end c:send(function(stream) stream:writestring("Hello,") end) @@ -378,8 +378,8 @@ void gameloop_net(lua_State* L){ if(err){ switch(err){ case NNG_EAGAIN: break; - case NNG_ESTATE: - if(stype == REQ) break; + //case NNG_ESTATE: + //if(stype == REQ) break; default: printf("Net error: %s\n\tSocket type:%d\n",nng_strerror(err),stype); lua_pushstring(L,"Error while receving message:"); @@ -535,7 +535,6 @@ int send(lua_State* L){ //lua_getfield(L,-1,"n");//{socket},type //int sockettype = lua_tonumber(L,-1); //lua_pop(L,2);// - //switch(sockettype //netfuncs[fd] = func;// //nettypes[fd] = sockettype; //return 0; diff --git a/src/shared/lua_api/phys/bcollider.cpp b/src/shared/lua_api/phys/bcollider.cpp index 356c504..0a87af3 100644 --- a/src/shared/lua_api/phys/bcollider.cpp +++ b/src/shared/lua_api/phys/bcollider.cpp @@ -33,12 +33,57 @@ to move. int activate(lua_State *L){ btCollisionObject *r = popCollider(L); - r->activate(); + 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/bghostobject.cpp b/src/shared/lua_api/phys/bghostobject.cpp index da9406d..8174b21 100644 --- a/src/shared/lua_api/phys/bghostobject.cpp +++ b/src/shared/lua_api/phys/bghostobject.cpp @@ -59,14 +59,14 @@ void makeghostobject(lua_State* L){ btGhostObject *ghost = new btGhostObject(); ghost->setCollisionShape(shape); ghost->setWorldTransform(transform); - ghost->setCollisionFlags( - btCollisionObject::CollisionFlags::CF_NO_CONTACT_RESPONSE | - btCollisionObject::CollisionFlags::CF_KINEMATIC_OBJECT - ); + //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); + //Objects.push_back(ghost); lua_pushlightuserdata(L,ghost);//ud_ghost } @@ -148,7 +148,7 @@ int bghostoverlapping(lua_State *L){ 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 collider, failing...\n"); + printf("Failed to find object of collider %p\n", (void*)co); lua_pushstring(L,"Failed to find collider we are overlapping"); lua_error(L); } diff --git a/src/shared/lua_api/phys/bhingeconstraint.cpp b/src/shared/lua_api/phys/bhingeconstraint.cpp index ab6fb50..a8c6567 100644 --- a/src/shared/lua_api/phys/bhingeconstraint.cpp +++ b/src/shared/lua_api/phys/bhingeconstraint.cpp @@ -8,15 +8,9 @@ extern "C" { #include } #include -#include #include #include -using namespace irr; -using namespace scene; -using namespace core; -using namespace video; - extern btDiscreteDynamicsWorld* World; extern std::list Objects; diff --git a/src/shared/lua_api/phys/bphysbox.cpp b/src/shared/lua_api/phys/bphysbox.cpp index a0ad15a..4564c6e 100644 --- a/src/shared/lua_api/phys/bphysbox.cpp +++ b/src/shared/lua_api/phys/bphysbox.cpp @@ -79,7 +79,6 @@ void makenewbphysbox(lua_State* L){ // Add it to the world World->addRigidBody(rigidbody); - //printf("Added rigid body to world: %p\n",World); Objects.push_back(rigidbody); lua_pushlightuserdata(L,rigidbody);//ud_rigidbody diff --git a/src/shared/lua_api/phys/bphysgeneric.cpp b/src/shared/lua_api/phys/bphysgeneric.cpp index 56320dd..718ffed 100644 --- a/src/shared/lua_api/phys/bphysgeneric.cpp +++ b/src/shared/lua_api/phys/bphysgeneric.cpp @@ -205,6 +205,7 @@ int setdamping(lua_State *L){ return 0; } + /*** Sets flags on this rigidbody @function rigidbody:setflags(flags) @@ -223,41 +224,29 @@ int setflags(lua_State *L){ /*** Apply an impulse to the rigidboy -@function rigidbody:centralimpulse(vec3 impulse) +@function rigidbody:applyimpulse(vec3 impulse[, vec3 offset]) @tparam vector3 impulse The direction to apply the impulse in +@tparam? vector3 offset The offset from the center to apply the impulse, default = {0, 0, 0} */ +//applyimpuse(self,{pos},{off}) int applyimpulse(lua_State *L){ + printf("Apply impulse called...\n"); + int nargs = lua_gettop(L); double x,y,z,ox,oy,oz; + if(nargs > 2){ + popvector3d(L,&ox,&oy,&oz); + }else{ + ox = 0; + oy = 0; + oz = 0; + } 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; } -//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; -} /* A callback used to detect collisions @@ -296,9 +285,7 @@ extern const luaL_reg brigidbody_m[] = { {"getldamping", getlineardamping}, {"getadamping", getangulardamping}, {"setdamping", setdamping}, - {"getpos", getpos}, - {"setpos", setpos}, - //{"activate", activate}, + //{"activate", activate}, --moved to bcollider_m {"getvelocity", getvelocity}, {"setvelocity", setvelocity}, {"setangfactor", setangfactor}, diff --git a/src/shared/lua_api/phys/bphysmodel.cpp b/src/shared/lua_api/phys/bphysmodel.cpp index 55039ba..a89a28f 100644 --- a/src/shared/lua_api/phys/bphysmodel.cpp +++ b/src/shared/lua_api/phys/bphysmodel.cpp @@ -16,9 +16,13 @@ extern "C" { #include #include +#include +#include + extern btDiscreteDynamicsWorld* World; extern std::list Objects; +//TODO: This will break in the future, see github.com/syoyo/tinyobjloader-c/issues/16 //"physicfile",mass[,position][,lookat] :: ud_rigidbody void makebphysmodel(lua_State *L){ printf("making bphysmodel\n"); @@ -57,25 +61,37 @@ void makebphysmodel(lua_State *L){ fclose(objfile); printf("About to tinyobj_parse_obj\n"); int err = tinyobj_parse_obj(&attrib, &shapes, &meshcount, &materials, &num_materials, objdata, data_len, TINYOBJ_FLAG_TRIANGULATE); + printf("Finished parsing tinyobj\n"); if(err != TINYOBJ_SUCCESS){ printf("Tinyobj failed to load model:%s\n",ppath); } //u32 meshcount = pmesh->getMeshBufferCount(); + __mingw_printf("attrib.num_vertices: %u\n",attrib.num_vertices); + __mingw_printf("attrib.num_faces: %u\n",attrib.num_faces); btTriangleMesh* trimesh = new btTriangleMesh(); - for(size_t i = 0; i < attrib.num_vertices; i++){ - float *vs = attrib.vertices + (sizeof(float)*3*i);//3 floats per vertex + 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]; + //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); } - for(size_t i = 0; i < attrib.num_faces; i+= 3){ + 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 tinyobj_vertex_index_t i1,i2,i3; 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); } + printf("Finished adding triangle indicies\n"); //size_t numverts = attrib.num_face_num_verts; ////size_t stride = 9; //9 = 3 position floats + 3 normal floats + 3 color floats //size_t face_offset = 0; @@ -197,12 +213,18 @@ int bphysmodel_register(lua_State* L){ //printf("bphysmodel registered\n"); - luaL_newmetatable(L, "phys.physmodel");//{} + luaL_newmetatable(L, "phys.physmodel");//{m_physmodel} + lua_newtable(L);//{m_physmodel},{} + luaL_register(L,NULL,bcollider_m); luaL_register(L,NULL,bphysmodel_m); + luaL_register(L,NULL,brigidbody_m); + lua_setfield(L,-2,"__index");//{m_physmodel} + lua_pop(L,1);// //luaL_register(L,NULL,igeneric_m); //Inherit all the things to do with scene nodes lua_getglobal(L,"phys"); luaL_register(L,NULL,bphysmodel_f); + lua_pop(L,1); - return 1; + return 0; } diff --git a/src/shared/phys/physcommon.cpp b/src/shared/phys/physcommon.cpp index b588b23..379ad55 100644 --- a/src/shared/phys/physcommon.cpp +++ b/src/shared/phys/physcommon.cpp @@ -85,6 +85,7 @@ void UpdatePhysics(double TDeltaTime, void(*f)(btCollisionObject *)) { World->stepSimulation(TDeltaTime * 0.2f, 60); //printf("Done step simulation\n"); if(f){ + //printf("Updating the position of %llu objects\n", Objects.size()); // Relay the object's orientation to irrlicht for(std::list::iterator it = Objects.begin(); it != Objects.end(); ++it) { (*f)(*it); -- cgit v1.2.3-70-g09d2