diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2017-10-28 18:12:50 -0400 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2017-10-28 18:12:50 -0400 |
| commit | 61c0c9f53d3a57ee7fd5db5faa74c4b51e2da396 (patch) | |
| tree | 7f828d9557aa28ffcb4c7a1b9b3e3326f3ad0170 /src/shared/lua_api/phys/bphysbox.cpp | |
| parent | 33e6b9627e6a46d388d46f2c5b4d15ba7e9f9904 (diff) | |
| download | brokengine-61c0c9f53d3a57ee7fd5db5faa74c4b51e2da396.tar.gz brokengine-61c0c9f53d3a57ee7fd5db5faa74c4b51e2da396.tar.bz2 brokengine-61c0c9f53d3a57ee7fd5db5faa74c4b51e2da396.zip | |
Lots of updates
* Networking is finally working
* Started moveing physics into the shared domain
* Streams now have a readString() and writeString() method
* streams are passed to the lua context for networking
* Refactored cameras and physboxes to use metatables
* Finally wrote the pushvector3* and popvector3* methods
* Fixed a few crashes in ;main
* Deleted a lot of code
Diffstat (limited to 'src/shared/lua_api/phys/bphysbox.cpp')
| -rw-r--r-- | src/shared/lua_api/phys/bphysbox.cpp | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/shared/lua_api/phys/bphysbox.cpp b/src/shared/lua_api/phys/bphysbox.cpp new file mode 100644 index 0000000..be9db07 --- /dev/null +++ b/src/shared/lua_api/phys/bphysbox.cpp @@ -0,0 +1,151 @@ + +#include <stdio.h> +#include <stdlib.h> +#include <list> +extern "C" { + #include <lua.h> + #include <lauxlib.h> + #include <lualib.h> +} +#include <btBulletDynamicsCommon.h> +#include "bphysbox.hpp" +#include "../common.h" + +extern btDiscreteDynamicsWorld* World; +extern std::list<btRigidBody*> Objects; +/* +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); +} +*/ + +// phys.newphysbox(vector3 size, vector3 origin, double mass) +int newbphysbox(lua_State* L){ + printf("Createing bphysbox!\n"); + int nargs = lua_gettop(L); + if(nargs != 3){ + lua_pushfstring(L,"Expected 3 arguments, got %d",nargs); + lua_error(L); + } + double px,py,pz; //position + double sx,sy,sz; //size + double mass; + + mass = lua_tonumber(L,-1);//{v3_size},{v3_origin},mass + lua_pop(L,1);//{v3_size},{v3_origin} + printf("Got mass: %d\n",mass); + + 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; + transform.setIdentity(); + transform.setOrigin(pos); + + // Give it a default MotionState + btDefaultMotionState* motionstate = new btDefaultMotionState(transform); + + // Create the shape + btCollisionShape* shape = new btBoxShape(vshape); + + // Add mass + btVector3 localinertia; + shape->calculateLocalInertia(mass, localinertia); + + // Create the rigid body object + btRigidBody* rigidbody = new btRigidBody(mass, motionstate, shape, localinertia); + + // Add it to the world + World->addRigidBody(rigidbody); + Objects.push_back(rigidbody); + + //Create it's lua representation + lua_newtable(L);//{} + lua_pushlightuserdata(L,rigidbody);//{},ud_rigidbody + lua_setfield(L,-2,"rigidbody");//{} + + //Set it's metatable + luaL_getmetatable(L, "phys.physbox");//{},{phys.physbox} + lua_setmetatable(L, -2);//{} + + return 1; +} + +//{phys.physbox}:delete() +static int delbphysbox(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(); + 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);// + return 0; +} + +// {v3 pos} :: physbox:getpos() +static int bphysgetpos(lua_State *L){//self + 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}, +}; + +void bphysbox_register(lua_State* L){// + + luaL_newmetatable(L, "phys.physbox");//{phys.physbox} + lua_newtable(L);//{phys.physbox},{} + luaL_register(L,NULL,bphysbox_m);//{phys.physbox},{} + lua_setfield(L,-2,"__index");//{phys.physbox} + + lua_pop(L,1);// + + lua_getglobal(L,"phys");//{} + lua_pushcfunction(L,newbphysbox); + lua_setfield(L,-2,"newphysbox"); + +} |
