aboutsummaryrefslogtreecommitdiff
path: root/src/shared/lua_api/phys/bcharactercontroller.cpp
diff options
context:
space:
mode:
authorAlexander <alex@cogarr.net>2020-05-18 13:41:40 -0400
committerAlexander <alex@cogarr.net>2020-05-18 13:41:40 -0400
commit355589a9100c7d08fdc4094ad32eb9852c88fcc4 (patch)
tree9e2d2ce6b736539907fc06fc3acc2c23daadafac /src/shared/lua_api/phys/bcharactercontroller.cpp
parent377ca5d31a35009913a795c8542659e4872d7c35 (diff)
downloadbrokengine-355589a9100c7d08fdc4094ad32eb9852c88fcc4.tar.gz
brokengine-355589a9100c7d08fdc4094ad32eb9852c88fcc4.tar.bz2
brokengine-355589a9100c7d08fdc4094ad32eb9852c88fcc4.zip
various updates
Diffstat (limited to 'src/shared/lua_api/phys/bcharactercontroller.cpp')
-rw-r--r--src/shared/lua_api/phys/bcharactercontroller.cpp197
1 files changed, 174 insertions, 23 deletions
diff --git a/src/shared/lua_api/phys/bcharactercontroller.cpp b/src/shared/lua_api/phys/bcharactercontroller.cpp
index 0f3096c..4dfe791 100644
--- a/src/shared/lua_api/phys/bcharactercontroller.cpp
+++ b/src/shared/lua_api/phys/bcharactercontroller.cpp
@@ -15,8 +15,35 @@ extern "C" {
#include <shared/lua_api/common.hpp>
extern btDiscreteDynamicsWorld* World;
-extern std::list<btRigidBody*> Objects;
+extern std::list<btCollisionObject*> Objects;
extern std::list<btKinematicCharacterController*> Chars;
+
+//{character} :: btKinematicCharacterController*
+btKinematicCharacterController *popCharacter(lua_State *L){
+ lua_getfield(L,-1,"type");//{char},"type"
+ if(lua_isnil(L,-1)){
+ lua_pushstring(L,"Tried to call a character method on something that had not 'type'");
+ lua_error(L);
+ }
+ const char *s = lua_tostring(L,-1);//{char},"type"
+ if(strcmp(s,"character")!= 0){
+ printf("Tried to pop character when it was not a character!\n");
+ lua_pushstring(L,"Tried to call a character method on a ");
+ lua_pushstring(L,s);
+ lua_concat(L,2);
+ lua_error(L);
+ }
+ lua_getfield(L,-2,"character");//{char},"type",ud_character
+ if(lua_isnil(L,-1)){
+ printf("Failed to get a \"character\" field\n");
+ lua_pushstring(L,"Character object was not set up correctly\n");
+ lua_error(L);
+ }
+ btKinematicCharacterController *c = (btKinematicCharacterController*)lua_touserdata(L,-1);
+ lua_pop(L,3);
+ return c;
+
+}
/*
static LBPhysNode* checkisbphysbox(lua_State* L, int index){
void* ud = luaL_checkudata(L,index,"phys.physbox");
@@ -32,8 +59,9 @@ static LISceneNode* checkismesh(lua_State* L){
*/
// 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);
+ printf("Creating new character controller\n");
+ //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;
@@ -43,11 +71,11 @@ void makenewbcharactercontroller(lua_State* L){
//printf("Got mass: %f\n",mass);
popvector3d(L,&px,&py,&pz);//{v3_size}
- //printf("Got position: (%f,%f,%f)\n",px,py,pz);
+ 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);
+ 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);
@@ -70,43 +98,115 @@ void makenewbcharactercontroller(lua_State* L){
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));
+ World->addCollisionObject(ghost,btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
+ //ghost->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
+ printf("Character controller created\n");
+ btKinematicCharacterController *cc = new btKinematicCharacterController(ghost, cshape, 8, btVector3(0,1,0));
+ //cc->setMaxSlope(3.14 / 4.0);
//cinfo.m_friction = 0;
- //btRigidBody *rigidbody = new btRigidBody(cinfo);
// Add it to the world
+ printf("About to add action\n");
World->addAction(cc);
- //World->addVehicle(cc);
+ printf("Finished adding action\n");
//printf("Added rigid body to world: %p\n",World);
- Chars.push_back(cc);
+ printf("Added to Chars\n");
+ //Chars.push_back(cc);
+ //Objects.push_back(ghost);
lua_pushlightuserdata(L,cc);//ud_cc
}
+// char:getvelocity()
+int bcharactergetvelocity(lua_State *L){
+ btKinematicCharacterController *r = popCharacter(L);
+ btVector3 v = r->getLinearVelocity();
+ pushvector3d(L,v.x(),v.y(),v.z());
+ return 1;
+}
+
+// char:setvelocity(v3 vel)
+int bcharactersetvelocity(lua_State *L){
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);
+ btKinematicCharacterController *r = popCharacter(L);
+ r->setLinearVelocity(btVector3(x,y,z));
+ return 0;
+}
+
// 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);
+ btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1);//ud_cc
+ lua_pop(L,1);//
lua_newtable(L);//{}
- lua_pushlightuserdata(L,r);//ud_cc
- lua_setfield(L,-2,"character");//{}
+ lua_pushlightuserdata(L,r);//{},ud_cc
+ lua_setfield(L,-2,"character");//{character=ud_cc}
+
+ lua_pushstring(L,"character");
+ lua_setfield(L,-2,"type");//{character=ud_cc,type="character"}
//Set it's metatable
luaL_getmetatable(L, "phys.charactercontroller");//{},{phys.charactercontroller}
- lua_setmetatable(L, -2);//{}
+ lua_setmetatable(L, -2);//{cc}
return 1;
}
+// char:setgravity(v3 gravity)
+int bcharactersetgravity(lua_State *L){
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);
+ btKinematicCharacterController *c = popCharacter(L);
+ c->setGravity(btVector3(x,y,z));
+ return 0;
+}
+
+// char:getpos() :: v3
+int bcharactergetpos(lua_State *L){
+ btKinematicCharacterController *c = popCharacter(L);
+ btVector3 pos = c->getGhostObject()->getWorldTransform().getOrigin();
+ pushvector3d(L,pos.x(),pos.y(),pos.z());
+ return 1;
+}
+
+// char:setpos(v3 pos)
+int bcharactersetpos(lua_State *L){
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);
+ btKinematicCharacterController *c = popCharacter(L);
+ btTransform t = c->getGhostObject()->getWorldTransform();
+ t.setOrigin(btVector3(x,y,z));
+ c->getGhostObject()->setWorldTransform(t);
+ return 0;
+}
+
+// char:onground()
+int bcharacteronground(lua_State *L){
+ btKinematicCharacterController *c = popCharacter(L);
+ lua_pushboolean(L,c->onGround() == true ? 1 : 0);
+ return 1;
+}
+
+// char:jump(v3 jump)
+int bcharacterjump(lua_State *L){
+ printf("Jump called\n");
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);
+ btKinematicCharacterController *c = popCharacter(L);
+ printf("About to jump\n");
+ c->jump(btVector3(x,y,z));
+ printf("Done jumping\n");
+ return 0;
+}
+
//{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_getfield(L,-1,"character");//self,ud_character
+ btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1);//self,ud_character
lua_pop(L,2);
delete r->getGhostObject();
delete r;
@@ -124,25 +224,76 @@ int bcharsetwalkdirection(lua_State *L){
return 0;
}
-static const luaL_reg bcharactercontroller_m[] = {
+// char:setfallspeed(n)
+int bcharactersetfallspeed(lua_State *L){
+ double speed = luaL_optnumber(L,-1,1);
+ printf("Got number: %f\n",speed);
+ lua_pop(L,1);
+ btKinematicCharacterController *c = popCharacter(L);
+ printf("About to set speed\n");
+ c->setFallSpeed(speed);
+ printf("Done setting speed\n");
+ return 0;
+}
+
+// char:getmaxslope()
+int bcharactergetmaxslope(lua_State *L){
+ btKinematicCharacterController *c = popCharacter(L);
+ btScalar s = c->getMaxSlope();
+ lua_pushnumber(L,s);
+ return 1;
+}
+
+// char:setmaxslope(slope)
+int bcharactersetmaxslope(lua_State *L){
+ btScalar s = lua_tonumber(L,-1);
+ lua_pop(L,1);
+ btKinematicCharacterController *c = popCharacter(L);
+ c->setMaxSlope(s);
+ return 0;
+}
+
+// char:setvelocityforinterval(vec3 {velocity},number interval)
+int bcharactersetvelocityforinterval(lua_State *L){
+ double interval = lua_tonumber(L,-1);
+ lua_pop(L,1);
+ double x,y,z;
+ popvector3d(L,&x,&y,&z);
+ btKinematicCharacterController *c = popCharacter(L);
+ c->setVelocityForTimeInterval(btVector3(x,y,z),interval);
+ return 0;
+}
+
+extern const luaL_reg bcharactercontroller_m[] = {
{"setwalkdir", bcharsetwalkdirection},
{"remove", delbcharactercontroller},
+ {"getvelocity", bcharactergetvelocity},
+ {"setvelocity", bcharactersetvelocity},
+ {"setgravity", bcharactersetgravity},
+ {"getpos", bcharactergetpos},
+ {"setpos", bcharactersetpos},
+ {"onground", bcharacteronground},
+ {"jump", bcharacterjump},
+ {"setfallspeed", bcharactersetfallspeed},
+ {"getmaxslope", bcharactergetmaxslope},
+ {"setmaxslope", bcharactersetmaxslope},
+ {"setvelocityforinterval",bcharactersetvelocityforinterval},
{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}
+ luaL_newmetatable(L, "phys.charactercontroller");//{phys.characontroller}
+ lua_newtable(L);//{phys.charcontroller},{}
+ luaL_register(L,NULL,bcharactercontroller_m);//{phys.charcontroller},{}
+ lua_setfield(L,-2,"__index");//{phys.charcontroller}
lua_pop(L,1);//
lua_getglobal(L,"phys");//{}
lua_pushcfunction(L,newbcharactercontroller);//{},newbcharactercontroller()
- lua_setfield(L,-2,"newcharactercontroller");//{}
+ lua_setfield(L,-2,"newbcharactercontroller");//{}
lua_pop(L,1);
}