aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander <alex@cogarr.net>2019-04-24 21:16:08 -0400
committerAlexander <alex@cogarr.net>2019-04-24 21:16:08 -0400
commit3d60e1432ec43ade4aa61b5a70dd6b8975417e9f (patch)
tree954719a0f4a27fe42f9d8113844a21b79ae70f5d
parent42bedce123739287339a95626675ffda3a1ce8e7 (diff)
downloadbrokengine-3d60e1432ec43ade4aa61b5a70dd6b8975417e9f.tar.gz
brokengine-3d60e1432ec43ade4aa61b5a70dd6b8975417e9f.tar.bz2
brokengine-3d60e1432ec43ade4aa61b5a70dd6b8975417e9f.zip
updates
-rw-r--r--Makefile11
-rw-r--r--src/client/callbackhandeler.cpp46
-rw-r--r--src/client/lua_api/gui/iguielement.cpp23
-rw-r--r--src/client/lua_api/load_cphys.cpp7
-rw-r--r--src/client/lua_api/load_video.cpp8
-rw-r--r--src/client/lua_api/phys/cbcharactercontroller.cpp120
-rw-r--r--src/client/lua_api/phys/cbcharactercontroller.hpp10
-rw-r--r--src/client/lua_api/phys/cbphysbox.cpp18
-rw-r--r--src/client/lua_api/phys/cbphysmodel.cpp226
-rw-r--r--src/client/lua_api/scene/icamera.cpp51
-rw-r--r--src/client/lua_api/scene/imesh.cpp7
-rw-r--r--src/client/main.cpp59
-rw-r--r--src/server/lua_api/phys/sbphysmodel.cpp126
-rw-r--r--src/server/main.cpp17
-rw-r--r--src/shared/lua_api/load_net.cpp50
-rw-r--r--src/shared/lua_api/load_phys.cpp6
-rw-r--r--src/shared/lua_api/phys/bcharactercontroller.cpp145
-rw-r--r--src/shared/lua_api/phys/bcharactercontroller.hpp1
-rw-r--r--src/shared/lua_api/phys/bcollider.cpp44
-rw-r--r--src/shared/lua_api/phys/bcollider.hpp7
-rw-r--r--src/shared/lua_api/phys/bghostobject.cpp185
-rw-r--r--src/shared/lua_api/phys/bghostobject.hpp13
-rw-r--r--src/shared/lua_api/phys/bphysbox.cpp10
-rw-r--r--src/shared/lua_api/phys/bphysgeneric.cpp146
-rw-r--r--src/shared/lua_api/phys/bphysmodel.cpp161
-rw-r--r--src/shared/lua_api/phys/bphysmodel.hpp1
-rw-r--r--src/shared/phys/physcommon.cpp38
-rw-r--r--src/shared/phys/physcommon.hpp4
28 files changed, 1152 insertions, 388 deletions
diff --git a/Makefile b/Makefile
index e9ae94b..25dc478 100644
--- a/Makefile
+++ b/Makefile
@@ -149,7 +149,8 @@ CLIENT_BUILD_FOLDERS = $(FSYSTEM_FOLDERS:%=$(BUILD_DIR)/$(CLIENTNAME)/%)
all : $(BINS)
@echo "Done"
-LAPI_S_PHYS = bhingeconstraint bphysbox bphysmodel bphysgeneric
+# The shared stuff
+LAPI_S_PHYS = bhingeconstraint bphysbox bphysmodel bphysgeneric bcharactercontroller bghostobject bcollider
LAPI_S_LOAD = load_phys load_net common stream
LAPI_PATHS_T = $(LAPI_S_PHYS:%=lua_api/phys/%) $(LAPI_S_LOAD:%=lua_api/%)
SHARED_CLIENT_FILES = $(LAPI_PATHS_T) phys/physcommon util/hashmap util/tinyobj
@@ -157,7 +158,7 @@ SHARED_CLIENT_OBJS = $(SHARED_CLIENT_FILES:%=$(BUILD_DIR)/$(CLIENTNAME)/%.o)
# The client-side only stuff
LAPI_C_GUI = iguibutton iguicheckbox iguielement iguiimage iguilabel iguiwindow iguieditbox iguicolorselector iguifiledialog iguispinbox iguitreeview iguicombobox
-LAPI_C_PHYS = cbphysbox cbphysmodel
+LAPI_C_PHYS = cbphysbox cbphysmodel cbcharactercontroller
LAPI_C_SCENE = icamera igeneric ilight imesh
LAPI_C_VIDEO = iimage itexture smaterial
LAPI_C_IO = ifilesystem
@@ -171,7 +172,7 @@ CLIENT_OBJS = $(BUILD_DIR)/$(CLIENTNAME)/main.o $(CLIENT_SRCS) $(SHARED_CLIENT_O
#Compile the client
$(BIN_DIR)/$(CLIENTNAME)/bin/$(EX_PRE)$(CLIENTNAME)$(EXE_EXT) : $(CLIENT_OBJS) $(LIB_OBJS)
@echo "[CLIENT] Building binary $@"
- $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(C_LDFLAGS) $(LIB_OBJS) # -Wl,--verbose
+ @$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(C_LDFLAGS) $(LIB_OBJS) # -Wl,--verbose
# does not have .hpp associated
$(BUILD_DIR)/$(CLIENTNAME)/main.o : $(SRC_DIR)/client/main.cpp
@@ -318,10 +319,10 @@ $(LNNGDIR)/dynamic/Makefile : $(LNNGDIR)/CMakeLists.txt
$(LIB_S_NNG) : $(LNNGDIR)/static/libnng$(STA_LIB_EXT)
cp $< $@
-$(LIB_D_NNG) : $(LNNGDIR)/dynamic/libnng$(DYN_LIB_EXT).a
+$(LIB_D_NNG) : $(LNNGDIR)/dynamic/libnng$(DYN_LIB_EXT)
cp $< $@
-$(LNNGDIR)/static/libnng$(DYN_LIB_EXT).a : $(LNNGDIR)/static/Makefile
+$(LNNGDIR)/static/libnng$(STA_LIB_EXT) : $(LNNGDIR)/static/Makefile
cd $(LNNGDIR)/static && $(MAKE) nng
$(LNNGDIR)/dynamic/libnng$(DYN_LIB_EXT) : $(LNNGDIR)/dynamic/Makefile
diff --git a/src/client/callbackhandeler.cpp b/src/client/callbackhandeler.cpp
index 5d51ba6..3b069a4 100644
--- a/src/client/callbackhandeler.cpp
+++ b/src/client/callbackhandeler.cpp
@@ -38,7 +38,8 @@ GlobalEventReceiver::GlobalEventReceiver(IrrlichtDevice* d){
//device = d;
}
-void callMouse(lua_State* L, const char* funcname, double x, double y, double event){
+int callMouse(lua_State* L, const char* funcname, double x, double y, double event){
+ int consume = 0;
lua_getglobal(L,"GAME");//GAME
pusherrorfunc(L);//{GAME},errfun
lua_getfield(L,-2,funcname);//{GAME},errfunc,funcname?
@@ -46,22 +47,28 @@ void callMouse(lua_State* L, const char* funcname, double x, double y, double ev
lua_pushnumber(L,x);//{GAME},errfunc,func,x
lua_pushnumber(L,y);//{GAME},errfunc,func,x,y
lua_pushnumber(L,event);//{GAME},errfunc,func,x,y,event
- int err = lua_pcall(L,3,0,-4);//{GAME},errfunc
+ int err = lua_pcall(L,3,1,-5);//{GAME},errfunc,consume|err
if(err){
printf("Failed to call GAME.%s\n",funcname);
+ }else{
+ if(!lua_isnil(L,-1)){
+ consume = lua_toboolean(L,-1);
+ lua_pop(L,-1);
+ }
}
lua_pop(L,2);
}else{
//{GAME},errfunc,nil
lua_pop(L,3);//
}
+ return consume;
}
bool GlobalEventReceiver::OnEvent(const SEvent& e){
//lua_State* L = this->L;
EEVENT_TYPE type = e.EventType;
SEvent::SMouseInput se = e.MouseInput;
- //printf("Onevent called:%d\n",(int)type);
+ printf("Onevent called:%d\n",(int)type);
switch (type){
case EET_GUI_EVENT:{
//printf("Gui event\n");
@@ -111,7 +118,7 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){
}
printf("About to push error func");
pusherrorfunc(L);//{guielement},errfunc()
- lua_getfield(L,-2,fieldname);//{guielement},errfunc(),func()
+ lua_getfield(L,-2,fieldname);//{guielement},errfunc(),(func() | nil)
if(lua_isnil(L,-1)){
printf("Element did not have a function %s, returning\n",fieldname);
lua_pop(L,3);//
@@ -119,15 +126,13 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){
}
lua_rawgeti(L,LUA_REGISTRYINDEX,ref);//{guielement},errfunc(),func(),{guielement}
printf("About to pcall in callback.cpp!\n");
- lua_pcall(L,1,1,-3);//{guielement}
- int n = lua_gettop(L);
- if(n > 1){
+ lua_pcall(L,1,1,-3);//{guielement},errfunc(),(nil|true)
+ if(!lua_isnil(L,-1)){
printf("Got an argument back!\n");
int ans = lua_toboolean(L,-1);
- lua_pop(L,n);
return ans;
}
- lua_pop(L,1);//
+ lua_pop(L,3);//
return false;
}
break;
@@ -144,37 +149,37 @@ Detects when the mouse moves across the game.
*/
switch(se.Event){
case EMIE_MOUSE_MOVED:{
- callMouse(L,"onMouseMove",se.X,se.Y,se.Event);
+ return callMouse(L,"onMouseMove",se.X,se.Y,se.Event);
}
break;
case EMIE_LMOUSE_PRESSED_DOWN:
case EMIE_RMOUSE_PRESSED_DOWN:
case EMIE_MMOUSE_PRESSED_DOWN:{
printf("Mouse down\n");
- callMouse(L,"onMouseDown",se.X,se.Y,se.Event);
+ return callMouse(L,"onMouseDown",se.X,se.Y,se.Event);
}
break;
case EMIE_LMOUSE_LEFT_UP:
case EMIE_RMOUSE_LEFT_UP:
case EMIE_MMOUSE_LEFT_UP:{
printf("Mouse up\n");
- callMouse(L,"onMouseUp",se.X,se.Y,se.Event);
+ return callMouse(L,"onMouseUp",se.X,se.Y,se.Event);
}
break;
case EMIE_MOUSE_WHEEL:{
- callMouse(L,"onMouseWheel",se.X,se.Y,se.Wheel);
+ return callMouse(L,"onMouseWheel",se.X,se.Y,se.Wheel);
}
break;
case EMIE_RMOUSE_DOUBLE_CLICK:
case EMIE_MMOUSE_DOUBLE_CLICK:
case EMIE_LMOUSE_DOUBLE_CLICK:{
- callMouse(L,"onDoubleClick",se.X,se.Y,se.Event);
+ return callMouse(L,"onDoubleClick",se.X,se.Y,se.Event);
}
break;
case EMIE_RMOUSE_TRIPLE_CLICK:
case EMIE_MMOUSE_TRIPLE_CLICK:
case EMIE_LMOUSE_TRIPLE_CLICK:{
- callMouse(L,"onTripleClick",se.X,se.Y,se.Event);
+ return callMouse(L,"onTripleClick",se.X,se.Y,se.Event);
}
break;
case EMIE_COUNT:break;
@@ -197,13 +202,14 @@ Detects any key presses from the game.
lua_getglobal(L,"GAME");//{}
lua_getfield(L,-1,"onKeyDown");//{},()|nil
if(!lua_isnil(L,-1)){
+ //printf("onKeyDown not nil, calling...\n");
pusherrorfunc(L);//GAME,GAME.onKeyDown(),errfunc
lua_pushvalue(L,-2);//GAME,GAME.onKeyDown(),errfunc,onKeyDown()
- lua_pushnumber(L,se.Key);
- lua_pushboolean(L,se.PressedDown);
- lua_pushboolean(L,se.Control);
- lua_pushboolean(L,se.Shift);
- lua_pcall(L,4,0,-5);//GAME,GAME.onKeyDown()
+ lua_pushnumber(L,se.Key);//{GAME},GAME.onKeyDown(),errfunc,onKeyDown(),key_number
+ lua_pushboolean(L,se.PressedDown);//{GAME},GAME.onKeyDown(),errfunc,onKeyDown(),key_number,is_down
+ lua_pushboolean(L,se.Control);//{GAME},GAME.onKeyDown(),errfunc,onKeyDown(),key_number,is_down,is_ctrl
+ lua_pushboolean(L,se.Shift);//{GAME},GAME.onKeyDown(),errfunc,onKeyDown(),key_number,is_down,is_ctrl,is_shift
+ lua_pcall(L,4,0,-6);//GAME,GAME.onKeyDown()
lua_pop(L,2);
}else{
lua_pop(L,2);
diff --git a/src/client/lua_api/gui/iguielement.cpp b/src/client/lua_api/gui/iguielement.cpp
index af9d234..9254ba0 100644
--- a/src/client/lua_api/gui/iguielement.cpp
+++ b/src/client/lua_api/gui/iguielement.cpp
@@ -215,6 +215,27 @@ int focus(lua_State *L){
return 0;
}
+//{guielement},bool
+int setelementenabled(lua_State *L){
+ int enable = lua_toboolean(L,-1);
+ lua_pop(L,1);
+ lua_getfield(L,-1,"guielement");
+ IGUIElement *ele = (IGUIElement*)lua_touserdata(L,-1);
+ ele->setEnabled(enable == 1);
+ lua_pop(L,1);
+ return 0;
+}
+
+//{guielement}
+int getelementenabled(lua_State *L){
+ lua_getfield(L,-1,"guielement");
+ IGUIElement *ele = (IGUIElement*)lua_touserdata(L,-1);
+ bool enabled = ele->isEnabled();
+ lua_pushboolean(L,enabled?1:0);
+ lua_pop(L,1);
+ return 1;
+}
+
class guicallback{
private:
@@ -257,6 +278,7 @@ int guigetid(lua_State* L){
return 1;
}
+
extern const luaL_reg iguielement_m[] = {
{"move", moveiguielement},
{"setvisible", setvisible},
@@ -268,5 +290,6 @@ extern const luaL_reg iguielement_m[] = {
{"gettext", getiguitext},
{"remove", removeiguielement},
{"focus", focus},
+ {"enable", setelementenabled},
{NULL, NULL}
};
diff --git a/src/client/lua_api/load_cphys.cpp b/src/client/lua_api/load_cphys.cpp
index ed81408..92eaff0 100644
--- a/src/client/lua_api/load_cphys.cpp
+++ b/src/client/lua_api/load_cphys.cpp
@@ -11,10 +11,13 @@ extern "C" {
#include "../callbackhandeler.hpp"
#include "phys/cbphysbox.hpp"
#include "phys/cbphysmodel.hpp"
+#include "phys/cbcharactercontroller.hpp"
#include <btBulletDynamicsCommon.h>
#include <btBulletCollisionCommon.h>
//#include <shared/phys/physcommon.hpp>
#include <shared/lua_api/common.hpp>
+#include <shared/lua_api/load_phys.hpp>
+#include <shared/lua_api/phys/bghostobject.hpp>
using namespace irr;
using namespace gui;
@@ -41,10 +44,12 @@ int raytest(lua_State *L){
}
void load_cphysfuncs(lua_State* L){
+
//phys things
cbphysbox_register(L);
-
+ cbcharactercontroller_register(L);
cbphysmodel_register(L);
+ bghostobject_register(L);
lua_getglobal(L,"phys");//{}
lua_pushcfunction(L,raytest);//{},raytest()
diff --git a/src/client/lua_api/load_video.cpp b/src/client/lua_api/load_video.cpp
index 1266c4d..e0c7557 100644
--- a/src/client/lua_api/load_video.cpp
+++ b/src/client/lua_api/load_video.cpp
@@ -23,9 +23,9 @@ extern IVideoDriver* driver;
//{texture},{x,y},{sourcerect},{color},use_alpha
int draw2dimage(lua_State* L){
int nargs = lua_gettop(L);
- printf("Drawing a 2d image\n");
+ //printf("Drawing a 2d image\n");
if(nargs == 2){
- printf("2-argument version\n");
+ //printf("2-argument version\n");
long x,y;
popvector2i(L,&x,&y);
lua_getfield(L,-1,"texture");
@@ -33,10 +33,10 @@ int draw2dimage(lua_State* L){
lua_pop(L,2);
driver->draw2DImage(tex,position2d<s32>(x,y));
}else if(nargs == 5){
- printf("5-argument version\n");
+ //printf("5-argument version\n");
int usealpha = lua_toboolean(L,-1);
lua_pop(L,1);
- printf("Got usealpha: %d\n",usealpha);
+ //printf("Got usealpha: %d\n",usealpha);
long r,g,b,a;
popvector4i(L,&r,&g,&b,&a);
long ssx,ssy,sex,sey;
diff --git a/src/client/lua_api/phys/cbcharactercontroller.cpp b/src/client/lua_api/phys/cbcharactercontroller.cpp
new file mode 100644
index 0000000..6a6da96
--- /dev/null
+++ b/src/client/lua_api/phys/cbcharactercontroller.cpp
@@ -0,0 +1,120 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <list>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+
+#include <btBulletDynamicsCommon.h>
+#include <BulletDynamics/Character/btKinematicCharacterController.h>
+#include <irrlicht.h>
+#include "cbphysbox.hpp"
+#include "../scene/imesh.hpp"
+#include "../scene/igeneric.hpp"
+#include <shared/lua_api/phys/bcharactercontroller.hpp>
+#include <shared/lua_api/common.hpp>
+
+#include <shared/lua_api/phys/bphysgeneric.hpp>
+
+using namespace irr;
+using namespace scene;
+using namespace core;
+using namespace video;
+
+extern IrrlichtDevice* device;
+
+extern btDiscreteDynamicsWorld* World;
+extern std::list<btRigidBody*> Objects;
+
+
+//phys.newcharactercontroller({vector3 size},{vector3 origin})
+static int newcbcharactercontroller(lua_State* L){//
+ printf("Createing new cbcharactercontroller\n");
+ double sx,sy,sz,x,y,z;
+ //Get the data
+ popvector3d(L,&x,&y,&z);//{v3 size}
+ popvector3d(L,&sx,&sy,&sz);//
+
+ pushvector3d(L,sx,sy,sz);//{v3 size}
+ pushvector3d(L,x,y,z);//{v3 size},{v3 origin}
+ makenewbcharactercontroller(L);//ud_cc
+ btKinematicCharacterController* cc = (btKinematicCharacterController*)lua_touserdata(L,-1);//ud_cc
+ lua_pop(L,1);
+
+ pushvector3d(L,sx,sy,sz);//{v3 size}
+ pushvector3d(L,x,y,z);//{v3 size},{v3 origin}
+ makenewiscenecube(L);//ud_iscenenode
+ ISceneNode* n = (ISceneNode*)lua_touserdata(L,-1);//ud_iscenenode
+ lua_pop(L,1);
+
+ //cc->setUserPointer(n);//TODO: what does this break?
+
+ lua_newtable(L);//{}
+ lua_pushlightuserdata(L,cc);//{},ud_cc
+ lua_setfield(L,-2,"character");//{}
+ lua_pushlightuserdata(L,n);//{},ud_iscenenode
+ lua_setfield(L,-2,"node");//{}
+
+ luaL_getmetatable(L,"phys.charactercontroller");//{},{phys.charactercontroller}
+ lua_setmetatable(L,-2);//{}
+
+ return 1;
+}
+
+//setMaterial(self,material)
+//int cbsetmaterial(lua_State* L){
+ //printf("Call to setmaterial\n");
+ ////SMaterial* mat = (SMaterial*)lua_touserdata(L,-1);//{node=ud_ISceneNode},ud_IMaterial
+ //ITexture* tex = (ITexture*)lua_touserdata(L,-1);
+ //lua_pop(L,1);//{node=ud_ISceneNode}
+ //printf("About to get field node\n");
+ //lua_getfield(L,-1,"node");//{node=ud_ISceneNode},ud_ISceneNode
+ //printf("After call to field node\n");
+ //ISceneNode* i = (ISceneNode*)lua_touserdata(L,-1);//{node=ud_ISceneNode},ud_ISceneNode
+ //lua_pop(L,2);//
+
+ //lua_pushlightuserdata(L,i);
+ //lua_pushlightuserdata(L,tex);
+ //printf("Finished getting everything for setmaterial\n");
+ //iscenesetmaterial(L);
+
+ //return 0;
+//}
+
+//int cbchar
+
+static const luaL_reg cbcharactercontroller_m[] = {
+ //{"setpos", cbcharsetpos},//overload
+ //{"getpos", cbchargetpos},
+ //{"getgravity", cbphysgetgravity},
+ //{"applygravity",cbphysapplygravity},
+ //{"setMaterial", cbsetmaterial},
+// {"delete", delbphysbox},//client side delete needs to delete the visual representation
+ {0, 0},
+};
+
+void cbcharactercontroller_register(lua_State* L){
+ bcharactercontroller_register(L);//
+ lua_getglobal(L,"phys");//{}
+ lua_pushcfunction(L,newcbcharactercontroller);//{},newcbphysbox()
+ lua_setfield(L,-2,"newccharactercontroller");//{}
+
+ lua_pop(L,1);//
+
+ luaL_getmetatable(L,"phys.charactercontroller");//phys.physbox
+ lua_newtable(L);//phys.physbox,{}
+ luaL_register(L,NULL,brigidbody_m);
+ luaL_register(L,NULL,igeneric_m);
+ luaL_register(L,NULL,cbcharactercontroller_m);//phys.physbox,{}
+ lua_setfield(L,-2,"__index");//phys.physbox
+
+ lua_pop(L,1);
+
+ //printf("When registering physbox, new() is %p\n",newcbphysbox);
+ //printf("setpos is %p\n",cbphyssetpos);
+
+ lua_pop(L,1);
+
+}
diff --git a/src/client/lua_api/phys/cbcharactercontroller.hpp b/src/client/lua_api/phys/cbcharactercontroller.hpp
new file mode 100644
index 0000000..d7b76eb
--- /dev/null
+++ b/src/client/lua_api/phys/cbcharactercontroller.hpp
@@ -0,0 +1,10 @@
+#include <stdio.h>
+#include <stdlib.h>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+
+void cbcharactercontroller_register(lua_State* L);
diff --git a/src/client/lua_api/phys/cbphysbox.cpp b/src/client/lua_api/phys/cbphysbox.cpp
index 86deacf..c5aacd8 100644
--- a/src/client/lua_api/phys/cbphysbox.cpp
+++ b/src/client/lua_api/phys/cbphysbox.cpp
@@ -13,6 +13,7 @@ extern "C" {
#include "cbphysbox.hpp"
#include "../scene/imesh.hpp"
#include <shared/lua_api/phys/bphysbox.hpp>
+#include <shared/lua_api/phys/bcollider.hpp>
#include "../scene/igeneric.hpp"
#include <shared/lua_api/common.hpp>
@@ -56,7 +57,7 @@ static int newcbphysbox(lua_State* L){//
lua_newtable(L);//{}
lua_pushlightuserdata(L,r);//{},ud_rigidbody
- lua_setfield(L,-2,"rigidbody");//{}
+ lua_setfield(L,-2,"collider");//{}
lua_pushlightuserdata(L,n);//{},ud_iscenenode
lua_setfield(L,-2,"node");//{}
@@ -73,7 +74,7 @@ int cbphyssetpos(lua_State* L){//{rigidbody=ud_btRigidbody,node=ud_iscenenode},{
popvector3d(L,&x,&y,&z);//{rigidbody=ud_btRigidbody,node=ud_iscenenode}
//printf("Getting rigidbody\n");
- lua_getfield(L,-1,"rigidbody");//{rigidbody=ud_btRigidbody,node=ud_iscenenode},ud_btRigidbody
+ lua_getfield(L,-1,"collider");//{rigidbody=ud_btRigidbody,node=ud_iscenenode},ud_btRigidbody
btRigidBody* r = (btRigidBody*)lua_touserdata(L,-1);//{rigidbody=ud_btRigidbody,node=ud_iscenenode},ud_btRigidbody
//printf("Got rigidbody, it was %p\n",r);
lua_pop(L,1);//{rigidbody=ud_btRigidbody,node=ud_iscenenode}
@@ -99,7 +100,7 @@ int cbphyssetpos(lua_State* L){//{rigidbody=ud_btRigidbody,node=ud_iscenenode},{
//bphysbox:getpos()
int cbphysgetpos(lua_State* L){//{rigidbody=ud_btRigidbody,node=ud_iscenenode}
//printf("cphysgetpos called, stack size is %d\n",lua_gettop(L));
- lua_getfield(L,-1,"rigidbody");//{rigidbody=ud_btRigidbody,node=ud_iscenenode}, ud_rigidbody
+ lua_getfield(L,-1,"collider");//{rigidbody=ud_btRigidbody,node=ud_iscenenode}, ud_rigidbody
btRigidBody* r = (btRigidBody*) lua_touserdata(L,-1);
lua_pop(L,2);
btTransform bt = r->getCenterOfMassTransform();
@@ -109,7 +110,7 @@ int cbphysgetpos(lua_State* L){//{rigidbody=ud_btRigidbody,node=ud_iscenenode}
}
int cbphysgetgravity(lua_State* L){
- lua_getfield(L,-1,"rigidbody");//{rigidbody=ud_btRigidbody,node=ud_iscenenode}, ud_rigidbody
+ lua_getfield(L,-1,"collider");//{rigidbody=ud_btRigidbody,node=ud_iscenenode}, ud_rigidbody
btRigidBody* r = (btRigidBody*) lua_touserdata(L,-1);
lua_pop(L,2);
btVector3 p = r->getGravity();
@@ -118,7 +119,7 @@ int cbphysgetgravity(lua_State* L){
}
int cbphysapplygravity(lua_State* L){
- lua_getfield(L,-1,"rigidbody");//{rigidbody=ud_btRigidbody,node=ud_iscenenode}, ud_rigidbody
+ lua_getfield(L,-1,"collider");//{rigidbody=ud_btRigidbody,node=ud_iscenenode}, ud_rigidbody
btRigidBody* r = (btRigidBody*) lua_touserdata(L,-1);
lua_pop(L,2);
r->applyGravity();
@@ -148,10 +149,6 @@ int cbsetmaterial(lua_State* L){
static const luaL_reg cbphysbox_m[] = {
{"setpos", cbphyssetpos},//overload
{"getpos", cbphysgetpos},
- //{"getgravity", cbphysgetgravity},
- //{"applygravity",cbphysapplygravity},
- //{"setMaterial", cbsetmaterial},
-// {"delete", delbphysbox},//client side delete needs to delete the visual representation
{0, 0},
};
@@ -165,9 +162,12 @@ void cbphysbox_register(lua_State* L){
luaL_getmetatable(L,"phys.physbox");//phys.physbox
lua_newtable(L);//phys.physbox,{}
+ luaL_register(L,NULL,bcollider_m);
luaL_register(L,NULL,brigidbody_m);
luaL_register(L,NULL,igeneric_m);
luaL_register(L,NULL,cbphysbox_m);//phys.physbox,{}
+ lua_pushstring(L,"rigidbody");
+ lua_setfield(L,-2,"type");
lua_setfield(L,-2,"__index");//phys.physbox
lua_pop(L,1);
diff --git a/src/client/lua_api/phys/cbphysmodel.cpp b/src/client/lua_api/phys/cbphysmodel.cpp
index 51ff2d8..4f4c106 100644
--- a/src/client/lua_api/phys/cbphysmodel.cpp
+++ b/src/client/lua_api/phys/cbphysmodel.cpp
@@ -17,7 +17,10 @@ extern "C" {
#include "cbphysbox.hpp"
#include "cbphysmodel.hpp"
#include <client/lua_api/scene/igeneric.hpp>
+#include <shared/lua_api/phys/bphysmodel.hpp>
+#include <shared/lua_api/phys/bcollider.hpp>
#include <shared/lua_api/common.hpp>
+#include <shared/util/tinyobj.hpp>
using namespace irr;
@@ -32,81 +35,209 @@ extern std::list<btRigidBody*> Objects;
//newbphysmodel("graphicfile","physicfile",mass,[,{position}][,{lookat}])
static int newbphysmodel(lua_State* L){
- printf("Creating bphysmodel\n");
+ printf("Creating cbphysmodel\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"
+ //"graphicsfile","physicsfile",mass
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_pop(L,3);//
ISceneManager *smgr = device->getSceneManager();
printf("bphysnode, creating the scene node\n");
//Create the scene node
- IMesh *gmesh = smgr->getMesh(gpath);
+ //IMeshBuffer *buf = new CDynamicMeshBuffer(E_VERTEX_TYPE::EVT_STANDARD,E_INDEX_TYPE::EIT_16BIT);
+ IMeshBuffer *buf = new SMeshBuffer();
+ tinyobj_attrib_t attrib;
+ printf("At the very beginning, attrib is %p\n",(void*)&attrib);
+ tinyobj_shape_t *shapes = NULL;
+ size_t meshcount;
+ tinyobj_material_t *materials = NULL;
+ size_t num_materials;
+
+ //Read data from the graphics file
+ size_t data_len = 0;
+ FILE *objfile = fopen(gpath,"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);
+
+ //Parse the data into a tinyobj
+ printf("About to tinyobj_parse_obj\n");
+ int err = tinyobj_parse_obj(&attrib, &shapes, &meshcount, &materials, &num_materials, objdata, data_len, TINYOBJ_FLAG_TRIANGULATE);
+ free(objdata);
+
+ //int err = tinyobj_parse_obj(&attrib, &shapes, &meshcount, &materials, &num_materials, objdata, data_len, 0);
+ if(err != TINYOBJ_SUCCESS){
+ printf("Tinyobj failed to load model:%s\n",ppath);
+ }
+ printf("num_shapes: %d\n",(int)meshcount);
+ for(size_t i = 0; i < meshcount; i++){
+ tinyobj_shape_t shape = shapes[i];
+ printf("Shape %d:\n\tname: %s\n\tface_offset: %d\n\tlength: %d\n",(int)i, shape.name, shape.face_offset, shape.length);
+ }
+ //u32 meshcount = pmesh->getMeshBufferCount();
+ //size_t numverts = attrib.num_vertices;
+ //size_t stride = 9; //9 = 3 position floats + 3 normal floats + 3 color floats
+ //size_t face_offset = 0;
+ printf("Obj attrib:\n\tnum_vertices:%d\n\tnum_normals:%d\n\tnum_texcoords:%d\n\tnum_faces:%d\n\tnum_face_num_verts:%d\n",
+ attrib.num_vertices, attrib.num_normals, attrib.num_texcoords, attrib.num_faces, attrib.num_face_num_verts);
+ //S3DVertex verts[attrib.num_faces];
+ //for(size_t i = 0; i < attrib.num_face_num_verts; i++){
+ //printf("verts %d starts at %p\n",(int)i,(void*)&(verts[i]));
+ //}
+ //u16 indicies[attrib.num_faces];
+ size_t index_c = 0;
+ printf("Faces for %d verts:\n",attrib.num_face_num_verts);
+ //int num_faces = attrib.num_faces;
+ //for(int i = 0; i < num_faces; i+=3){
+ ////printf("\t%u verts in face %u\n",attrib.face_num_verts[i],i);
+ //tinyobj_vertex_index_t fac1 = attrib.faces[i + 0];
+ //tinyobj_vertex_index_t fac2 = attrib.faces[i + 1];
+ //tinyobj_vertex_index_t fac3 = attrib.faces[i + 2];
+ ////printf("Verts:\n\tPosition, normal, texture\n\t(%8d %7d %7d)\n\t(%8d %7d %7d)\n\t(%8d %7d %7d)\n",fac1.v_idx,fac1.vn_idx,fac1.vt_idx,fac2.v_idx,fac2.vn_idx,fac2.vt_idx,fac3.v_idx,fac3.vn_idx,fac3.vt_idx);
+
+ //float f1px, f1py, f1pz;
+ //f1px = attrib.vertices[(3 * fac1.v_idx) + 0];
+ //f1py = attrib.vertices[(3 * fac1.v_idx) + 1];
+ //f1pz = attrib.vertices[(3 * fac1.v_idx) + 2];
+ //float f1nx, f1ny, f1nz;
+ //f1nx = attrib.normals[(3 * fac1.vn_idx) + 0];
+ //f1ny = attrib.normals[(3 * fac1.vn_idx) + 1];
+ //f1nz = attrib.normals[(3 * fac1.vn_idx) + 2];
+
+ //float f2px, f2py, f2pz;
+ //f2px = attrib.vertices[(3 * fac2.v_idx) + 0];
+ //f2py = attrib.vertices[(3 * fac2.v_idx) + 1];
+ //f2pz = attrib.vertices[(3 * fac2.v_idx) + 2];
+ //float f2nx, f2ny, f2nz;
+ //f2nx = attrib.normals[(3 * fac2.vn_idx) + 0];
+ //f2ny = attrib.normals[(3 * fac2.vn_idx) + 1];
+ //f2nz = attrib.normals[(3 * fac2.vn_idx) + 2];
+
+ //float f3px, f3py, f3pz;
+ //f3px = attrib.vertices[(3 * fac3.v_idx) + 0];
+ //f3py = attrib.vertices[(3 * fac3.v_idx) + 1];
+ //f3pz = attrib.vertices[(3 * fac3.v_idx) + 2];
+ //float f3nx, f3ny, f3nz;
+ //f3nx = attrib.normals[(3 * fac3.vn_idx) + 0];
+ //f3ny = attrib.normals[(3 * fac3.vn_idx) + 1];
+ //f3nz = attrib.normals[(3 * fac3.vn_idx) + 2];
+
+ /*printf("Triangle %d:\n\t\
+ //Positions:\n\t\t\
+ //(%f %f %f)\n\t\t\
+ //(%f %f %f)\n\t\t\
+ //(%f %f %f)\n\t\
+ //Normals:\n\t\t\
+ //(%f %f %f)\n\t\t\
+ //(%f %f %f)\n\t\t\
+ //(%f %f %f)\n",((int)i)/3,f1px,f1py,f1pz,f2px,f2py,f2pz,f3px,f3py,f3pz,f1nx,f1ny,f1nz,f2nx,f2ny,f2nz,f3nx,f3ny,f3nz);
+ */
+ ////float
+
+ //verts[i + 0] = S3DVertex(f1px,f1py,f1pz,f1nx,f1ny,f1nz,SColor(255,255,255,255),0,0);
+ //verts[i + 1] = S3DVertex(f2px,f2py,f2pz,f2nx,f2ny,f2nz,SColor(255,255,255,255),0,0);
+ //verts[i + 2] = S3DVertex(f3px,f3py,f3pz,f3nx,f3ny,f3nz,SColor(255,255,255,255),0,0);
+ //indicies[index_c] = i;
+ //index_c++;
+ //indicies[index_c] = i + 1;
+ //index_c++;
+ //indicies[index_c] = i + 2;
+ //index_c++;
+ //}
+ S3DVertex verts[attrib.num_vertices];
+ u16 indicies[attrib.num_faces];
+ for(size_t i = 0; i < attrib.num_vertices; i++){
+ float x,y,z;
+ x = attrib.vertices[i * 3];
+ y = attrib.vertices[(i * 3) + 1];
+ z = attrib.vertices[(i * 3) + 2];
+ verts[i] = S3DVertex(x,y,z,0,0,0,SColor(255,255,255,255),0,0);
+ }
+ for(size_t i = 0; i < attrib.num_faces; i ++){
+ indicies[i] = attrib.faces[i].v_idx;
+ }
+ printf("indicies:\n");
+ for(size_t i = 0; i < index_c; i++){
+ printf("%d ",indicies[i]);
+ }
+ printf("\n");
+
+ //for(size_t i = 0; i < attrib.num_faces; i++){
+ //tinyobj_vertex_index_t tface = attrib.faces[i];
+ //printf("Looking at face %d: %d %d %d\n", (int)i, (int)tface.v_idx, (int)tface.vt_idx, (int)tface.vn_idx);
+ //indicies[index_c] = (u16)tface.v_idx;
+ //index_c++;
+ //}
+ buf->append(verts, attrib.num_vertices, indicies, (u32)attrib.num_faces);
+ //buf->recalculateBoundingBox();
+ SMesh *gmesh = new SMesh();
+ gmesh->addMeshBuffer(buf);
+ //gmesh->recalculateBoundingBox();
+ //gmesh->setDirty();
+
+ //IMesh *gmesh = smgr->getMesh(gpath);
+ 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");
//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");
+ 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");
+ makebphysmodel(L);//ud_rigidbody
+ printf("done makebphysmodel\n");
+
+ btRigidBody *rb = (btRigidBody*)lua_touserdata(L,-1);
+ rb->setUserPointer(node);
+ lua_pop(L,1);
//Create the lua representation
- lua_newtable(L);
+ lua_newtable(L);//{}
+
lua_pushlightuserdata(L,rb);
- lua_setfield(L,-2,"rigidbody");
+ lua_setfield(L,-2,"collider");//{rb=ud_rb}
+
+ lua_pushstring(L,"rigidbody");
+ lua_setfield(L,-2,"type");//{rb=ud_rb, type="rigidbody"}
+
lua_pushlightuserdata(L,node);
- lua_setfield(L,-2,"node");
+ lua_setfield(L,-2,"node");//{rb=ud_rb, node=ud_node, type="rigidbody"}
+
luaL_getmetatable(L,"phys.physmodel");
- lua_setmetatable(L,-2);
+ lua_setmetatable(L,-2);//{physnode}
+
+ lua_getglobal(L,"phys");//{rb},{phys}
+ lua_getfield(L,-1,"colliders");//{rb},{phys},{phys.colliders}
+ lua_pushlightuserdata(L,rb);//{rb},{phys},{colliders},ud_rb
+ 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");
return 1;
@@ -125,8 +256,11 @@ int cbphysmodel_register(lua_State* L){
//printf("bphysmodel registered\n");
luaL_newmetatable(L, "phys.physmodel");//{}
- luaL_register(L,NULL,bphysmodel_m);
+ lua_newtable(L);//{physmodel_m},{}
luaL_register(L,NULL,igeneric_m); //Inherit all the things to do with scene nodes
+ luaL_register(L,NULL,bcollider_m);
+ luaL_register(L,NULL,bphysmodel_m);
+ lua_setfield(L,-2,"__index");
lua_getglobal(L,"phys");
luaL_register(L,NULL,bphysmodel_f);
diff --git a/src/client/lua_api/scene/icamera.cpp b/src/client/lua_api/scene/icamera.cpp
index 482bf31..003f2ad 100644
--- a/src/client/lua_api/scene/icamera.cpp
+++ b/src/client/lua_api/scene/icamera.cpp
@@ -42,6 +42,7 @@ static int newiscenemayacamera(lua_State* L){
static int newiscenefpscamera(lua_State* L){//
ISceneManager* smgr = device->getSceneManager();
ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS();
+ //cam->bindTargetAndRotation(false);
lua_newtable(L);//{}
lua_pushlightuserdata(L,cam);//{},ud_cam
lua_setfield(L,-2,"node");
@@ -97,21 +98,52 @@ static int newiscenecamera(lua_State* L){
return 1;
}
+//camera:bind_target(bool) :: nil
+static int icamerabindtarget(lua_State *L){
+ int should_bind = lua_toboolean(L,-1);//{node=ud_cam},bool_shouldbind
+ printf("Bind target called %d\n",should_bind);
+ lua_pop(L,1);//{node=ud_cam}
+ lua_getfield(L,-1,"node");//{node=ud_cam},ud_cam
+ ICameraSceneNode *cam = (ICameraSceneNode*)lua_touserdata(L,-1);
+ lua_pop(L,2);//
+ cam->bindTargetAndRotation(should_bind == 1);
+ return 0;
+}
+
+//camera:gettarget() :: v3f
+static int icameragettarget(lua_State *L){
+ lua_getfield(L,-1,"node");
+ ICameraSceneNode *cam = (ICameraSceneNode*)lua_touserdata(L,-1);
+ lua_pop(L,2);//
+ vector3df targ = cam->getTarget();
+ pushvector3d(L,targ.X, targ.Y, targ.Z);
+ return 1;
+}
+
+//camera:settarget(v3f)
+static int icamerasettarget(lua_State *L){
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);
+ lua_getfield(L,-1,"node");
+ ICameraSceneNode *cam = (ICameraSceneNode*)lua_touserdata(L,-1);
+ lua_pop(L,2);//
+ cam->setTarget(vector3df(x,y,z));
+ return 0;
+}
+
static const luaL_reg icamera_m[] = {
- {"getpos", iscenegetpos},
- {"setpos", iscenesetpos},
- {"getangle", iscenegetangle},
- {"setangle", iscenesetangle},
- {0, 0},
+ {"bindtarget", icamerabindtarget},
+ {"gettarget", icameragettarget},
+ {"settarget", icamerasettarget},
+ {0,0},
};
static const luaL_reg imayacamera_m[] = {
- {0,0},
+ {0,0},
};
static const luaL_reg ifpscamera_m[] = {
- {"getpos", iscenegetpos},
- {0,0},
+ {0,0},
};
void icamera_register(lua_State* L){
@@ -119,6 +151,7 @@ void icamera_register(lua_State* L){
luaL_newmetatable(L, "scene.icamera");//scene.icamera
lua_newtable(L);//scene.icamera, {}
luaL_register(L,NULL,icamera_m);//scene.icamera, {}
+ luaL_register(L,NULL,igeneric_m);//scene.icamera, {}
lua_setfield(L,-2,"__index");//scene.icamera
lua_pop(L,1);//
@@ -126,6 +159,7 @@ void icamera_register(lua_State* L){
lua_newtable(L);//scene.imayascamera,{}
luaL_register(L,NULL,imayacamera_m);//scene.imayascamera,{}
luaL_register(L,NULL,icamera_m);//scene.imayascamera,{}
+ luaL_register(L,NULL,igeneric_m);
lua_setfield(L,-2,"__index");//scene.imayascamera
lua_pop(L,1);//
@@ -133,6 +167,7 @@ void icamera_register(lua_State* L){
lua_newtable(L);//scene.ifpscamera, {}
luaL_register(L,NULL,ifpscamera_m);//scene.ifpscamera,{}
luaL_register(L,NULL,icamera_m);//scene.ifpscamera,{}
+ luaL_register(L,NULL,igeneric_m);
lua_setfield(L,-2,"__index");//scene.ifpscamera
lua_pop(L,1);//
diff --git a/src/client/lua_api/scene/imesh.cpp b/src/client/lua_api/scene/imesh.cpp
index c6ced65..7a72edf 100644
--- a/src/client/lua_api/scene/imesh.cpp
+++ b/src/client/lua_api/scene/imesh.cpp
@@ -82,9 +82,9 @@ int newiscenecube(lua_State* L){//{v3 size}, {v3 origin}
}
static const luaL_reg imesh_m[] = {
- {"setMaterial", iscenesetmaterial},
- {"getpos", iscenegetpos},
- {"setpos", iscenesetpos},
+ //{"setMaterial", iscenesetmaterial},
+ //{"getpos", iscenegetpos},
+ //{"setpos", iscenesetpos},
// {"remove", removeiguielement},
{0, 0},
};
@@ -98,6 +98,7 @@ void imesh_register(lua_State* L){
luaL_newmetatable(L, "scene.imesh");//scene.icamera
lua_newtable(L);//scene.icamera,{}
luaL_register(L,NULL,imesh_m);//scene.icamera,{}
+ luaL_register(L,NULL,igeneric_m);
lua_setfield(L,-2,"__index");//scene.icamera
lua_pop(L,1);//
diff --git a/src/client/main.cpp b/src/client/main.cpp
index 6e515ad..0a82e99 100644
--- a/src/client/main.cpp
+++ b/src/client/main.cpp
@@ -59,10 +59,10 @@ void loadIrrLibs(lua_State* L, IrrlichtDevice* device){
load_iofuncs(L);
}
-void dropRigidBody(btRigidBody* rb){
- ISceneNode *Node = static_cast<ISceneNode*>(rb->getUserPointer());
- if(Node)
- Node->remove();
+void dropCollisionObject(btCollisionObject* rb){
+ ISceneNode *node = static_cast<ISceneNode*>(rb->getUserPointer());
+ if(node)
+ node->remove();
}
void dropChar(btKinematicCharacterController *a){
@@ -71,6 +71,12 @@ void dropChar(btKinematicCharacterController *a){
node->remove();
}
+void dropGhostObject(btGhostObject *ghost){
+ ISceneNode *node = (ISceneNode*)ghost->getUserPointer();
+ if(node)
+ node->remove();
+}
+
// Converts a quaternion to an euler angle
void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler) {
btScalar W = TQuat.getW();
@@ -88,20 +94,22 @@ void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler) {
TEuler *= core::RADTODEG;
}
-void UpdateElement(btRigidBody* TObject){
+void UpdateElement(btCollisionObject* obj){
- if(TObject->getUserPointer() != NULL){
+ if(obj->getUserPointer() != NULL){
//UpdateRender(*Iterator);
- scene::ISceneNode *Node = static_cast<scene::ISceneNode *>((TObject)->getUserPointer());
+ scene::ISceneNode *node = static_cast<scene::ISceneNode *>(obj->getUserPointer());
// Set position
- btVector3 Point = TObject->getCenterOfMassPosition();
- Node->setPosition(core::vector3df((f32)Point[0], (f32)Point[1], (f32)Point[2]));
+ btTransform transform = obj->getWorldTransform();
+ btVector3 pos = transform.getOrigin();
+ node->setPosition(core::vector3df((f32)pos[0], (f32)pos[1], (f32)pos[2]));
- // Set rotation
- btVector3 EulerRotation;
- QuaternionToEuler(TObject->getOrientation(), EulerRotation);
- Node->setRotation(core::vector3df(EulerRotation[0], EulerRotation[1], EulerRotation[2]));
+ //Set rotation
+ btQuaternion rot = transform.getRotation();
+ btVector3 eulerrot;
+ QuaternionToEuler(rot,eulerrot);
+ node->setRotation(core::vector3df(eulerrot[0], eulerrot[1], eulerrot[2]));
}
}
@@ -153,11 +161,13 @@ int main(int argc, char *argv[]){
device->setEventReceiver(&ger);
pusherrorfunc(L);//errfunc
- switch(luaL_loadfile(state,"init.lua")){//errmsg or nothing
+ int err = luaL_loadfile(state,"init.lua");
+ printf("Error loading init.lua: %d\n",err);
+ switch(err){//errmsg or nothing
case 0:
break; //no error
case LUA_ERRSYNTAX:
- printf("Syntax error, failed to load: %s\n%s","../data/init.lua",lua_tostring(L,-1));
+ printf("Syntax error, failed to load: %s\n%s\n","../data/init.lua",lua_tostring(L,-1));
break;
case LUA_ERRMEM:
printf("Failed to allocate memroy\n");
@@ -169,6 +179,7 @@ int main(int argc, char *argv[]){
//errfunc,initfile()
printf("Loaded file\n");
lua_pcall(state,0,0,-2);
+ printf("Finished running init.lua");
//int iErr = luaL_dofile(state,"init.lua");
//if(iErr != 0){
//printf("Failed to open lua file:%s/init.lua\n",path);
@@ -198,17 +209,20 @@ int main(int argc, char *argv[]){
//printf("End gameloop phys\n");
if(device->isWindowActive()){
driver->beginScene(true, true, background);
-
- lua_getglobal(state,"GAME");
- lua_getfield(state,-1,"draw");
+ //printf("Device active, began scene\n");
+ pusherrorfunc(state);
+ lua_getglobal(state,"GAME");//err(),{GAME}
+ lua_getfield(state,-1,"draw");//err(),{GAME},GAME.draw()
if(!lua_isnil(state,-1)){
- lua_call(state,0,0);
- lua_pop(state,1);
- }else{
+ lua_pcall(state,0,0,-3);
lua_pop(state,2);
+ }else{
+ lua_pop(state,3);
}
+ //printf("Finished calling GAME.draw()\n");
smgr->drawAll();
+ //printf("Scene manager drew all\n");
lua_getglobal(state,"GAME");
lua_getfield(state,-1,"drawPostScene");
if(!lua_isnil(state,-1)){
@@ -217,7 +231,9 @@ int main(int argc, char *argv[]){
}else{
lua_pop(state,2);
}
+ //printf("Post draw scene completed\n");
guienv->drawAll();
+ //printf("Gui draw all completed\n");
lua_getglobal(state,"GAME");
lua_getfield(state,-1,"drawPostGui");
if(!lua_isnil(state,-1)){
@@ -226,6 +242,7 @@ int main(int argc, char *argv[]){
}else{
lua_pop(state,2);
}
+ //printf("GAME.drawPostGui completed\n");
driver->endScene();
}else{
device->yield();
diff --git a/src/server/lua_api/phys/sbphysmodel.cpp b/src/server/lua_api/phys/sbphysmodel.cpp
new file mode 100644
index 0000000..4d81b2c
--- /dev/null
+++ b/src/server/lua_api/phys/sbphysmodel.cpp
@@ -0,0 +1,126 @@
+//#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/phys/bphysmodel.hpp>
+//#include <shared/lua_api/phys/bcollider.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);
+ //}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",mass
+
+ //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
+ //lua_pushstring(L,ppath);
+ //lua_pushnumber(L,mass);
+ //pushvector3d(L,x,y,z);
+ //pushvector3d(L,lx,ly,lz);
+ //printf("About to makebphysmodel\n");
+ //makebphysmodel(L);
+ //printf("done makebphysmodel\n");
+
+ //btRigidBody *rb = (btRigidBody*)lua_touserdata(L,-1);
+ ////Create the lua representation
+ //lua_newtable(L);//
+
+ //lua_pushlightuserdata(L,rb);
+ //lua_setfield(L,-2,"collider");//{rb=ud_rb}
+
+ //lua_pushstring(L,"rigidbody");
+ //lua_setfield(L,-2,"type");//{rb=ud_rb, type="rigidbody"}
+
+ //lua_pushlightuserdata(L,node);
+ //lua_setfield(L,-2,"node");//{rb=ud_rb, node=ud_node, type="rigidbody"}
+
+ //luaL_getmetatable(L,"phys.physmodel");
+ //lua_setmetatable(L,-2);
+
+ //lua_getglobal(L,"phys");//{rb},{phys}
+ //lua_getfield(L,-1,"colliders");//{rb},{phys},{colliders}
+ //lua_pushlightuserdata(L,rb);//{rb},{phys},{colliders},ud_rb
+ //lua_pushvalue(L,-4);//{rb},{phys},{colliders},ud_rb,{rb}
+ //lua_settable(L,-3);//{rb},{phys},{colliders}
+ //lua_pop(L,2);//{rb}
+ //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,bcollider_m);
+ //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;
+//}
diff --git a/src/server/main.cpp b/src/server/main.cpp
index b326e5f..c4c4d8e 100644
--- a/src/server/main.cpp
+++ b/src/server/main.cpp
@@ -34,9 +34,15 @@ bool game_active = true;
void dropRigidBody(btRigidBody* rb){
}
+void dropCollisionObject(btCollisionObject* obj){
+}
+
void dropChar(btKinematicCharacterController *a){
}
+void dropGhostObject(btGhostObject *ghost){
+}
+
lua_State* L;
void gameloop(){
gameloop_phys(NULL);
@@ -106,15 +112,16 @@ int main (int argc, char *argv[]){
//std::this_thread::yield();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
//printf("Thread yeild\n");
- lua_getglobal(L,"GAME");//{}
- lua_getfield(L,-1,"tick");//{},function_tick()
+ pusherrorfunc(L);//errfunc()
+ lua_getglobal(L,"GAME");//errfunc(),{}
+ lua_getfield(L,-1,"tick");//errfunc(),{},function_tick()?
if(!lua_isnil(L,-1)){
//printf("Found game tick\n");
- lua_call(L,0,0);
- lua_pop(L,1);
+ lua_pcall(L,0,0,-3);//errfunc(),{}
+ lua_pop(L,2);
}else{
//printf("Did not find tick function\n");
- lua_pop(L,2);
+ lua_pop(L,3);
}
//printf("End of server gameloop\n");
}while(game_active);
diff --git a/src/shared/lua_api/load_net.cpp b/src/shared/lua_api/load_net.cpp
index b6e95bc..04a9239 100644
--- a/src/shared/lua_api/load_net.cpp
+++ b/src/shared/lua_api/load_net.cpp
@@ -422,53 +422,6 @@ void gameloop_net(lua_State* L){
}
//printf("There are %d items left on the lua stack\n",lua_gettop(L));
lua_pop(L,2);
- //for(std::map<nng_socket*,int>::iterator it = netfuncs.begin(); it != netfuncs.end(); ++it){
- ////printf("In for socket %p\n",(void*)(it->first));
- ////char* buf = NULL;
- ////size_t size;
- ////int err = nng_recv(*(it->first), &buf, &size, NNG_FLAG_NONBLOCK | NNG_FLAG_ALLOC);
- //nng_msg *msgp;
- ////printf("About to recvmsg()\n");
- //int err = nng_recvmsg(*(it->first), &msgp, NNG_FLAG_NONBLOCK);
- ////printf("Done recvmsg()\n");
- ////printf("Got bytes: %d EAGAIN:%d\n",bytes,EAGAIN);
- //if(err != 0 && err != NNG_EAGAIN){
- //printf("Net error: %s\n\t err: %d\n\teagain:%d\n",nng_strerror(err),err,NNG_EAGAIN);
- ////lua_pushstring(L,"Failed to receive");
- ////lua_error(L);
- //}else if(err == NNG_EAGAIN){
- ////printf("EAGAIN\n");
- ////do nothing
- //}else{
- //printf("Calling function with stream\n");
- //char* buf = (char*)nng_msg_body(msgp);
- //printf("Got msg body\n");
- //size_t size = nng_msg_len(msgp);
- //printf("Got msg size\n");
- ////find how long until the first null character
- //struct stream* stream = stream_create();
- //stream->length = size;
- //stream->data = buf;
- //stream_print(stream);
- //int f = it->second;
- //if(f == -1){//not set yet
- //lua_pushstring(L,"Got message for a socket without a receiving function");
- //lua_error(L);
- //}
- //lua_rawgeti(L,LUA_REGISTRYINDEX,f);//function(stream)
- //lua_newtable(L);//function(stream),{}
- //lua_pushlightuserdata(L,stream);//func,{},ud_stream
- //lua_setfield(L,-2,"data");//func,{data=stream}
- //luaL_getmetatable(L,"net.stream");//func,{data=stream}
- //lua_setmetatable(L,-2);//func,{stream}
- //lua_call(L,1,0);//
- ////printf("Finished calling gameloop, buf is %p, size is %zu\n",buf,size);
- //nng_msg_free(msgp);
- //printf("called nn_freemsg\n");
- //free(stream);//We manually set stream->data so free_stream would crash here
- //printf("Called free on stream\n");
- //}
- //}
//printf("Done with net game loop\n");
}
@@ -542,7 +495,8 @@ int send(lua_State* L){
lua_call(L,1,0);//socket,
printf("Finished the stream call\n");
lua_pop(L,1);//
- printf("About to nn_send\n\tfd:%p\n\t%s\n\t%ld\n",fd,s->data,s->length);
+ printf("About to nng_send\n\tfd:%p\n\t%s\n\t%ld\n",fd,s->data,s->length);
+ //int err = nng_send(*fd, s->data, s->length,0);
int err = nng_send(*fd, s->data, s->length,0);
printf("Finished nn_send\n");
stream_free(s);
diff --git a/src/shared/lua_api/load_phys.cpp b/src/shared/lua_api/load_phys.cpp
index db35f37..be5529c 100644
--- a/src/shared/lua_api/load_phys.cpp
+++ b/src/shared/lua_api/load_phys.cpp
@@ -1,17 +1,21 @@
#include "load_phys.hpp"
#include "phys/bphysbox.hpp"
#include "phys/bhingeconstraint.hpp"
+#include "phys/bcharactercontroller.hpp"
#include <btBulletDynamicsCommon.h>
#include <shared/lua_api/common.hpp>
void load_physfuncs(lua_State* L){
lua_newtable(L);//{}
lua_setglobal(L,"phys");//
- lua_getglobal(L,"phys");
+ lua_getglobal(L,"phys");//{phys}
+ lua_newtable(L);//{phys},{}
+ lua_setfield(L,-2,"colliders");//{phys}
set_const(L,BT_DISABLE_WORLD_GRAVITY);
set_const(L,BT_ENABLE_GYROSCOPIC_FORCE_EXPLICIT);
set_const(L,BT_ENABLE_GYROSCOPIC_FORCE_IMPLICIT_WORLD);
set_const(L,BT_ENABLE_GYROSCOPIC_FORCE_IMPLICIT_BODY);
bphysbox_register(L);
bhingeconstraint_register(L);
+ bcharactercontroller_register(L);
}
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
diff --git a/src/shared/phys/physcommon.cpp b/src/shared/phys/physcommon.cpp
index 783f282..b588b23 100644
--- a/src/shared/phys/physcommon.cpp
+++ b/src/shared/phys/physcommon.cpp
@@ -9,11 +9,10 @@
using namespace std::chrono;
btDiscreteDynamicsWorld* World;
-std::list<btRigidBody*> Objects;
+std::list<btCollisionObject*> Objects;
std::list<btKinematicCharacterController*> Chars;
-extern void dropRigidBody(btRigidBody* b);
-extern void dropChar(btKinematicCharacterController* a);
+extern void dropCollisionObject(btCollisionObject* b);
// Removes all objects from the world
//void ClearObjects(btDiscreteDynamicsWorld* wr, std::list<btRigidBody*> objs, void(*f)(btRigidBody*)) {
@@ -32,30 +31,22 @@ extern void dropChar(btKinematicCharacterController* a);
//objs.clear();
//}
void ClearObjects(){
- for(std::list<btRigidBody *>::iterator itr = Objects.begin(); itr != Objects.end(); ++itr){
- dropRigidBody(*itr);
- World->removeRigidBody(*itr);
+ for(std::list<btCollisionObject*>::iterator itr = Objects.begin(); itr != Objects.end(); ++itr){
+ dropCollisionObject(*itr);
+ World->removeCollisionObject(*itr);
delete *itr;
}
Objects.clear();
}
-void ClearChars(){
- for(std::list<btKinematicCharacterController *>::iterator itr = Chars.begin(); itr != Chars.end(); ++itr){
- dropChar(*itr);
- World->removeVehicle(*itr);
- delete *itr;
- }
- Chars.clear();
-}
-
-btBroadphaseInterface* BroadPhase;
+btBroadphaseInterface* broadphase;
btDefaultCollisionConfiguration* CollisionConfiguration;
btCollisionDispatcher* Dispatcher;
btSequentialImpulseConstraintSolver* Solver;
void phys_genesis(){
- BroadPhase = new btAxisSweep3(btVector3(-1000,-1000,-1000),btVector3(1000,1000,1000));
+ broadphase = new btAxisSweep3(btVector3(-1000,-1000,-1000),btVector3(1000,1000,1000));
+ broadphase ->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
//printf("Broadphase\n");
CollisionConfiguration = new btDefaultCollisionConfiguration();
//printf("Collision config\n");
@@ -63,7 +54,7 @@ void phys_genesis(){
//printf("Dispatcher\n");
Solver = new btSequentialImpulseConstraintSolver();
//printf("Solver\n");
- World = new btDiscreteDynamicsWorld(Dispatcher, BroadPhase, Solver, CollisionConfiguration);
+ World = new btDiscreteDynamicsWorld(Dispatcher, broadphase, Solver, CollisionConfiguration);
//printf("Physics world init ok.\n");
World->setGravity(btVector3(0,-10,0));
//printf("Created physics world: %p\n",World);
@@ -71,9 +62,8 @@ void phys_genesis(){
void phys_shutdown(){
ClearObjects();
- ClearChars();
printf("cleared objects\n");
- delete BroadPhase;
+ delete broadphase;
printf("deleted broadphase\n");
delete CollisionConfiguration;
printf("deleted collision config\n");
@@ -90,18 +80,20 @@ void phys_shutdown(){
// Runs the physics simulation.
// - TDeltaTime tells the simulation how much time has passed since the last frame so the simulation can run independently of the frame rate. Optionally pass in an argument that will be called on every rigidbody in the world
-void UpdatePhysics(double TDeltaTime, void(*f)(btRigidBody*)) {
+void UpdatePhysics(double TDeltaTime, void(*f)(btCollisionObject *)) {
+ //printf("Pre step simulation\n");
World->stepSimulation(TDeltaTime * 0.2f, 60);
+ //printf("Done step simulation\n");
if(f){
// Relay the object's orientation to irrlicht
- for(std::list<btRigidBody *>::iterator it = Objects.begin(); it != Objects.end(); ++it) {
+ for(std::list<btCollisionObject *>::iterator it = Objects.begin(); it != Objects.end(); ++it) {
(*f)(*it);
}
}
}
high_resolution_clock::time_point t1 = high_resolution_clock::now();
-void gameloop_phys(void(*f)(btRigidBody*)){
+void gameloop_phys(void(*f)(btCollisionObject *)){
//printf("Doing phys gameloop\n");
high_resolution_clock::time_point now = high_resolution_clock::now();
duration<double> delta = now-t1;
diff --git a/src/shared/phys/physcommon.hpp b/src/shared/phys/physcommon.hpp
index dd5fe14..559683f 100644
--- a/src/shared/phys/physcommon.hpp
+++ b/src/shared/phys/physcommon.hpp
@@ -1,7 +1,9 @@
#ifndef _shared_physcommon_
+#define _shared_physcommon_
#include <BulletDynamics/Character/btKinematicCharacterController.h>
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
-void gameloop_phys(void(*f)(btRigidBody*));
+void gameloop_phys(void(*f)(btCollisionObject *));
void phys_genesis();
void phys_shutdown();
+extern btDiscreteDynamicsWorld* World;
#endif