From ececf2c8624f4d95d9413686839f7fa6e5bb5044 Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 2 Jun 2020 08:54:14 -0400 Subject: Add a shape cast Add a raycast like function, which can cast shapes. phys.shapecast(), along with a shape structure needed to call this function. --- src/client/lua_api/phys/cbphysbox.cpp | 2 +- src/client/lua_api/phys/cbphysmodel.cpp | 14 +++--- src/shared/lua_api/load_phys.cpp | 64 ++++++++++++++++++++++++ src/shared/lua_api/phys/bcharactercontroller.cpp | 15 +++--- src/shared/lua_api/phys/bphysmodel.cpp | 36 ++++++------- src/shared/lua_api/phys/bshape.cpp | 52 +++++++++++++++++++ src/shared/lua_api/phys/bshape.hpp | 11 ++++ src/shared/lua_api/phys/btaction.cpp | 62 +++++++++++++++++++++++ src/shared/lua_api/phys/btaction.hpp | 11 ++++ 9 files changed, 234 insertions(+), 33 deletions(-) create mode 100644 src/shared/lua_api/phys/bshape.cpp create mode 100644 src/shared/lua_api/phys/bshape.hpp create mode 100644 src/shared/lua_api/phys/btaction.cpp create mode 100644 src/shared/lua_api/phys/btaction.hpp (limited to 'src') diff --git a/src/client/lua_api/phys/cbphysbox.cpp b/src/client/lua_api/phys/cbphysbox.cpp index e675a04..6763945 100644 --- a/src/client/lua_api/phys/cbphysbox.cpp +++ b/src/client/lua_api/phys/cbphysbox.cpp @@ -33,7 +33,7 @@ extern std::list Objects; //phys.newphysbox({vector3 size},{vector3 origin},mass) static int newcbphysbox(lua_State* L){// - printf("Createing new cbphysbox\n"); + //printf("Createing new cbphysbox\n"); double sx,sy,sz,x,y,z,mass; //Get the data mass = lua_tonumber(L,-1);//{v3 size}, {v3 origin}, mass diff --git a/src/client/lua_api/phys/cbphysmodel.cpp b/src/client/lua_api/phys/cbphysmodel.cpp index ae2559e..e09c62e 100644 --- a/src/client/lua_api/phys/cbphysmodel.cpp +++ b/src/client/lua_api/phys/cbphysmodel.cpp @@ -37,7 +37,7 @@ extern std::list Objects; //newbphysmodel("graphicfile","physicfile",mass,[,{position}][,{lookat}]) static int newbphysmodel(lua_State* L){ - printf("Creating cbphysmodel\n"); + //printf("Creating cbphysmodel\n"); int nargs = lua_gettop(L); double lx,ly,lz; double x,y,z; @@ -63,7 +63,7 @@ static int newbphysmodel(lua_State* L){ ISceneManager *smgr = device->getSceneManager(); - printf("bphysnode, creating the scene node\n"); + //printf("bphysnode, creating the scene node\n"); //Create the scene node //IMeshBuffer *buf = new CDynamicMeshBuffer(E_VERTEX_TYPE::EVT_STANDARD,E_INDEX_TYPE::EIT_16BIT); @@ -209,18 +209,18 @@ static int newbphysmodel(lua_State* L){ ////gmesh->setDirty(); IMesh *gmesh = smgr->getMesh(gpath); - printf("Creating client physbox at %f %f %f\n",x,y,z); + //printf("Creating client physbox at %f %f %f\n",x,y,z); ISceneNode *node = smgr->addMeshSceneNode(gmesh,0,-1,vector3df(x,y,z)); - printf("bphysnode, createing the physics body\n"); + //printf("bphysnode, createing the physics body\n"); //Create the physics body lua_pushstring(L,ppath);//"physpath" lua_pushnumber(L,mass);//"physpath",mass pushvector3d(L,x,y,z);//"physpath",mass,{x,y,z} pushvector3d(L,lx,ly,lz);//"physpath,mass,{x,y,z},{lx,ly,lz} - printf("About to makebphysmodel\n"); + //printf("About to makebphysmodel\n"); makebphysmodel(L);//ud_rigidbody - printf("done makebphysmodel\n"); + //printf("done makebphysmodel\n"); btRigidBody *rb = (btRigidBody*)lua_touserdata(L,-1); rb->setUserPointer(node); @@ -247,7 +247,7 @@ static int newbphysmodel(lua_State* L){ lua_pushvalue(L,-4);//{rb},{phys},{colliders},ud_rb,{rb} lua_settable(L,-3);//{rb},{phys},{phys.colliders} lua_pop(L,2);//{rb} - printf("finished creating the lua representation\n"); + //printf("finished creating the lua representation\n"); return 1; } diff --git a/src/shared/lua_api/load_phys.cpp b/src/shared/lua_api/load_phys.cpp index 7002a09..33a59fe 100644 --- a/src/shared/lua_api/load_phys.cpp +++ b/src/shared/lua_api/load_phys.cpp @@ -2,14 +2,74 @@ #include "phys/bphysbox.hpp" #include "phys/bhingeconstraint.hpp" #include "phys/bcharactercontroller.hpp" +#include "phys/btaction.hpp" +#include "phys/bshape.hpp" #include +#include +#include #include +extern btDiscreteDynamicsWorld* World; + +//phys.shapecast(shape,{fromx,fromy,fromz},{tox,toy,toz}) +//returns { +// hit :: boolean +// pos :: v3 +// normal :: v3 +//} +int shapecast(lua_State *L){ + double fx,fy,fz,tx,ty,tz; + popvector3d(L,&fx,&fy,&fz); + popvector3d(L,&tx,&ty,&tz); + lua_getfield(L,-1,"shape"); + btBoxShape *cs = (btBoxShape*)lua_touserdata(L,-1); + //printf("shape:%p\n",(void*)cs); + btTransform ft = btTransform(btQuaternion(0,0,0),btVector3(fx,fy,fz)); + btTransform tt = btTransform(btQuaternion(0,0,0),btVector3(tx,ty,tz)); + btCollisionWorld::ClosestConvexResultCallback *cb = new btCollisionWorld::ClosestConvexResultCallback(ft.getOrigin(),tt.getOrigin()); + btVector3 hw, hn; + //hw = cb->m_hitPointWorld; + //hn = cb->m_hitNormalWorld; + //printf("before getting results\n\tHasHit:%d\n\tHit:%f,%f,%f\n\tNor:%f,%f,%f\n",cb->hasHit() ? 1 : 0,hw.x(),hw.y(),hw.z(),hn.x(),hn.y(),hn.z()); + World->convexSweepTest(cs,ft,tt,*cb,0.f); + hw = cb->m_hitPointWorld; + hn = cb->m_hitNormalWorld; + //printf("Got sweep results\n\tHasHit:%d\n\tHit:%f,%f,%f\n\tNor:%f,%f,%f\n",cb->hasHit() ? 1 : 0,hw.x(),hw.y(),hw.z(),hn.x(),hn.y(),hn.z()); + lua_newtable(L);//{} + lua_pushboolean(L,cb->hasHit() ? 1 : 0); + lua_setfield(L,-2,"hit"); + pushvector3d(L,hw.x(),hw.y(),hw.z()); + lua_setfield(L,-2,"pos"); + pushvector3d(L,hn.x(),hn.y(),hn.z()); + lua_setfield(L,-2,"normal"); + + return 1; +} + +//phys.aabbcast(v3 from, v3 to, v3 mins, v3 maxs) +int aabbcast(lua_State *L){ + double sx,sy,sz,ex,ey,ez,minx,miny,minz,maxx,maxy,maxz; + popvector3d(L,&maxx,&maxy,&maxz); + popvector3d(L,&minx,&miny,&minz); + popvector3d(L,&ex,&ey,&ez); + popvector3d(L,&sx,&sy,&sz); + btVector3 normal; + btScalar param = btScalar(1.); + bool out = btRayAabb(btVector3(sx,sy,sz),btVector3(ex,ey,ez),btVector3(minx,miny,minz),btVector3(maxx,maxy,maxz),param,normal); + printf("aabbcast:\n\thashit:%d\n\tparam:%f\n\tnormal:%f,%f,%f\n",out ? 1 : 0, param, normal.x(),normal.y(),normal.z()); + lua_pushboolean(L,out?1:0); + return 1; +} + void load_physfuncs(lua_State* L){ printf("Loading phys things\n"); lua_newtable(L);//{} lua_setglobal(L,"phys");// lua_getglobal(L,"phys");//{phys} + lua_pushcfunction(L,shapecast); + lua_setfield(L,-2,"shapecast"); + lua_pushcfunction(L,aabbcast); + lua_setfield(L,-2,"aabbcast"); lua_newtable(L);//{phys},{} lua_setfield(L,-2,"colliders");//{phys} set_const(L,BT_DISABLE_WORLD_GRAVITY); @@ -19,4 +79,8 @@ void load_physfuncs(lua_State* L){ bphysbox_register(L); bhingeconstraint_register(L); bcharactercontroller_register(L); + printf("About to allow makeaction\n"); + baction_register(L); + bshape_register(L); + printf("Done registering makeaction\n"); } diff --git a/src/shared/lua_api/phys/bcharactercontroller.cpp b/src/shared/lua_api/phys/bcharactercontroller.cpp index 4dfe791..f61aca5 100644 --- a/src/shared/lua_api/phys/bcharactercontroller.cpp +++ b/src/shared/lua_api/phys/bcharactercontroller.cpp @@ -101,7 +101,7 @@ void makenewbcharactercontroller(lua_State* L){ World->addCollisionObject(ghost,btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter); //ghost->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT); printf("Character controller created\n"); - btKinematicCharacterController *cc = new btKinematicCharacterController(ghost, cshape, 8, btVector3(0,1,0)); + btKinematicCharacterController *cc = new btKinematicCharacterController(ghost, cshape, 1, btVector3(0,1,0)); //cc->setMaxSlope(3.14 / 4.0); //cinfo.m_friction = 0; @@ -177,9 +177,10 @@ int bcharactersetpos(lua_State *L){ double x,y,z; popvector3d(L,&x,&y,&z); btKinematicCharacterController *c = popCharacter(L); - btTransform t = c->getGhostObject()->getWorldTransform(); - t.setOrigin(btVector3(x,y,z)); - c->getGhostObject()->setWorldTransform(t); + c->warp(btVector3(x,y,z)); + //btTransform t = c->getGhostObject()->getWorldTransform(); + //t.setOrigin(btVector3(x,y,z)); + //c->getGhostObject()->setWorldTransform(t); return 0; } @@ -192,13 +193,13 @@ int bcharacteronground(lua_State *L){ // char:jump(v3 jump) int bcharacterjump(lua_State *L){ - printf("Jump called\n"); + //printf("Jump called\n"); double x,y,z; popvector3d(L,&x,&y,&z); btKinematicCharacterController *c = popCharacter(L); - printf("About to jump\n"); + //printf("About to jump\n"); c->jump(btVector3(x,y,z)); - printf("Done jumping\n"); + //printf("Done jumping\n"); return 0; } diff --git a/src/shared/lua_api/phys/bphysmodel.cpp b/src/shared/lua_api/phys/bphysmodel.cpp index 0515922..d56adb5 100644 --- a/src/shared/lua_api/phys/bphysmodel.cpp +++ b/src/shared/lua_api/phys/bphysmodel.cpp @@ -27,7 +27,7 @@ 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"); + //printf("making bphysmodel\n"); int nargs = lua_gettop(L); double lx,ly,lz; double x,y,z; @@ -39,7 +39,7 @@ void makebphysmodel(lua_State *L){ //"physicsfile",{position} popvector3d(L,&x,&y,&z); } - printf("got arguments for bphysmodel\n"); + //printf("got arguments for bphysmodel\n"); //"physicsfile" double mass = lua_tonumber(L,-1); @@ -56,14 +56,14 @@ void makebphysmodel(lua_State *L){ FILE *objfile = fopen(ppath,"rb"); fseek(objfile,0,SEEK_END); data_len = ftell(objfile); - printf("model data is %d long\n",(int)data_len); + //printf("model data is %d long\n",(int)data_len); fseek(objfile,0,SEEK_SET); char *objdata = (char*)malloc(sizeof(char)*data_len); fread(objdata, sizeof(char), data_len, objfile); fclose(objfile); - printf("About to tinyobj_parse_obj\n"); + //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"); + //printf("Finished parsing tinyobj\n"); if(err != TINYOBJ_SUCCESS){ printf("Tinyobj failed to load model:%s\n",ppath); } @@ -82,7 +82,7 @@ void makebphysmodel(lua_State *L){ float v3 = vs[2]; vertexes[i] = btVector3(v1,v2,v3); } - printf("Finished finding or adding vertexes\n"); + //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]; @@ -93,30 +93,30 @@ void makebphysmodel(lua_State *L){ v2 = vertexes[i2.v_idx]; v3 = vertexes[i3.v_idx]; trimesh->addTriangle(vertexes[i1.v_idx],vertexes[i2.v_idx],vertexes[i3.v_idx],true);//Some triangles are "the wrong way round", - trimesh->addTriangle(vertexes[i3.v_idx],vertexes[i2.v_idx],vertexes[i1.v_idx],true);//double-side all triangles + //trimesh->addTriangle(vertexes[i3.v_idx],vertexes[i2.v_idx],vertexes[i1.v_idx],true);//double-side all triangles } - printf("Finished adding triangle indicies\n"); - printf("Done building trimesh\n"); + //printf("Finished adding triangle indicies\n"); + //printf("Done building trimesh\n"); btGImpactShapeInterface *shape = new btGImpactMeshShape(trimesh); shape->updateBound(); //btCollisionShape *shape = new btBvhTriangleMeshShape(trimesh,true); btTransform tr; tr.setIdentity(); tr.setOrigin(btVector3(x,y,z)); - printf("Created default motion shape\n"); + //printf("Created default motion shape\n"); btDefaultMotionState *ms = new btDefaultMotionState(tr); btVector3 li; shape->calculateLocalInertia(mass, li); btRigidBody *rb = new btRigidBody(mass,ms,shape,li); World->addRigidBody(rb); Objects.push_back(rb); - printf("Rigid body finished\n"); + //printf("Rigid body finished\n"); lua_pushlightuserdata(L,rb);//ud_rigidbody } //newbphysmodel("graphicfile","physicfile",mass[,position][,lookat]) :: ud_rigidbody static int newbphysmodel(lua_State* L){ - printf("Creating bphysmodel\n"); + //printf("Creating bphysmodel\n"); int nargs = lua_gettop(L); double lx,ly,lz; double x,y,z; @@ -143,11 +143,11 @@ static int newbphysmodel(lua_State* L){ lua_pushnumber(L,mass);//"phys_path",double_mass pushvector3d(L,x,y,z);//"phys_path",double_mass,{position} pushvector3d(L,lx,ly,lz);//"phys_path",double_mass,{position},{lookat} - printf("Starting makeing bphysmodel\n"); + //printf("Starting makeing bphysmodel\n"); makebphysmodel(L);//ud_rigidbody - printf("Done making bphysmodel\n"); + //printf("Done making bphysmodel\n"); btRigidBody *rb = (btRigidBody*)lua_touserdata(L,-1); - printf("bphysnode, createing the physics body\n"); + //printf("bphysnode, createing the physics body\n"); //Create the lua representation lua_newtable(L);//{} @@ -156,7 +156,7 @@ static int newbphysmodel(lua_State* L){ lua_pushstring(L,"rigidbody");//{collider=ud_rigidbody},"rigidbody" lua_setfield(L,-2,"type");//{rb} - printf("Added collider to lua rep.\n"); + //printf("Added collider to lua rep.\n"); //Add it to the global list of colliders lua_getglobal(L,"phys");//{rb},{phys} @@ -166,13 +166,13 @@ static int newbphysmodel(lua_State* L){ lua_settable(L,-3);//{rb},{phys},{phys.colliders} lua_pop(L,2);//{rb} - printf("Added collider to phys.colliders\n"); + //printf("Added collider to phys.colliders\n"); //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"); + //printf("finished creating the lua representation\n"); return 1; } diff --git a/src/shared/lua_api/phys/bshape.cpp b/src/shared/lua_api/phys/bshape.cpp new file mode 100644 index 0000000..5533b42 --- /dev/null +++ b/src/shared/lua_api/phys/bshape.cpp @@ -0,0 +1,52 @@ +extern "C" { + #include + #include + #include +} +#include +#include +#include + +extern btDiscreteDynamicsWorld* World; + +//phys.newboxshape({ax,ay,az}) +static int newboxshape(lua_State* L){ + double ax,ay,az; + popvector3d(L,&ax,&ay,&az); + ax *= 0.5; + ay *= 0.5; + az *= 0.5; + btBoxShape *bs = new btBoxShape(btVector3(ax,ay,az)); + printf("Created shape: %p\n",(void*)bs); + lua_newtable(L);//{} + lua_pushlightuserdata(L,bs);//{},ud_bs + lua_setfield(L,-2,"shape");//{} + luaL_getmetatable(L,"phys.shape");//{},{m_shape} + lua_setmetatable(L,-2);//{} + + return 1; +} + +static const luaL_reg bshape_f[] = { + {"newboxshape", newboxshape}, + {0,0}, +}; + +static const luaL_reg bshape_m[] = { + {0,0}, +}; + +int bshape_register(lua_State* L){ + + luaL_newmetatable(L, "phys.shape");//{m_physshape} + lua_newtable(L);//{m_physshape},{} + luaL_register(L,NULL,bshape_m); + lua_setfield(L,-2,"__index");//{m_physshape} + lua_pop(L,1);// + + lua_getglobal(L,"phys");//{} + luaL_register(L,NULL,bshape_f);//{} + lua_pop(L,1);// + + return 0; +} diff --git a/src/shared/lua_api/phys/bshape.hpp b/src/shared/lua_api/phys/bshape.hpp new file mode 100644 index 0000000..ef3c48a --- /dev/null +++ b/src/shared/lua_api/phys/bshape.hpp @@ -0,0 +1,11 @@ +#ifndef _BSHAPE_HPP_ +#include +#include +extern "C" { + #include + #include + #include +} + +int bshape_register(lua_State* L); +#endif diff --git a/src/shared/lua_api/phys/btaction.cpp b/src/shared/lua_api/phys/btaction.cpp new file mode 100644 index 0000000..afd2371 --- /dev/null +++ b/src/shared/lua_api/phys/btaction.cpp @@ -0,0 +1,62 @@ +extern "C" { + #include + #include + #include +} +#include +#include + +#include + +extern btDiscreteDynamicsWorld* World; + +struct BActionItem: public btActionInterface{ + int ref; + lua_State *L; + void updateAction(btCollisionWorld *world, btScalar delta){ + lua_rawgeti(this->L,LUA_REGISTRYINDEX,this->ref);//{} + pusherrorfunc(this->L);//{},errfunc() + lua_getfield(this->L,-2,"action");//{},errfunc(),action() + if(lua_isnil(this->L,-1)){//no .action method + lua_pop(this->L,3); + return; + } + lua_pushvalue(this->L,-3);//{},errfunc(),action(),{} + lua_pushlightuserdata(this->L,world);//{},errfunc(),action(),{},ud_world + lua_pushnumber(this->L,delta);//{},errfunc(),action(),{},ud_world,delta + lua_pcall(this->L,3,0,-4);//{},errfunc() + //printf("error:%d\n",err); + lua_pop(this->L,2); + return; + } + void debugDraw(btIDebugDraw *d){ + //no debug draw I guess + } +}; + +int makeaction(lua_State *L){ + lua_newtable(L);//{} + int r = luaL_ref(L,LUA_REGISTRYINDEX); + lua_rawgeti(L,LUA_REGISTRYINDEX,r); + BActionItem *a = new BActionItem(); + a->ref = r; + a->L = L; + World->addAction(a); + lua_pushlightuserdata(L,a);//{},ud_action + lua_setfield(L,-2,"action");//{} + return 1; +} + +int newaction(lua_State *L){ + lua_newtable(L);//{} + return 0; +} + +int baction_register(lua_State *L){ + lua_getglobal(L,"phys"); + lua_pushcfunction(L,makeaction); + lua_setfield(L,-2,"makeaction"); + + lua_pop(L,1); + return 0; +} diff --git a/src/shared/lua_api/phys/btaction.hpp b/src/shared/lua_api/phys/btaction.hpp new file mode 100644 index 0000000..e295b77 --- /dev/null +++ b/src/shared/lua_api/phys/btaction.hpp @@ -0,0 +1,11 @@ +#ifndef _BTACTION_HPP_ +#include +#include +extern "C" { + #include + #include + #include +} +int baction_register(lua_State* L); +void makeaction(lua_State *L); +#endif -- cgit v1.2.3-70-g09d2