aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/phys/cbphysbox.cpp
blob: 029e6ab07eed1fd581224b4d9cc2054b05ce0fa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <stdlib.h>
#include <list>
extern "C" {
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
}

#include <btBulletDynamicsCommon.h>
#include <irrlicht.h>
#include "cbphysbox.hpp"
#include "../scene/imesh.hpp"
#include "../../../shared/lua_api/phys/bphysbox.hpp"

using namespace irr;
using namespace scene;
using namespace core;
using namespace video;

extern IrrlichtDevice* device;

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},mass)
static int newcbphysbox(lua_State* L){//
	printf("Createing new cbphysbox\n");
	double sx,sy,sz,x,y,z,mass;
	//Get the data
	mass = lua_tonumber(L,-1);//{v3 size}, {v3 origin}, mass
	lua_pop(L,1);//{v3 size}, {v3 origin}
	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}
	lua_pushnumber(L,mass);//{v3 size}, {v3 origin}, mass	
	makenewbphysbox(L);//ud_btRigidbody
	btRigidBody* r = (btRigidBody*)lua_touserdata(L,-1);//ud_btRigidbody
	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);

	lua_newtable(L);//{}
	lua_pushlightuserdata(L,r);//{},ud_rigidbody
	lua_setfield(L,-2,"rigidbody");//{}
	lua_pushlightuserdata(L,n);//{},ud_iscenenode
	lua_setfield(L,-2,"node");//{}

	luaL_getmetatable(L,"phys.physbox");//{},{phys.physbox}
	lua_setmetatable(L,-2);//{}
	
	return 1;
}

//bphysbox:setpos({v3 pos})
int cbphyssetpos(lua_State* L){//{rigidbody=ud_btRigidbody,node=ud_iscenenode},{v3 pos}
	printf("calling cbphysbox setpos\n");
	double x,y,z;
	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
	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}
	lua_getfield(L,-1,"node");//{rigidbody=ud_btRigidbody,node=ud_iscenenode},ud_iscenenode
	ISceneNode* i = (ISceneNode*)lua_touserdata(L,-1);//{btRigidBody=ud_btRigidbody,node=ud_iscenenode},ud_iscenenode
	printf("Got node, it was %p\n",i);
	lua_pop(L,2);//

	btTransform bt;
	btMotionState* ms = r->getMotionState();
	ms->getWorldTransform(bt);
	bt.setOrigin(btVector3(x,y,z));
	ms->setWorldTransform(bt);
	r->activate();

	i->setPosition(vector3df(x,y,z));
	i->updateAbsolutePosition();

	return 0;
	
}

static const luaL_reg cbphysbox_m[] = {
	{"setcpos",	cbphyssetpos},//overload
//	{"delete",	delbphysbox},//client side delete needs to delete the visual representation
	{0, 0},
};

void cbphysbox_register(lua_State* L){
	bphysbox_register(L);//
	lua_getglobal(L,"phys");//{}
	lua_pushcfunction(L,newcbphysbox);//{},newcbphysbox()
	lua_setfield(L,-2,"newcphysbox");//{}

	lua_pop(L,1);//
	
	luaL_getmetatable(L,"phys.physbox");//phys.physbox
	lua_newtable(L);//phys.physbox,{}
	luaL_register(L,NULL,cbphysbox_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);

}