diff options
| author | Alexander <alex@cogarr.net> | 2019-04-24 21:16:08 -0400 |
|---|---|---|
| committer | Alexander <alex@cogarr.net> | 2019-04-24 21:16:08 -0400 |
| commit | 3d60e1432ec43ade4aa61b5a70dd6b8975417e9f (patch) | |
| tree | 954719a0f4a27fe42f9d8113844a21b79ae70f5d /src/shared/lua_api/phys | |
| parent | 42bedce123739287339a95626675ffda3a1ce8e7 (diff) | |
| download | brokengine-3d60e1432ec43ade4aa61b5a70dd6b8975417e9f.tar.gz brokengine-3d60e1432ec43ade4aa61b5a70dd6b8975417e9f.tar.bz2 brokengine-3d60e1432ec43ade4aa61b5a70dd6b8975417e9f.zip | |
updates
Diffstat (limited to 'src/shared/lua_api/phys')
| -rw-r--r-- | src/shared/lua_api/phys/bcharactercontroller.cpp | 145 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bcharactercontroller.hpp | 1 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bcollider.cpp | 44 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bcollider.hpp | 7 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bghostobject.cpp | 185 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bghostobject.hpp | 13 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bphysbox.cpp | 10 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bphysgeneric.cpp | 146 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bphysmodel.cpp | 161 | ||||
| -rw-r--r-- | src/shared/lua_api/phys/bphysmodel.hpp | 1 |
10 files changed, 520 insertions, 193 deletions
diff --git a/src/shared/lua_api/phys/bcharactercontroller.cpp b/src/shared/lua_api/phys/bcharactercontroller.cpp index 8030846..0f3096c 100644 --- a/src/shared/lua_api/phys/bcharactercontroller.cpp +++ b/src/shared/lua_api/phys/bcharactercontroller.cpp @@ -9,11 +9,14 @@ extern "C" { #include <lualib.h> } #include <btBulletDynamicsCommon.h> +#include <BulletDynamics/Character/btKinematicCharacterController.h> +#include <BulletCollision/CollisionDispatch/btGhostObject.h> #include "bcharactercontroller.hpp" #include <shared/lua_api/common.hpp> extern btDiscreteDynamicsWorld* World; extern std::list<btRigidBody*> Objects; +extern std::list<btKinematicCharacterController*> Chars; /* static LBPhysNode* checkisbphysbox(lua_State* L, int index){ void* ud = luaL_checkudata(L,index,"phys.physbox"); @@ -27,14 +30,16 @@ static LISceneNode* checkismesh(lua_State* L){ return checkismesh(L,1); } */ -// ud_btRigidBody :: ({v3 size}, {v3 origin}, double mass) -void makenewbphysbox(lua_State* L){ +// ud_character :: ({v3 size}, {v3 origin}) +void makenewbcharactercontroller(lua_State* L){ + lua_pushstring(L,"Character controller is totally fucking broken for now\n"); + lua_error(L); double px,py,pz; //position double sx,sy,sz; //size - double mass; + //double mass; - mass = lua_tonumber(L,-1);//{v3_size},{v3_origin},mass - lua_pop(L,1);//{v3_size},{v3_origin} + //mass = lua_tonumber(L,-1);//{v3_size},{v3_origin},mass + //lua_pop(L,1);//{v3_size},{v3_origin} //printf("Got mass: %f\n",mass); popvector3d(L,&px,&py,&pz);//{v3_size} @@ -44,132 +49,100 @@ void makenewbphysbox(lua_State* L){ btVector3 vshape = btVector3(sx * 0.5f, sy * 0.5f, sz * 0.5f); //printf("Got size: (%f,%f,%f)\n",sx,sy,sz); btVector3 pos = btVector3(px,py,pz); - - // Set the initial position of the object btTransform transform = btTransform(btQuaternion(0,0,0,1),pos); - //transform.setIdentity(); - //transform.setOrigin(pos); - - // Give it a default MotionState - btDefaultMotionState* motionstate = new btDefaultMotionState(transform); - if(!motionstate){ - //printf("No motionstate\n"); - } + // Create the shape - btCollisionShape* shape = new btBoxShape(vshape); - if(!shape){ - //printf("no shape\n"); - } + btConvexShape* cshape = new btBoxShape(vshape); + + // Add mass - btVector3 localinertia = btVector3(0,0,0); - shape->calculateLocalInertia(mass, localinertia); + //btVector3 localinertia = btVector3(0,0,0); + //shape->calculateLocalInertia(mass, localinertia); // Create the rigid body object - btRigidBody::btRigidBodyConstructionInfo cinfo = btRigidBody::btRigidBodyConstructionInfo( - mass, - motionstate, - shape, - localinertia - ); - btKinematicCharacterController cc = btKinematicCharacterController(btPairCacheingGhostObject(), shape, 0.8, btVector3(0,1,0)); + //btRigidBody::btRigidBodyConstructionInfo cinfo = btRigidBody::btRigidBodyConstructionInfo( + //mass, + //motionstate, + //shape, + //localinertia + //); + btPairCachingGhostObject *ghost = new btPairCachingGhostObject(); + ghost->setWorldTransform(transform); + ghost->setCollisionShape(cshape); + ghost->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT); + btKinematicCharacterController *cc = new btKinematicCharacterController(ghost, cshape, 1, btVector3(0,1,0)); //cinfo.m_friction = 0; //btRigidBody *rigidbody = new btRigidBody(cinfo); - if(!rigidbody){ - //printf("No rigidbody\n"); - } // Add it to the world - World->addVehicle(cc); + World->addAction(cc); + //World->addVehicle(cc); //printf("Added rigid body to world: %p\n",World); - Objects.push_back(cc); + Chars.push_back(cc); - lua_pushlightuserdata(L,rigidbody);//ud_rigidbody + lua_pushlightuserdata(L,cc);//ud_cc } // phys.newphysbox(vector3 size, vector3 origin, double mass) -int newbphysbox(lua_State* L){ +int newbcharactercontroller(lua_State* L){ //printf("Createing bphysbox!\n"); //Create it's lua representation - makenewbphysbox(L);//ud_btRigidBody - btRigidBody* r = (btRigidBody*)lua_touserdata(L,-1); + makenewbcharactercontroller(L);//ud_cc + btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1); lua_pop(L,1); lua_newtable(L);//{} - lua_pushlightuserdata(L,r);//ud_btRigidBody - lua_setfield(L,-2,"rigidbody");//{} + lua_pushlightuserdata(L,r);//ud_cc + lua_setfield(L,-2,"character");//{} //Set it's metatable - luaL_getmetatable(L, "phys.physbox");//{},{phys.physbox} + luaL_getmetatable(L, "phys.charactercontroller");//{},{phys.charactercontroller} lua_setmetatable(L, -2);//{} return 1; } //{phys.physbox}:delete() -static int delbphysbox(lua_State* L){//self +static int delbcharactercontroller(lua_State* L){//self //printf("Attempting to delete physbox\n"); - lua_getfield(L,-1,"rigidbody");//self,ud_rigidbody - btRigidBody* r = (btRigidBody*)lua_touserdata(L,-1);//self,ud_rigidbody - delete r->getCollisionShape(); - delete r->getMotionState(); + lua_getfield(L,-1,"character");//self,ud_rigidbody + btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1);//self,ud_rigidbody + lua_pop(L,2); + delete r->getGhostObject(); delete r; - return 0; } -// physbox:setpos({v3 pos}) -static int bphyssetpos(lua_State *L){//self,{v3 pos} - double nx,ny,nz; - popvector3d(L,&nx,&ny,&nz);//self - - lua_getfield(L,-1,"rigidbody");//self,ud_rigidbody - btRigidBody* i = (btRigidBody*)lua_touserdata(L,-1);//self - btMotionState* ms = i->getMotionState(); - btTransform bt; - ms->getWorldTransform(bt); - - btVector3 to = btVector3(nx,ny,nz); - bt.setOrigin(to); - ms->setWorldTransform(bt); - i->activate(); - - lua_pop(L,1);// +//{char},{v3_dir} :: +int bcharsetwalkdirection(lua_State *L){ + double x,y,z; + popvector3d(L,&x,&y,&z);//{char} + lua_getfield(L,-1,"character");//{char},ud_cc + btKinematicCharacterController* cc = (btKinematicCharacterController*)lua_touserdata(L,-1); + lua_pop(L,2); + cc->setWalkDirection(btVector3(x,y,z)); return 0; } -// {v3 pos} :: physbox:getpos() -static int bphysgetpos(lua_State *L){//self - //printf("Physics box set pos called\n"); - lua_getfield(L,-1,"rigidbody");//self,ud_rigidbody - btRigidBody* i = (btRigidBody*)lua_touserdata(L,-1);//self,ud_rigidbody - btTransform bt = i->getWorldTransform(); - btVector3 bv = bt.getOrigin(); - lua_pop(L,2);// - pushvector3d(L,bv.x(),bv.y(),bv.z());//{} - - return 1; -} - -static const luaL_reg bphysbox_m[] = { - {"getpos", bphysgetpos}, - {"setpos", bphyssetpos}, - {"delete", delbphysbox}, - {0, 0}, +static const luaL_reg bcharactercontroller_m[] = { + {"setwalkdir", bcharsetwalkdirection}, + {"remove", delbcharactercontroller}, + {0, 0}, }; -void bphysbox_register(lua_State* L){// +void bcharactercontroller_register(lua_State* L){// //printf("Registered bphysbox\n"); - luaL_newmetatable(L, "phys.physbox");//{phys.physbox} + luaL_newmetatable(L, "phys.charactercontroller");//{phys.physbox} lua_newtable(L);//{phys.physbox},{} - luaL_register(L,NULL,bphysbox_m);//{phys.physbox},{} + luaL_register(L,NULL,bcharactercontroller_m);//{phys.physbox},{} lua_setfield(L,-2,"__index");//{phys.physbox} lua_pop(L,1);// lua_getglobal(L,"phys");//{} - lua_pushcfunction(L,newbphysbox);//{},newbphysbox() - lua_setfield(L,-2,"newphysbox");//{} + lua_pushcfunction(L,newbcharactercontroller);//{},newbcharactercontroller() + lua_setfield(L,-2,"newcharactercontroller");//{} lua_pop(L,1); } diff --git a/src/shared/lua_api/phys/bcharactercontroller.hpp b/src/shared/lua_api/phys/bcharactercontroller.hpp index 10d71df..d6048a5 100644 --- a/src/shared/lua_api/phys/bcharactercontroller.hpp +++ b/src/shared/lua_api/phys/bcharactercontroller.hpp @@ -8,3 +8,4 @@ extern "C" { #include <irrlicht.h> void bcharactercontroller_register(lua_State* L); +void makenewbcharactercontroller(lua_State* L); diff --git a/src/shared/lua_api/phys/bcollider.cpp b/src/shared/lua_api/phys/bcollider.cpp new file mode 100644 index 0000000..356c504 --- /dev/null +++ b/src/shared/lua_api/phys/bcollider.cpp @@ -0,0 +1,44 @@ +extern "C" { + #include <lua.h> + #include <lauxlib.h> + #include <lualib.h> +} +#include <btBulletDynamicsCommon.h> +#include <shared/lua_api/common.hpp> +#include "bcollider.hpp" + + +/*Collider things from lua have the form of: +{ + type = "ghost" | "multi" | "rigidbody" | "softbody" + collider = ud_btCollisionObject, + node = ud_ISceneNode, --Optional, on client +} +*/ +btCollisionObject* popCollider(lua_State *L){ + lua_getfield(L,-1,"collider"); + btCollisionObject *r = (btCollisionObject*)lua_touserdata(L,-1); + lua_pop(L,2); + return r; +} + +/*** +Activates this object. +If this object was sleeping, it will move again. If you are using +applyforce or setvelocity, you will need to activate() the rigidbody for it +to move. +@function collider:activate() +*/ +//collider:activate() +int activate(lua_State *L){ + btCollisionObject *r = popCollider(L); + + r->activate(); + + return 0; +} + +extern const luaL_reg bcollider_m[] = { + {"activate", activate}, + {NULL, NULL} +}; diff --git a/src/shared/lua_api/phys/bcollider.hpp b/src/shared/lua_api/phys/bcollider.hpp new file mode 100644 index 0000000..3882df6 --- /dev/null +++ b/src/shared/lua_api/phys/bcollider.hpp @@ -0,0 +1,7 @@ +extern "C" { + #include <lua.h> + #include <lauxlib.h> + #include <lualib.h> +} + +extern const luaL_reg bcollider_m[]; diff --git a/src/shared/lua_api/phys/bghostobject.cpp b/src/shared/lua_api/phys/bghostobject.cpp new file mode 100644 index 0000000..da9406d --- /dev/null +++ b/src/shared/lua_api/phys/bghostobject.cpp @@ -0,0 +1,185 @@ +#include <stdio.h> +#include <stdlib.h> +#include <list> +extern "C" { + #include <lua.h> + #include <lauxlib.h> + #include <lualib.h> +} +#include <btBulletDynamicsCommon.h> +#include "bghostobject.hpp" +#include <shared/lua_api/common.hpp> + +extern btDiscreteDynamicsWorld* World; +extern std::list<btCollisionObject*> Objects; +//extern std::list<btGhostObject*> Ghosts; + +/* +static LBPhysNode* checkisbphysbox(lua_State* L, int index){ + void* ud = luaL_checkudata(L,index,"phys.physbox"); + luaL_argcheck(L,ud != NULL, index, "'phys.physbox' expected"); + return (LBPhysNode*) ud; +} +*/ + +/* +static LISceneNode* checkismesh(lua_State* L){ + return checkismesh(L,1); +} +*/ +// ud_btGhostObject :: ({v3 size}, {v3 origin}) +void makeghostobject(lua_State* L){ + double px,py,pz; //position + double sx,sy,sz; //size + + popvector3d(L,&px,&py,&pz);//{v3_size} + //printf("Got position: (%f,%f,%f)\n",px,py,pz); + popvector3d(L,&sx,&sy,&sz);// + + btVector3 vshape = btVector3(sx * 0.5f, sy * 0.5f, sz * 0.5f); + //printf("Got size: (%f,%f,%f)\n",sx,sy,sz); + btVector3 pos = btVector3(px,py,pz); + + // Set the initial position of the object + btTransform transform = btTransform(btQuaternion(0,0,0,1),pos); + //transform.setIdentity(); + //transform.setOrigin(pos); + + // Create the shape + btCollisionShape* shape = new btBoxShape(vshape); + if(!shape){ + //printf("no shape\n"); + } + + // Add mass + btVector3 localinertia = btVector3(0,0,0); + shape->calculateLocalInertia(1, localinertia); + + //cinfo.m_friction = 0; + btGhostObject *ghost = new btGhostObject(); + ghost->setCollisionShape(shape); + ghost->setWorldTransform(transform); + 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); + + lua_pushlightuserdata(L,ghost);//ud_ghost +} + +// phys.newghostobject(vector3 size, vector3 origin) +int newghostobject(lua_State* L){ + //printf("Createing bphysbox!\n"); + //Create it's lua representation + makeghostobject(L);//ud_btGhostObject + btGhostObject* ghost = (btGhostObject*)lua_touserdata(L,-1); + lua_pop(L,1); + lua_newtable(L);//{} + lua_pushlightuserdata(L,ghost);//ud_btGhostObject + lua_setfield(L,-2,"collider");//{} + + //Set it's metatable + luaL_getmetatable(L, "phys.ghost");//{},{phys.ghost} + lua_setmetatable(L, -2);//{} + + return 1; +} + +//{phys.physbox}:delete() +static int delbghostobject(lua_State* L){//self + //printf("Attempting to delete physbox\n"); + lua_getfield(L,-1,"collider");//self,ud_rigidbody + btGhostObject* r = (btGhostObject*)lua_touserdata(L,-1);//self,ud_rigidbody + delete r->getCollisionShape(); + delete r; + + return 0; +} + +// physbox:setpos({v3 pos}) +static int bghostsetpos(lua_State *L){//self,{v3 pos} + double nx,ny,nz; + popvector3d(L,&nx,&ny,&nz);//self + + lua_getfield(L,-1,"collider");//self,ud_ghost + btGhostObject *ghost = (btGhostObject*)lua_touserdata(L,-1);//self + btTransform bt = ghost->getWorldTransform(); + + btVector3 to = btVector3(nx,ny,nz); + bt.setOrigin(to); + ghost->setWorldTransform(bt); + ghost->activate(); + + lua_pop(L,1);// + return 0; +} + +// {v3 pos} :: physbox:getpos() +static int bghostgetpos(lua_State *L){//self + //printf("Physics box set pos called\n"); + lua_getfield(L,-1,"collider");//self,ud_ghost + btGhostObject* i = (btGhostObject*)lua_touserdata(L,-1);//self,ud_ghost + btTransform bt = i->getWorldTransform(); + btVector3 bv = bt.getOrigin(); + lua_pop(L,2);// + pushvector3d(L,bv.x(),bv.y(),bv.z());//{} + + return 1; +} + +//ghost:getoverlapping() +int bghostoverlapping(lua_State *L){ + lua_getfield(L,-1,"collider");//{ghost} + btGhostObject *ghost = (btGhostObject*)lua_touserdata(L,-1);//{ghost},ud_ghost + lua_pop(L,2);// + lua_newtable(L);//{} + btAlignedObjectArray<btCollisionObject *> ob = ghost->getOverlappingPairs(); + printf("Getting %d overlapping object\n",ob.size()); + for(int i = 0; i < ob.size(); i++){ + printf("Looking at object %d\n",i); + btCollisionObject *co = ob[i]; + lua_getglobal(L,"phys");//{},{phys} + lua_getfield(L,-1,"colliders");//{},{phys},{phys.colliders} + lua_pushnumber(L,i+1);//}{},{phys},{phys.colliders},i + 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"); + lua_pushstring(L,"Failed to find collider we are overlapping"); + lua_error(L); + } + lua_settable(L,-5);//{i={collider=co}},{phys},{phys.colliders} + lua_pop(L,2);//{i={...}} + } + printf("Finished adding %d overlapping objects to array...\n",(int)lua_objlen(L,-1)); + return 1; +} + +static const luaL_reg bghost_m[] = { + {"getpos", bghostgetpos}, + {"setpos", bghostsetpos}, + {"getoverlapping", bghostoverlapping}, + {"delete", delbghostobject}, + {0, 0}, +}; + +void bghostobject_register(lua_State* L){// + //printf("Registered bphysbox\n"); + + luaL_newmetatable(L, "phys.ghost");//{phys.physbox} + lua_newtable(L);//{phys.physbox},{} + luaL_register(L,NULL,bghost_m);//{phys.physbox},{} + lua_setfield(L,-2,"__index");//{phys.physbox} + + lua_pop(L,1);// + + lua_getglobal(L,"phys");//{} + lua_pushcfunction(L,newghostobject);//{},newghostobject() + lua_setfield(L,-2,"newghostbox");//{} + + lua_pop(L,1); +} diff --git a/src/shared/lua_api/phys/bghostobject.hpp b/src/shared/lua_api/phys/bghostobject.hpp new file mode 100644 index 0000000..5c96f7e --- /dev/null +++ b/src/shared/lua_api/phys/bghostobject.hpp @@ -0,0 +1,13 @@ + +#include <stdio.h> +#include <stdlib.h> +extern "C" { + #include <lua.h> + #include <lauxlib.h> + #include <lualib.h> +} +#include <irrlicht.h> +#include <BulletCollision/CollisionDispatch/btGhostObject.h> + +void bghostobject_register(lua_State* L); +void makeghostobject(lua_State* L); diff --git a/src/shared/lua_api/phys/bphysbox.cpp b/src/shared/lua_api/phys/bphysbox.cpp index 94b025e..a0ad15a 100644 --- a/src/shared/lua_api/phys/bphysbox.cpp +++ b/src/shared/lua_api/phys/bphysbox.cpp @@ -94,7 +94,15 @@ int newbphysbox(lua_State* L){ lua_pop(L,1); lua_newtable(L);//{} lua_pushlightuserdata(L,r);//ud_btRigidBody - lua_setfield(L,-2,"rigidbody");//{} + lua_setfield(L,-2,"collider");//{} + + //Add it to the global list of colliders + lua_getglobal(L,"phys");//{rb},{phys} + lua_getfield(L,-1,"colliders");//{rb},{phys},{phys.colliders} + lua_pushlightuserdata(L,r);//{rb},{phys},{phys.colliders},ud_collider + lua_pushvalue(L,-4);//{rb},{phys},{phys.colliders},ud_collider,{rb} + lua_settable(L,-3);//{rb},{phys},{phys.colliders} + lua_pop(L,2);//{rb} //Set it's metatable luaL_getmetatable(L, "phys.physbox");//{},{phys.physbox} diff --git a/src/shared/lua_api/phys/bphysgeneric.cpp b/src/shared/lua_api/phys/bphysgeneric.cpp index 5faef2d..56320dd 100644 --- a/src/shared/lua_api/phys/bphysgeneric.cpp +++ b/src/shared/lua_api/phys/bphysgeneric.cpp @@ -6,6 +6,7 @@ extern "C" { } #include <btBulletDynamicsCommon.h> #include <shared/lua_api/common.hpp> +#include <shared/phys/physcommon.hpp> /*** @module phys @@ -14,14 +15,34 @@ extern "C" { /*Physics things from lua have the form of: { - rigidbody = btRigidBody, - node = ISceneNode, + type = "rigidbody" + collider = btRigidBody, + node = ISceneNode, --optional, on client } */ btRigidBody* popRigidBody(lua_State *L){ - lua_getfield(L,-1,"rigidbody"); + lua_getfield(L,-1,"type");//{rigidbody},"rigidbody" + if(lua_isnil(L,-1)){ + printf("Could not get type\n"); + lua_pushstring(L,"Tried to call a rigidbody method with something that did not have a \"type\""); + lua_error(L); + } + const char *s = lua_tostring(L,-1); + if(strcmp(s,"rigidbody") != 0){ + printf("Tried to pop rigidbody when it was not a rigidbody!\n"); + lua_pushstring(L,"Tried to call a rigidbody method on a "); + lua_pushstring(L,s); + lua_concat(L,2); + lua_error(L); + } + lua_getfield(L,-2,"collider");//{rigidbody},"rigidbody",ud_rigidbody + if(lua_isnil(L,-1)){ + printf("Failed to get a \"collider\" field\n"); + lua_pushstring(L,"Rigid body object was not set up correctly\n"); + lua_error(L); + } btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + lua_pop(L,3); return r; } /*** @@ -34,12 +55,8 @@ int setgravity(lua_State *L){ double x,y,z; popvector3d(L,&x,&y,&z); - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); - + btRigidBody *r = popRigidBody(L); btVector3 v = btVector3(x,y,z); - r->setGravity(v); return 0; @@ -52,9 +69,7 @@ Gets the direction of gravity on this object. */ //rigidbody:getgravity() int getgravity(lua_State *L){ - lua_getfield(L,-1,"rigidbody");//{rigidbody},ud_rigidbody - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2);// + btRigidBody *r = popRigidBody(L); btVector3 v = r->getGravity(); pushvector3d(L,v.x(),v.y(),v.z()); @@ -81,9 +96,7 @@ int applyforce(lua_State *L){ double x,y,z; popvector3d(L,&x,&y,&z);//{phys} - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + btRigidBody *r = popRigidBody(L); btVector3 v = btVector3(x,y,z); btVector3 o = btVector3(rx,ry,rz); @@ -100,9 +113,7 @@ Gets the damping applied to this rigidbody */ //rigidbody:getldamping() int getlineardamping(lua_State *L){ - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + btRigidBody *r = popRigidBody(L); double damp = r->getLinearDamping(); lua_pushnumber(L,damp); @@ -119,9 +130,7 @@ TODO:What does this actually do? int setangfactor(lua_State *L){ double x,y,z; popvector3d(L,&x,&y,&z); - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + btRigidBody *r = popRigidBody(L); r->setAngularFactor(btVector3(x,y,z)); return 0; } @@ -133,9 +142,7 @@ Gets the angular damping applied to this rigidbody */ //rigidbody:getadamping() int getangulardamping(lua_State *L){ - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + btRigidBody *r = popRigidBody(L); double damp = r->getAngularDamping(); lua_pushnumber(L,damp); @@ -151,8 +158,7 @@ Gets the velocity of this object //rigidbody:getvelocity() int getvelocity(lua_State *L){ btVector3 vel; - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); + btRigidBody *r = popRigidBody(L); vel = r->getLinearVelocity(); pushvector3d(L,(double)vel.x(),(double)vel.y(),(double)vel.z()); @@ -171,32 +177,13 @@ int setvelocity(lua_State *L){ popvector3d(L,&x,&y,&z); btVector3 newvel = btVector3(x,y,z); - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + btRigidBody *r = popRigidBody(L); r->setLinearVelocity(newvel); return 0; } -/*** -Activates this object. -If this object was sleeping, it will move again. If you are using -applyforce or setvelocity, you will need to activate() the rigidbody for it -to move. -@function rigidbody:activate() -*/ -//rigidbody:activate() -int activate(lua_State *L){ - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); - - r->activate(); - - return 0; -} /*** Sets the damping of this object. @@ -211,9 +198,7 @@ int setdamping(lua_State *L){ ldamp = lua_tonumber(L,-2); lua_pop(L,2); - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + btRigidBody *r = popRigidBody(L); r->setDamping(adamp,ldamp); @@ -229,9 +214,7 @@ int setflags(lua_State *L){ int flags = lua_tonumber(L,-1); lua_pop(L,1); - lua_getfield(L,-1,"rigidbody"); - btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1); - lua_pop(L,2); + btRigidBody *r = popRigidBody(L); r->setFlags(flags); @@ -252,6 +235,60 @@ int applyimpulse(lua_State *L){ 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 +*/ +//class BContactResult : public ContactResultCallback +//{ + //public: + //~BContactResult(){ + //printf("Contact result being destroyed\n"); + //} + //bool needsCollision(){ + //return true; + //} + //btScalar addSingleResult(btManifoldPoint point, + //btCollisionObjectWrapper *wrap1, int part1, int index1, + //btCollisionObjectWrapper *wrap2, int part2, int index2 + //){ + //printf("Got single result\n"); + //return 1; + //} +//}; + +//rigidbody:contacttest() +//int testcontact(lua_State *L){//{rigibody} + //printf("Testing contact\n"); + //btRigidBody *r = popRigidBody(L);// + //ContactResultCallback *cr = new BContactResult(); + //World->contactTest(r,cr); + //return 0; +//} + extern const luaL_reg brigidbody_m[] = { {"setgravity", setgravity}, {"applyforce", applyforce}, @@ -259,10 +296,13 @@ extern const luaL_reg brigidbody_m[] = { {"getldamping", getlineardamping}, {"getadamping", getangulardamping}, {"setdamping", setdamping}, - {"activate", activate}, + {"getpos", getpos}, + {"setpos", setpos}, + //{"activate", activate}, {"getvelocity", getvelocity}, {"setvelocity", setvelocity}, {"setangfactor", setangfactor}, {"setflags", setflags}, + //{"testcontact", testcontact}, {NULL, NULL} }; diff --git a/src/shared/lua_api/phys/bphysmodel.cpp b/src/shared/lua_api/phys/bphysmodel.cpp index e9fecbf..55039ba 100644 --- a/src/shared/lua_api/phys/bphysmodel.cpp +++ b/src/shared/lua_api/phys/bphysmodel.cpp @@ -19,39 +19,27 @@ extern "C" { extern btDiscreteDynamicsWorld* World; extern std::list<btRigidBody*> Objects; -//newbphysmodel("graphicfile","physicfile",mass,[,{position}][,{lookat}]) -static int newbphysmodel(lua_State* L){ - printf("Creating bphysmodel\n"); +//"physicfile",mass[,position][,lookat] :: ud_rigidbody +void makebphysmodel(lua_State *L){ + printf("making bphysmodel\n"); int nargs = lua_gettop(L); double lx,ly,lz; double x,y,z; - if(nargs > 4){ - //"graphicsfile","physicsfile",{position},{lookat} + if(nargs > 3){ + //"physicsfile",{position},{lookat} popvector3d(L,&lx,&ly,&lz); } - if(nargs > 3){ - //"graphicsfile","physicsfile",{position} + if(nargs > 2){ + //"physicsfile",{position} popvector3d(L,&x,&y,&z); } - //"graphicsfile","physicsfile" + printf("got arguments for bphysmodel\n"); + //"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(); + lua_pop(L,2); - //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()); tinyobj_attrib_t attrib; tinyobj_shape_t *shapes = NULL; size_t meshcount; @@ -62,43 +50,59 @@ static int newbphysmodel(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); fseek(objfile,0,SEEK_SET); char *objdata = (char*)malloc(sizeof(char)*data_len); fread(objdata, sizeof(char), data_len, objfile); fclose(objfile); - int err = tinyobj_parse_obj(&attrib, &shapes, &meshcount, &materials, &num_materials, objdata, data_len, 0); + printf("About to tinyobj_parse_obj\n"); + int err = tinyobj_parse_obj(&attrib, &shapes, &meshcount, &materials, &num_materials, objdata, data_len, TINYOBJ_FLAG_TRIANGULATE); if(err != TINYOBJ_SUCCESS){ printf("Tinyobj failed to load model:%s\n",ppath); } //u32 meshcount = pmesh->getMeshBufferCount(); btTriangleMesh* trimesh = new btTriangleMesh(); - 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; - for(size_t i = 0; i < numverts; i++){ - for(size_t j = 0; j < (size_t)attrib.face_num_verts[i] / 3; j++){ - float v[3][3]; //this tri - tinyobj_vertex_index_t idx0, idx1, idx2; - idx0 = attrib.faces[face_offset + 3 * j + 0]; - idx1 = attrib.faces[face_offset + 3 * j + 1]; - idx2 = attrib.faces[face_offset + 3 * j + 2]; - for(short k = 0; k < 3; k++){ - int f0, f1, f2; - f0 = idx0.v_idx; - f1 = idx1.v_idx; - f2 = idx2.v_idx; - v[0][k] = attrib.vertices[3 * (size_t)f0 + k]; - v[1][k] = attrib.vertices[3 * (size_t)f1 + k]; - v[2][k] = attrib.vertices[3 * (size_t)f2 + k]; - } - btVector3 b1,b2,b3; - b1 = btVector3(v[0][0],v[0][1],v[0][2]); - b2 = btVector3(v[1][0],v[1][1],v[1][2]); - b3 = btVector3(v[2][0],v[2][1],v[2][2]); - trimesh->addTriangle(b1,b2,b3); - } - face_offset += (size_t)attrib.face_num_verts[i]; + for(size_t i = 0; i < attrib.num_vertices; i++){ + float *vs = attrib.vertices + (sizeof(float)*3*i);//3 floats per vertex + float v1 = vs[0]; + float v2 = vs[1]; + float v3 = vs[2]; + trimesh->findOrAddVertex(btVector3(v1,v2,v3),true); } + for(size_t i = 0; i < attrib.num_faces; i+= 3){ + 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); + } + //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; + //for(size_t i = 0; i < numverts; i++){ + //for(size_t j = 0; j < (size_t)attrib.face_num_verts[i] / 3; j++){ + //float v[3][3]; //this tri + //tinyobj_vertex_index_t idx0, idx1, idx2; + //idx0 = attrib.faces[face_offset + 3 * j + 0]; + //idx1 = attrib.faces[face_offset + 3 * j + 1]; + //idx2 = attrib.faces[face_offset + 3 * j + 2]; + //for(short k = 0; k < 3; k++){ + //int f0, f1, f2; + //f0 = idx0.v_idx; + //f1 = idx1.v_idx; + //f2 = idx2.v_idx; + //v[0][k] = attrib.vertices[3 * (size_t)f0 + k]; + //v[1][k] = attrib.vertices[3 * (size_t)f1 + k]; + //v[2][k] = attrib.vertices[3 * (size_t)f2 + k]; + //} + //btVector3 b1,b2,b3; + //b1 = btVector3(v[0][0],v[0][1],v[0][2]); + //b2 = btVector3(v[1][0],v[1][1],v[1][2]); + //b3 = btVector3(v[2][0],v[2][1],v[2][2]); + //trimesh->addTriangle(b1,b2,b3); + //} + //face_offset += (size_t)attrib.face_num_verts[i]; + //} printf("Done building trimesh\n"); btCollisionShape *shape = new btBvhTriangleMeshShape(trimesh,true); btTransform tr; @@ -109,15 +113,66 @@ static int newbphysmodel(lua_State* L){ 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"); + lua_pushlightuserdata(L,rb);//ud_rigidbody +} + +//newbphysmodel("graphicfile","physicfile",mass[,position][,lookat]) :: ud_rigidbody +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); + }else{ + lx = 1; ly = 1; lz = 1; + } + if(nargs > 3){ + //"graphicsfile","physicsfile",{position} + popvector3d(L,&x,&y,&z); + }else{ + x = 0; y = 0; z = 0; + } + //"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);// + + lua_pushstring(L,ppath);//"phys_path" + 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"); + makebphysmodel(L);//ud_rigidbody + printf("Done making bphysmodel\n"); + btRigidBody *rb = (btRigidBody*)lua_touserdata(L,-1); + printf("bphysnode, createing the physics body\n"); //Create the lua representation - lua_newtable(L); - lua_pushlightuserdata(L,rb); - lua_setfield(L,-2,"rigidbody"); + lua_newtable(L);//{} + lua_pushlightuserdata(L,rb);//{},ud_rigidbody + lua_setfield(L,-2,"collider");//{collider=ud_rigidbody} + lua_pushstring(L,"rigidbody");//{collider=ud_rigidbody},"rigidbody" + lua_setfield(L,-2,"type");//{rb} + + printf("Added collider to lua rep.\n"); + + //Add it to the global list of colliders + lua_getglobal(L,"phys");//{rb},{phys} + lua_getfield(L,-1,"colliders");//{rb},{phys},{phys.colliders} + lua_pushlightuserdata(L,rb);//{rb},{phys},{phys.colliders},ud_collider + lua_pushvalue(L,-4);//{rb},{phys},{phys.colliders},ud_collider,{rb} + lua_settable(L,-3);//{rb},{phys},{phys.colliders} + lua_pop(L,2);//{rb} + + printf("Added collider to phys.colliders\n"); + //lua_pushlightuserdata(L,node); //lua_setfield(L,-2,"node"); luaL_getmetatable(L,"phys.physmodel"); diff --git a/src/shared/lua_api/phys/bphysmodel.hpp b/src/shared/lua_api/phys/bphysmodel.hpp index 50645cc..1d1cd77 100644 --- a/src/shared/lua_api/phys/bphysmodel.hpp +++ b/src/shared/lua_api/phys/bphysmodel.hpp @@ -8,4 +8,5 @@ extern "C" { } int bphysmodel_register(lua_State* L); +void makebphysmodel(lua_State *L); #endif |
