aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/phys/bphysgeneric.cpp
blob: 0d882d64f25c05d915bcd541be11e3711d7eb101 (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
extern "C" {
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
}
#include <btBulletDynamicsCommon.h>
//#include <irrlicht.h>
#include <shared/lua_api/common.hpp>

//extern IrrlichtDevice* device;

//extern btDiscreteDynamicsWorld* World;
//extern std::list<btRigidBody*> Objects;

/*Physics things have the form of:
{
	rigidbody = btRigidBody,
	node = ISceneNode,
}
*/

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

//apply force at a reletive offset
//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;
}

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


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

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

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