aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/phys/cbphysbox.cpp
blob: 3e72672f8b262ad1df61c509bad0d47a4e3543cd (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#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"
#include "../scene/igeneric.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);

	r->setUserPointer(n);
	
	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 = btTransform(btQuaternion(0,0,0,1), btVector3(x,y,z));
	btMotionState* ms = r->getMotionState();
	ms->setWorldTransform(bt);
	r->setWorldTransform(bt);
	
	r->activate(true);

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

	return 0;
	
}

//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
	btRigidBody* r = (btRigidBody*) lua_touserdata(L,-1);
	lua_pop(L,2);
	btTransform bt = r->getCenterOfMassTransform();
	btVector3 p = bt.getOrigin();
	pushvector3d(L,p.x(),p.y(),p.z());
	return 1;
}

int cbphysgetgravity(lua_State* L){
	lua_getfield(L,-1,"rigidbody");//{rigidbody=ud_btRigidbody,node=ud_iscenenode}, ud_rigidbody
	btRigidBody* r = (btRigidBody*) lua_touserdata(L,-1);
	lua_pop(L,2);
	btVector3 p = r->getGravity();
	pushvector3d(L,p.x(),p.y(),p.z());
	return 1;
}

int cbphysapplygravity(lua_State* L){
	lua_getfield(L,-1,"rigidbody");//{rigidbody=ud_btRigidbody,node=ud_iscenenode}, ud_rigidbody
	btRigidBody* r = (btRigidBody*) lua_touserdata(L,-1);
	lua_pop(L,2);
	r->applyGravity();
	return 0;
}

//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;
}

static const luaL_reg cbphysbox_m[] = {
	{"setcpos",	cbphyssetpos},//overload
	{"getcpos",	cbphysgetpos},
	{"getgravity",  cbphysgetgravity},
	{"applygravity",cbphysapplygravity},
	{"setMaterial", cbsetmaterial},
//	{"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);

}