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/shared/lua_api/load_phys.cpp | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'src/shared/lua_api/load_phys.cpp') 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"); } -- cgit v1.2.3-70-g09d2