aboutsummaryrefslogtreecommitdiff
path: root/src/shared/lua_api/phys/bphysgeneric.cpp
blob: 5faef2d3b1acd8fa4e106ec580b3c9f11f1ec130 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
extern "C" {
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
}
#include <btBulletDynamicsCommon.h>
#include <shared/lua_api/common.hpp>

/***
@module phys
*/


/*Physics things from lua have the form of:
{
	rigidbody = btRigidBody,
	node = ISceneNode,
}
*/
btRigidBody* popRigidBody(lua_State *L){
	lua_getfield(L,-1,"rigidbody");
	btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1);
	lua_pop(L,2);
	return r;
}
/***
Sets the direction of gravity on this object.
@function rigidbody:setgravity({x,y,z})
@tparam vector3d direction The direction to make gravity point
*/
//rigidbody:setgravity({x,y,z})
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);

	btVector3 v = btVector3(x,y,z);

	r->setGravity(v);

	return 0;
}

/***
Gets the direction of gravity on this object.
@function rigidbody:getgravity()
@treturn vector3d 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);//

	btVector3 v = r->getGravity();
	pushvector3d(L,v.x(),v.y(),v.z());

	return 0;
}

/***
Apply force at a reletive offset.
@function rigidbody:applyforce(direction, offset = {0,0,0})
@tparam vector3d direction The direction of the force to apply
@tparam vector3d offset The offset from the center of gravity to apply the force
*/
//rigidbody:applyforce({x,y,z}[,{rx,ry,rz}])
int applyforce(lua_State *L){
	double rx,ry,rz;
	rx = 0;
	ry = 0;
	rz = 0;
	if(lua_gettop(L) > 2){
		popvector3d(L,&rx,&ry,&rz);//{phys},{x,y,z}
	}

	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);

	btVector3 v = btVector3(x,y,z);
	btVector3 o = btVector3(rx,ry,rz);

	r->applyForce(v,o);
	
	return 0;
}

/***
Gets the damping applied to this rigidbody
@function rigidbody:getldamping()
@treturn number damping The ammount of damping applied to the object's momentum
*/
//rigidbody:getldamping()
int getlineardamping(lua_State *L){
	lua_getfield(L,-1,"rigidbody");
	btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1);
	lua_pop(L,2);

	double damp = r->getLinearDamping();
	lua_pushnumber(L,damp);

	return 1;
}

/***
Sets the angular factor of the rigidbody
TODO:What does this actually do?
@function rigidbody:setangfactor(vec3 dir)
@tparam vector3 dir The direction to set the angular factor
*/
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);
	r->setAngularFactor(btVector3(x,y,z));
	return 0;
}

/***
Gets the angular damping applied to this rigidbody
@function rigidbody:getadamping()
@treturn number damping The ammount of damping applied to angular momentum
*/
//rigidbody:getadamping()
int getangulardamping(lua_State *L){
	lua_getfield(L,-1,"rigidbody");
	btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1);
	lua_pop(L,2);

	double damp = r->getAngularDamping();
	lua_pushnumber(L,damp);

	return 1;
}

/***
Gets the velocity of this object
@function rigidbody:getvelocity()
@treturn vector3 The velocity in each direction
*/
//rigidbody:getvelocity()
int getvelocity(lua_State *L){
	btVector3 vel;
	lua_getfield(L,-1,"rigidbody");
	btRigidBody *r = (btRigidBody*)lua_touserdata(L,-1);

	vel = r->getLinearVelocity();
	pushvector3d(L,(double)vel.x(),(double)vel.y(),(double)vel.z());

	return 1;
}

/***
Sets the velocity of this object
@function rigidbody:setvelocity()
@tparam vector3d direction The ammount on each axis to set the velocity of this object.
*/
//rigidbody:setvelocity({x,y,z})
int setvelocity(lua_State *L){
	double x,y,z;
	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);

	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.
@function rigidbody:setdamping(damping,angular_damping)
@tparam number damping The ammount of damping the object should put on it's movement.
@tparam number angular_damping The ammount of damping the object should put on it's angular momentum
*/
//rigidbody:setdamping(lineardamping, angulardamping)
int setdamping(lua_State *L){
	double adamp,ldamp;
	adamp = lua_tonumber(L,-1);
	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);

	r->setDamping(adamp,ldamp);

	return 0;
}

/***
Sets flags on this rigidbody
@function rigidbody:setflags(flags)
@tparam number flags
*/
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);

	r->setFlags(flags);

	return 0;
}

/***
Apply an impulse to the rigidboy
@function rigidbody:centralimpulse(vec3 impulse)
@tparam vector3 impulse The direction to apply the impulse in
*/
int applyimpulse(lua_State *L){
	double x,y,z,ox,oy,oz;
	popvector3d(L,&x,&y,&z);
	popvector3d(L,&ox,&oy,&oz);
	btRigidBody *r = popRigidBody(L);
	r->applyImpulse(btVector3(x,y,z),btVector3(ox,oy,oz));
	return 0;
}

extern const luaL_reg brigidbody_m[] = {
	{"setgravity",       setgravity},
	{"applyforce",       applyforce},
	{"applyimpulse",     applyimpulse},
	{"getldamping",      getlineardamping},
	{"getadamping",      getangulardamping},
	{"setdamping",       setdamping},
	{"activate",         activate},
	{"getvelocity",      getvelocity},
	{"setvelocity",      setvelocity},
	{"setangfactor",     setangfactor},
	{"setflags",         setflags},
	{NULL, NULL}
};