From 17a002f712d80e13ab4a9be414a4096494e768f0 Mon Sep 17 00:00:00 2001 From: nkorslund Date: Tue, 7 Oct 2008 08:51:17 +0000 Subject: [PATCH] Added bounding box collision generation (not done) git-svn-id: https://openmw.svn.sourceforge.net/svnroot/openmw/trunk@50 ea6a568a-9f4f-0410-981a-c910a81bb256 --- bullet/cpp_bullet.cpp | 125 +++++++++++++++++++++++++++--------------- bullet/cpp_player.cpp | 7 ++- 2 files changed, 87 insertions(+), 45 deletions(-) diff --git a/bullet/cpp_bullet.cpp b/bullet/cpp_bullet.cpp index af4b66d76..06f6b048e 100644 --- a/bullet/cpp_bullet.cpp +++ b/bullet/cpp_bullet.cpp @@ -242,13 +242,78 @@ extern "C" void bullet_getPlayerPos(float *x, float *y, float *z) *z = g_playerPosition.getZ(); } -unsigned char* copyBuffer(void *buf, int elemSize, int len) +void* copyBuffer(void *buf, int elemSize, int len) { int size = elemSize * len; void *res = malloc(size); memcpy(res, buf, size); - return (unsigned char*)res; + return res; +} + +// Define a cube with coordinates 0,0,0 - 1,1,1. +const float cube_verts[] = + { + 0,0,0, 1,0,0, 0,1,0, + 1,1,0, 0,0,1, 1,0,1, + 0,1,1, 1,1,1 + }; + +// Triangles of the cube. The orientation of each triange doesn't +// matter. +const short cube_tris[] = + { + // bottom side + 0, 1, 2, + 1, 2, 3, + // top side + 4, 5, 6, + 5, 6, 7, + // front side + 0, 4, 5, + 0, 1, 5, + // back side + 2, 3, 7, + 2, 6, 7, + // left side + 0, 2, 4, + 2, 4, 6, + // right side + 1, 3, 5, + 3, 5, 7 + }; + +const int cube_num_verts = 8; +const int cube_num_tris = 12; + +// Create a (trimesh) box with the given dimensions. Used for bounding +// boxes. TODO: I guess we have to use the NIF-specified bounding box +// for this, not our automatically calculated one. +extern "C" void bullet_createBox(float xmin, float ymin, float zmin, + float xmax, float ymax, float zmax, + float *trans, float *matrix) +{ + // Make a copy of the vertex buffer, since we need to change it + float *vbuffer = (float*)copyBuffer(cube_verts, 12, cube_num_verts); + + // Calculate the widths + float xwidth = xmax-xmin; + float ywidth = ymax-ymin; + float zwidth = zmax-zmin; + + // Transform the cube to (xmin,xmax) etc + float *vb = vbuffer; + for(int i=0; iaddCollisionObject(obj); } - -/* -// Insert a debug collision shape -extern "C" void bullet_insertBox(float x, float y, float z) -{ - btCollisionShape* groundShape = - new btSphereShape(50); - //new btBoxShape(btVector3(100,100,100)); - - // Create a motion state to hold the object (not sure why yet.) - btTransform groundTransform; - groundTransform.setIdentity(); - groundTransform.setOrigin(btVector3(x,y,z)); - - // Use a simple collision object for static objects - btCollisionObject *obj = new btCollisionObject(); - obj->setCollisionShape(groundShape); - obj->setWorldTransform(groundTransform); - - g_dynamicsWorld->addCollisionObject(obj); - - // You can also use a rigid body with a motion state, but this is - // overkill for statics. - /* - btDefaultMotionState* myMotionState = - new btDefaultMotionState(groundTransform); - - // Create a rigid body from the motion state. Give it zero mass and - // zero inertia. - btRigidBody::btRigidBodyConstructionInfo - rbInfo(0, myMotionState, groundShape, btVector3(0,0,0)); - btRigidBody* body = new btRigidBody(rbInfo); - - // Add the body to the world - g_dynamicsWorld->addRigidBody(body); - */ -//} - // Move the physics simulation 'delta' seconds forward in time extern "C" void bullet_timeStep(float delta) { diff --git a/bullet/cpp_player.cpp b/bullet/cpp_player.cpp index c1982bfc8..df94d1b49 100644 --- a/bullet/cpp_player.cpp +++ b/bullet/cpp_player.cpp @@ -49,7 +49,12 @@ btVector3 perpComponent (const btVector3& direction, const btVector3& normal) btManifoldArray manifoldArray; // Callback used for collision detection sweep tests. It prevents self -// collision and is used in calls to convexSweepTest(). +// collision and is used in calls to convexSweepTest(). TODO: It might +// be enough to just set the filters on this. If we set the group and +// mask so that we only collide with static objects, self collision +// would never happen. The sweep test function should have had a +// version where you only specify the filters - I might add that +// myself. class ClosestNotMeConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback { public: