diff options
| author | Alexander Pickering <alex@cogarr.net> | 2018-11-01 13:53:16 -0400 |
|---|---|---|
| committer | Alexander Pickering <alex@cogarr.net> | 2018-11-01 13:53:16 -0400 |
| commit | 112517494847f0c86f58544cbf4c35c9b7712ab1 (patch) | |
| tree | 115d9ae3da8028e67e498ed8c587d14972b7699b /src/client/lua_api/phys/cbphysmodel.cpp | |
| parent | 77b26fba6e78ac4af2b28b3bbb4646dea5682ed1 (diff) | |
| download | brokengine-112517494847f0c86f58544cbf4c35c9b7712ab1.tar.gz brokengine-112517494847f0c86f58544cbf4c35c9b7712ab1.tar.bz2 brokengine-112517494847f0c86f58544cbf4c35c9b7712ab1.zip | |
Refactored code and added library
Physics code for models now lives in the shared directory.
To get file loading without irrlicht, a single-file header library
(lib/tinyobjloader-c) was added.
Metatables for generics and physics generics have also been
seperated out.
Diffstat (limited to 'src/client/lua_api/phys/cbphysmodel.cpp')
| -rw-r--r-- | src/client/lua_api/phys/cbphysmodel.cpp | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/src/client/lua_api/phys/cbphysmodel.cpp b/src/client/lua_api/phys/cbphysmodel.cpp new file mode 100644 index 0000000..51ff2d8 --- /dev/null +++ b/src/client/lua_api/phys/cbphysmodel.cpp @@ -0,0 +1,135 @@ + +#include <stdio.h> +#include <stdlib.h> +#include <vector> +#include <memory> +#include <map> +#include <functional> +#include <list> +extern "C" { + #include <lua.h> + #include <lauxlib.h> + #include <lualib.h> +} +#include <btBulletDynamicsCommon.h> +#include <irrlicht.h> +#include "../gameparts.hpp" +#include "cbphysbox.hpp" +#include "cbphysmodel.hpp" +#include <client/lua_api/scene/igeneric.hpp> +#include <shared/lua_api/common.hpp> + + +using namespace irr; +using namespace scene; +using namespace core; +using namespace video; + +extern IrrlichtDevice* device; + +extern btDiscreteDynamicsWorld* World; +extern std::list<btRigidBody*> Objects; + +//newbphysmodel("graphicfile","physicfile",mass,[,{position}][,{lookat}]) +static int newbphysmodel(lua_State* L){ + printf("Creating bphysmodel\n"); + int nargs = lua_gettop(L); + double lx,ly,lz; + double x,y,z; + if(nargs > 4){ + //"graphicsfile","physicsfile",{position},{lookat} + popvector3d(L,&lx,&ly,&lz); + } + if(nargs > 3){ + //"graphicsfile","physicsfile",{position} + popvector3d(L,&x,&y,&z); + } + //"graphicsfile","physicsfile" + + double mass = lua_tonumber(L,-1); + const char *ppath = lua_tostring(L,-2); + const char *gpath = lua_tostring(L,-3); + lua_pop(L,3); + + ISceneManager *smgr = device->getSceneManager(); + + printf("bphysnode, creating the scene node\n"); + + //Create the scene node + IMesh *gmesh = smgr->getMesh(gpath); + ISceneNode *node = smgr->addMeshSceneNode(gmesh,0,-1,vector3df(x,y,z)); + + printf("bphysnode, createing the physics body\n"); + //Create the physics body + IMesh *pmesh = smgr->getMesh(ppath); + printf("We have %d mesh buffers\n",pmesh->getMeshBufferCount()); + u32 meshcount = pmesh->getMeshBufferCount(); + btTriangleMesh* trimesh = new btTriangleMesh(); + for(u32 meshnum = 0; meshnum < meshcount; meshnum++){ + IMeshBuffer *b = pmesh->getMeshBuffer(meshnum); + //assert(b->getVertexType() == video::EVT_STANDARD); + u32 bi = b->getIndexCount(); + u16 *indices = b->getIndices(); + for(u32 i = 0; i < bi; i+= 3){ + printf("Getting triangle %u of %u\n",i,bi); + u16 i1 = indices[i]; + u16 i2 = indices[i + 1]; + u16 i3 = indices[i + 2]; + vector3df p1 = b->getPosition(i1); + vector3df p2 = b->getPosition(i2); + vector3df p3 = b->getPosition(i3); + btVector3 b1 = btVector3(p1.X,p1.Y,p1.Z) ; + btVector3 b2 = btVector3(p2.X,p2.Y,p2.Z) ; + btVector3 b3 = btVector3(p3.X,p3.Y,p3.Z) ; + trimesh->addTriangle(b1,b2,b3); + } + } + printf("Done building trimesh\n"); + btCollisionShape *shape = new btBvhTriangleMeshShape(trimesh,true); + btTransform tr; + tr.setIdentity(); + tr.setOrigin(btVector3(x,y,z)); + printf("Created default motion shape\n"); + btDefaultMotionState *ms = new btDefaultMotionState(btTransform(tr)); + btVector3 li; + shape->calculateLocalInertia(mass, li); + btRigidBody *rb = new btRigidBody(mass,ms,shape,li); + rb->setUserPointer((void*) node); + World->addRigidBody(rb); + Objects.push_back(rb); + printf("Rigid body finished\n"); + + //Create the lua representation + lua_newtable(L); + lua_pushlightuserdata(L,rb); + lua_setfield(L,-2,"rigidbody"); + lua_pushlightuserdata(L,node); + lua_setfield(L,-2,"node"); + luaL_getmetatable(L,"phys.physmodel"); + lua_setmetatable(L,-2); + printf("finished creating the lua representation\n"); + + return 1; +} + +static const luaL_reg bphysmodel_f[] = { + {"newphysmodel", newbphysmodel}, + {0,0}, +}; + +static const luaL_reg bphysmodel_m[] = { + {0, 0}, +}; + +int cbphysmodel_register(lua_State* L){ + //printf("bphysmodel registered\n"); + + luaL_newmetatable(L, "phys.physmodel");//{} + luaL_register(L,NULL,bphysmodel_m); + 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); + + return 1; +} |
