diff options
| author | Alexander <alex@cogarr.net> | 2019-06-26 16:14:00 -0400 |
|---|---|---|
| committer | Alexander <alex@cogarr.net> | 2019-06-26 16:14:00 -0400 |
| commit | d5cd0c7b4425e25b11a1ceec154a5c752d508a42 (patch) | |
| tree | ef50cd7d419bba30ee08f46c97232b1c8c68d2be /src/shared/lua_api/phys | |
| parent | 3d60e1432ec43ade4aa61b5a70dd6b8975417e9f (diff) | |
| download | brokengine-d5cd0c7b4425e25b11a1ceec154a5c752d508a42.tar.gz brokengine-d5cd0c7b4425e25b11a1ceec154a5c752d508a42.tar.bz2 brokengine-d5cd0c7b4425e25b11a1ceec154a5c752d508a42.zip | |
Major refactor of physics code
Move all the physics code into the shared directory,
and fix the ghost objects (aabb only)
Diffstat (limited to 'src/shared/lua_api/phys')
| -rw-r--r-- | src/shared/lua_api/phys/bcollider.cpp | 47 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bghostobject.cpp | 12 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bhingeconstraint.cpp | 6 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bphysbox.cpp | 1 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bphysgeneric.cpp | 41 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bphysmodel.cpp | 32 |
6 files changed, 93 insertions, 46 deletions
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 <lualib.h> } #include <shared/lua_api/common.hpp> -#include <Irrlicht.h> #include <btBulletDynamicsCommon.h> #include <shared/lua_api/phys/bhingeconstraint.hpp> -using namespace irr; -using namespace scene; -using namespace core; -using namespace video; - extern btDiscreteDynamicsWorld* World; extern std::list<btRigidBody*> 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 <shared/lua_api/common.hpp> #include <shared/util/tinyobj.hpp> +#include <shared/lua_api/phys/bcollider.hpp> +#include <shared/lua_api/phys/bphysgeneric.hpp> + extern btDiscreteDynamicsWorld* World; extern std::list<btRigidBody*> 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; } |
