aboutsummaryrefslogtreecommitdiff
path: root/src/shared/lua_api/phys/bcharactercontroller.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/lua_api/phys/bcharactercontroller.cpp')
-rw-r--r--src/shared/lua_api/phys/bcharactercontroller.cpp145
1 files changed, 59 insertions, 86 deletions
diff --git a/src/shared/lua_api/phys/bcharactercontroller.cpp b/src/shared/lua_api/phys/bcharactercontroller.cpp
index 8030846..0f3096c 100644
--- a/src/shared/lua_api/phys/bcharactercontroller.cpp
+++ b/src/shared/lua_api/phys/bcharactercontroller.cpp
@@ -9,11 +9,14 @@ extern "C" {
#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");
@@ -27,14 +30,16 @@ static LISceneNode* checkismesh(lua_State* L){
return checkismesh(L,1);
}
*/
-// ud_btRigidBody :: ({v3 size}, {v3 origin}, double mass)
-void makenewbphysbox(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);
double px,py,pz; //position
double sx,sy,sz; //size
- double mass;
+ //double mass;
- mass = lua_tonumber(L,-1);//{v3_size},{v3_origin},mass
- lua_pop(L,1);//{v3_size},{v3_origin}
+ //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}
@@ -44,132 +49,100 @@ void makenewbphysbox(lua_State* L){
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);
-
- // Set the initial position of the object
btTransform transform = btTransform(btQuaternion(0,0,0,1),pos);
- //transform.setIdentity();
- //transform.setOrigin(pos);
-
- // Give it a default MotionState
- btDefaultMotionState* motionstate = new btDefaultMotionState(transform);
- if(!motionstate){
- //printf("No motionstate\n");
- }
+
// Create the shape
- btCollisionShape* shape = new btBoxShape(vshape);
- if(!shape){
- //printf("no shape\n");
- }
+ btConvexShape* cshape = new btBoxShape(vshape);
+
+
// Add mass
- btVector3 localinertia = btVector3(0,0,0);
- shape->calculateLocalInertia(mass, localinertia);
+ //btVector3 localinertia = btVector3(0,0,0);
+ //shape->calculateLocalInertia(mass, localinertia);
// Create the rigid body object
- btRigidBody::btRigidBodyConstructionInfo cinfo = btRigidBody::btRigidBodyConstructionInfo(
- mass,
- motionstate,
- shape,
- localinertia
- );
- btKinematicCharacterController cc = btKinematicCharacterController(btPairCacheingGhostObject(), shape, 0.8, btVector3(0,1,0));
+ //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);
- if(!rigidbody){
- //printf("No rigidbody\n");
- }
// Add it to the world
- World->addVehicle(cc);
+ World->addAction(cc);
+ //World->addVehicle(cc);
//printf("Added rigid body to world: %p\n",World);
- Objects.push_back(cc);
+ Chars.push_back(cc);
- lua_pushlightuserdata(L,rigidbody);//ud_rigidbody
+ lua_pushlightuserdata(L,cc);//ud_cc
}
// phys.newphysbox(vector3 size, vector3 origin, double mass)
-int newbphysbox(lua_State* L){
+int newbcharactercontroller(lua_State* L){
//printf("Createing bphysbox!\n");
//Create it's lua representation
- makenewbphysbox(L);//ud_btRigidBody
- btRigidBody* r = (btRigidBody*)lua_touserdata(L,-1);
+ makenewbcharactercontroller(L);//ud_cc
+ btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1);
lua_pop(L,1);
lua_newtable(L);//{}
- lua_pushlightuserdata(L,r);//ud_btRigidBody
- lua_setfield(L,-2,"rigidbody");//{}
+ lua_pushlightuserdata(L,r);//ud_cc
+ lua_setfield(L,-2,"character");//{}
//Set it's metatable
- luaL_getmetatable(L, "phys.physbox");//{},{phys.physbox}
+ luaL_getmetatable(L, "phys.charactercontroller");//{},{phys.charactercontroller}
lua_setmetatable(L, -2);//{}
return 1;
}
//{phys.physbox}:delete()
-static int delbphysbox(lua_State* L){//self
+static int delbcharactercontroller(lua_State* L){//self
//printf("Attempting to delete physbox\n");
- lua_getfield(L,-1,"rigidbody");//self,ud_rigidbody
- btRigidBody* r = (btRigidBody*)lua_touserdata(L,-1);//self,ud_rigidbody
- delete r->getCollisionShape();
- delete r->getMotionState();
+ 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;
}
-// physbox:setpos({v3 pos})
-static int bphyssetpos(lua_State *L){//self,{v3 pos}
- double nx,ny,nz;
- popvector3d(L,&nx,&ny,&nz);//self
-
- lua_getfield(L,-1,"rigidbody");//self,ud_rigidbody
- btRigidBody* i = (btRigidBody*)lua_touserdata(L,-1);//self
- btMotionState* ms = i->getMotionState();
- btTransform bt;
- ms->getWorldTransform(bt);
-
- btVector3 to = btVector3(nx,ny,nz);
- bt.setOrigin(to);
- ms->setWorldTransform(bt);
- i->activate();
-
- lua_pop(L,1);//
+//{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;
}
-// {v3 pos} :: physbox:getpos()
-static int bphysgetpos(lua_State *L){//self
- //printf("Physics box set pos called\n");
- lua_getfield(L,-1,"rigidbody");//self,ud_rigidbody
- btRigidBody* i = (btRigidBody*)lua_touserdata(L,-1);//self,ud_rigidbody
- btTransform bt = i->getWorldTransform();
- btVector3 bv = bt.getOrigin();
- lua_pop(L,2);//
- pushvector3d(L,bv.x(),bv.y(),bv.z());//{}
-
- return 1;
-}
-
-static const luaL_reg bphysbox_m[] = {
- {"getpos", bphysgetpos},
- {"setpos", bphyssetpos},
- {"delete", delbphysbox},
- {0, 0},
+static const luaL_reg bcharactercontroller_m[] = {
+ {"setwalkdir", bcharsetwalkdirection},
+ {"remove", delbcharactercontroller},
+ {0, 0},
};
-void bphysbox_register(lua_State* L){//
+void bcharactercontroller_register(lua_State* L){//
//printf("Registered bphysbox\n");
- luaL_newmetatable(L, "phys.physbox");//{phys.physbox}
+ luaL_newmetatable(L, "phys.charactercontroller");//{phys.physbox}
lua_newtable(L);//{phys.physbox},{}
- luaL_register(L,NULL,bphysbox_m);//{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,newbphysbox);//{},newbphysbox()
- lua_setfield(L,-2,"newphysbox");//{}
+ lua_pushcfunction(L,newbcharactercontroller);//{},newbcharactercontroller()
+ lua_setfield(L,-2,"newcharactercontroller");//{}
lua_pop(L,1);
}