diff --git a/Levels/ObjectSetups/Compositions.xml b/Levels/ObjectSetups/Compositions.xml index 1fa82eb..9a53ec6 100644 --- a/Levels/ObjectSetups/Compositions.xml +++ b/Levels/ObjectSetups/Compositions.xml @@ -5,7 +5,7 @@ 0.0 0.0 0.0 - 0.75 + 1.0 8.0 @@ -17,8 +17,8 @@ 0.0 1.0 2.0 - 1.0 - 2.0 + 1.5 + 0.0 @@ -35,7 +35,7 @@ - 99 + 80 torch.obj 0.0 @@ -55,14 +55,15 @@ + - 100 + 99 column.obj 0.0 0.0 0.0 - 1.0 + 1.5 0.0 @@ -70,7 +71,7 @@ 2.0 0.0 0.0 - 1.0 + 1.5 0.0 @@ -78,7 +79,7 @@ 1.0 3.0 0.0 - 1.0 + 1.5 0.0 @@ -126,9 +127,9 @@ 0.4 2.0 Box - 1.0 + 5.0 1.0 - 3.0 + 1.8 diff --git a/physics.cc b/physics.cc index 7fd76b2..dad6de8 100644 --- a/physics.cc +++ b/physics.cc @@ -1,4 +1,5 @@ #include "physics.hh" +#include "extern/bullet/Extras/Serialize/BulletWorldImporter/btBulletWorldImporter.h" Physics::Physics() { @@ -23,6 +24,44 @@ void Physics::takeUpdateStep(float timeDiff) world->stepSimulation(timeDiff); } +//TERRAIN SUBSET +void Physics::addTerrainTriangles(int width, int length, float** heightData) +{//not working correctly something with offset wrong? + btTriangleMesh* trimesh = new btTriangleMesh(); + for(int i = 0; i < width-1;i++) + { + for(int j = 0; j < length-1; j++) + { + btVector3 v0(i,heightData[j][i],j); + btVector3 v1(i+1,heightData[j][i+1],j); + btVector3 v2(i,heightData[j+1][i],j+1); + + trimesh->addTriangle(v0,v1,v2); + } + } + for(int i = 1; i < width;i++) + { + for(int j = 1; j < length; j++) + { + btVector3 v0(i,heightData[j][i],j); + btVector3 v1(i-1,heightData[j][i-1],j); + btVector3 v2(i,heightData[j-1][i],j-1); + + trimesh->addTriangle(v0,v1,v2); + } + } + + btBvhTriangleMeshShape* shape = new btBvhTriangleMeshShape(trimesh, true); + btRigidBody* tBody = new btRigidBody(0, new btDefaultMotionState(),shape); + + tBody->getWorldTransform().setOrigin(btVector3(-length/2,0,-width/2)); + + terrainBody = tBody; + + world->addRigidBody(tBody); + +} + void Physics::addTerrain(int width, int length, float** heightData) { @@ -50,7 +89,7 @@ void Physics::addTerrain(int width, int length, float** heightData) terrainBody = tBody; - world->addRigidBody(terrainBody); + world->addRigidBody(terrainBody, COL_TERRAIN, COL_TERRAIN | COL_OBJECTS); } void Physics::addStaticGroundPlane() @@ -63,6 +102,8 @@ void Physics::addStaticGroundPlane() world->addRigidBody(staticGroundBody); } + +//players and objects void Physics::addPlayer(float rad, Entity entity, float mass, unsigned indice) { if(bodies.size() == indice) @@ -79,6 +120,8 @@ void Physics::addPlayer(float rad, Entity entity, float mass, unsigned indice) btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia); + info.m_friction = 5; + playerBall = new btRigidBody(info); playerBall->setDamping(0.1f,0.3f); @@ -93,6 +136,77 @@ void Physics::addPlayer(float rad, Entity entity, float mass, unsigned indice) } +void Physics::addTriangleMeshBody(Entity entity, std::string path, float mass, float dampningL, float dampningA,unsigned indice) +{//TODO look at convexHullShapes + + if(bodies.size() == indice) + throw std::invalid_argument( "Bodies out of Sync" ); + + std::vector< unsigned int > vertexIndices; + std::vector< btVector3 > temp_vertices; + + path = "../Levels/Geometry/" + path; + FILE * file = fopen(path.c_str(), "r"); + if( file == NULL ){ + printf("Impossible to open the file !\n"); + } + + while( 1 ){ + + char lineHeader[128]; + // read the first word of the line + int res = fscanf(file, "%s", lineHeader); + if (res == EOF) + break; // EOF = End Of File. Quit the loop. + // else : parse lineHeader + if ( strcmp( lineHeader, "v" ) == 0 ){ + glm::vec3 vertex; + fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z ); + temp_vertices.push_back(btVector3(vertex.x,vertex.y,vertex.z)); + } + else if ( strcmp( lineHeader, "f" ) == 0 ){ + std::string vertex1, vertex2, vertex3; + unsigned int vertexIndex[3], uvIndex[3], normalIndex[3]; + int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2] ); + vertexIndices.push_back(vertexIndex[0]); + vertexIndices.push_back(vertexIndex[1]); + vertexIndices.push_back(vertexIndex[2]); + } + } + printf("olla"); + //finally start making body + btTriangleMesh* triMesh = new btTriangleMesh(); + + for(unsigned i = 2; i < vertexIndices.size();i++) + { + triMesh->addTriangle(temp_vertices[vertexIndices[i]],temp_vertices[vertexIndices[i-1]],temp_vertices[vertexIndices[i-2]]); + } + + btBvhTriangleMeshShape* shape = new btBvhTriangleMeshShape(triMesh,true); + + btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(entity.getPosition().x,entity.getPosition().y,entity.getPosition().z))); + + btVector3 inertia(0,0,0); + if(mass != 0.0) + { + shape->calculateLocalInertia((btScalar)mass,inertia); + } + + btRigidBody::btRigidBodyConstructionInfo info(mass,motion,shape,inertia); + + btRigidBody* body = new btRigidBody(info); + + body->setDamping(dampningL,dampningA); + + bodies.push_back(body); + + world->addRigidBody(body); + + + if(bodies.size() != indice) + throw std::invalid_argument( "Bodies out of Sync" ); +} + void Physics::addBox(float width, float height, float length, Entity entity, float mass, unsigned indice) { @@ -138,7 +252,7 @@ void Physics::addSphere(float rad, Entity entity, float mass, unsigned indice) btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(entity.getPosition().x,entity.getPosition().y,entity.getPosition().z))); btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia); - //info. + btRigidBody* body = new btRigidBody(info); @@ -154,7 +268,7 @@ void Physics::addSphere(float rad, Entity entity, float mass, unsigned indice) throw std::invalid_argument( "Bodies out of Sync" ); } - +/* void Physics::addTriangleMeshBody(Entity entity, float mass, float dampningL, float dampningA,unsigned indice) { btTriangleMesh* trimesh = new btTriangleMesh(); @@ -180,7 +294,7 @@ void Physics::addTriangleMeshBody(Entity entity, float mass, float dampningL, fl body->setDamping(dampningL,dampningA); -} +}*/ void Physics::addCamera(float rad, float distance) { @@ -206,6 +320,8 @@ void Physics::addCamera(float rad, float distance) world->addConstraint(pdc); } + +//update functions glm::vec3 Physics::getCameraPosition() { btVector3 origin = cameraBody->getCenterOfMassPosition(); diff --git a/physics.hh b/physics.hh index 9eff59c..9d96467 100644 --- a/physics.hh +++ b/physics.hh @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "entity.hh" @@ -17,6 +19,8 @@ #include "extern/bullet/src/BulletCollision/CollisionShapes/btStaticPlaneShape.h" #include "extern/bullet/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h" #include "extern/bullet/src/BulletCollision/CollisionShapes/btTriangleMesh.h" +#include "extern/bullet/src/BulletCollision/CollisionShapes/btCollisionShape.h" +#include "extern/bullet/src/BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h" #include "extern/bullet/src/BulletDynamics/ConstraintSolver/btConstraintSolver.h" #include "extern/bullet/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"//YAY! @@ -51,20 +55,21 @@ class Physics { void addStaticGroundPlane(); void addCamera(float rad,float distance); //Do NOT impliment before Player has been created; glm::vec3 getCameraPosition(); - void addTriangleMeshBody(Entity entity, float mass, float dampningL, float dampningA,unsigned indice); + void addRigidBodyFromFile(Entity entity, float mass, float dampningL, float dampningA, std::string modelLocation,unsigned indice); + void addTriangleMeshBody(Entity entity, std::string path, float mass, float dampningL, float dampningA,unsigned indice); void addTerrain(int width, int length, float** heightData); + void addTerrainTriangles(int width, int length, float** heightData); //add the terrain as a trimesh instead of a heightmap void addPlayer(float rad, Entity entity, float mass, unsigned indice); //use these AFTER physicObjects.push_back(object)! if mass == 0 then the object is unmoveable void addSphere(float rad, Entity entity, float mass, unsigned indice); //The Indice should be set to physicObjects.size() void addBox(float width, float height, float length, Entity entity, float mass, unsigned indice); //this is used to ensuer that the system is synchronized private: - btRigidBody* playerBall; //allows for quicker access to the ball + btRigidBody* playerBall; //allows for easier access to the ball btRigidBody* terrainBody; //duh btRigidBody* cameraBody; std::vector bodies; //list of all bodies. bodies are also in world, but save again to ease cleaning up process. btRigidBody* staticGroundBody; - - + btDynamicsWorld* world; //contains physical attributes of the world. btDispatcher* dispatcher; // btCollisionConfiguration* colConfig; //defines the type of collision detection. @@ -72,6 +77,12 @@ class Physics { btConstraintSolver* solver; //solver for forces and impulses. }; +enum collisionTypes{ + COL_NOTHING = 0, + COL_TERRAIN = 1, + COL_OBJECTS = 2, + COL_OBJECTS_NO_TERRAIN = 4 +}; class btDistanceConstraint : public btPoint2PointConstraint {