Composition scales addapted

Physics added trimeshshapes
This commit is contained in:
Jasper 2014-12-15 16:02:04 +01:00
parent fdeb783055
commit e49e4c2177
3 changed files with 146 additions and 18 deletions

View File

@ -5,7 +5,7 @@
<xOffset>0.0</xOffset>
<yOffset>0.0</yOffset>
<zOffset>0.0</zOffset>
<scale>0.75</scale>
<scale>1.0</scale>
<mass>8.0</mass>
</object>
</composition>
@ -17,8 +17,8 @@
<xOffset>0.0</xOffset>
<yOffset>1.0</yOffset>
<zOffset>2.0</zOffset>
<scale>1.0</scale>
<mass>2.0</mass>
<scale>1.5</scale>
<mass>0.0</mass>
</object>
</composition>
@ -35,7 +35,7 @@
</composition>
<composition>
<typeID>99</typeID>
<typeID>80</typeID>
<object>
<modelPath>torch.obj</modelPath>
<xOffset>0.0</xOffset>
@ -55,14 +55,15 @@
</light>
</composition>
<!-- Block on 2 Pillars -->
<composition>
<typeID>100</typeID>
<typeID>99</typeID>
<object>
<modelPath>column.obj</modelPath>
<xOffset>0.0</xOffset>
<yOffset>0.0</yOffset>
<zOffset>0.0</zOffset>
<scale>1.0</scale>
<scale>1.5</scale>
<mass>0.0</mass>
</object>
<object>
@ -70,7 +71,7 @@
<xOffset>2.0</xOffset>
<yOffset>0.0</yOffset>
<zOffset>0.0</zOffset>
<scale>1.0</scale>
<scale>1.5</scale>
<mass>0.0</mass>
</object>
<object>
@ -78,7 +79,7 @@
<xOffset>1.0</xOffset>
<yOffset>3.0</yOffset>
<zOffset>0.0</zOffset>
<scale>1.0</scale>
<scale>1.5</scale>
<mass>0.0</mass>
</object>
</composition>
@ -126,9 +127,9 @@
<specularFactor>0.4</specularFactor>
<shininess>2.0</shininess>
<physicType>Box</physicType>
<width>1.0</width>
<width>5.0</width>
<height>1.0</height>
<length>3.0</length>
<length>1.8</length>
</objectData>
<objectData>

View File

@ -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();

View File

@ -4,6 +4,8 @@
#include <ACGL/Base/Settings.hh>
#include <ACGL/Math/Math.hh>
#include <vector>
#include <string>
#include <stdio.h>
#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<btRigidBody*> 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
{