aboutsummaryrefslogtreecommitdiff
path: root/src/shared/lua_api/phys/bcharactercontroller.cpp
blob: 0f3096c79c2e46e4ffa777e6814b81a4b498103f (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
#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 <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");
  luaL_argcheck(L,ud != NULL, index, "'phys.physbox' expected");
  return (LBPhysNode*) ud;
}
*/

/*
static LISceneNode* checkismesh(lua_State* L){
  return checkismesh(L,1);
}
*/
// 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;
	
	//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}
	//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);
	btTransform transform = btTransform(btQuaternion(0,0,0,1),pos);

	// Create the shape
	btConvexShape* cshape = new btBoxShape(vshape);



	// Add mass
	//btVector3 localinertia = btVector3(0,0,0);
	//shape->calculateLocalInertia(mass, localinertia);

	// Create the rigid body object
	//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);
    
	// Add it to the world
	World->addAction(cc);
	//World->addVehicle(cc);
	//printf("Added rigid body to world: %p\n",World);
	Chars.push_back(cc);

	lua_pushlightuserdata(L,cc);//ud_cc
}

// phys.newphysbox(vector3 size, vector3 origin, double mass)
int newbcharactercontroller(lua_State* L){
	//printf("Createing bphysbox!\n");
	//Create it's lua representation
	makenewbcharactercontroller(L);//ud_cc
	btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1);
	lua_pop(L,1);
	lua_newtable(L);//{}
	lua_pushlightuserdata(L,r);//ud_cc
	lua_setfield(L,-2,"character");//{}

	//Set it's metatable
	luaL_getmetatable(L, "phys.charactercontroller");//{},{phys.charactercontroller}
	lua_setmetatable(L, -2);//{}

  	return 1;
}

//{phys.physbox}:delete()
static int delbcharactercontroller(lua_State* L){//self
	//printf("Attempting to delete physbox\n");
	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;
}

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

static const luaL_reg bcharactercontroller_m[] = {
	{"setwalkdir",          bcharsetwalkdirection},
	{"remove",              delbcharactercontroller},
	{0, 0},
};

void bcharactercontroller_register(lua_State* L){//
	//printf("Registered bphysbox\n");

	luaL_newmetatable(L, "phys.charactercontroller");//{phys.physbox}
	lua_newtable(L);//{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,newbcharactercontroller);//{},newbcharactercontroller()
	lua_setfield(L,-2,"newcharactercontroller");//{}

	lua_pop(L,1);
}