Added position Constraint structure, an add/remove function for them, and added their handeling to the update function

This commit is contained in:
Jasper 2015-01-16 16:17:26 +01:00
parent 3cba239897
commit 913e862a88
2 changed files with 50 additions and 13 deletions

View File

@ -30,6 +30,35 @@ void Physics::init(lua_State* L)
void Physics::takeUpdateStep(float timeDiff) void Physics::takeUpdateStep(float timeDiff)
{ {
world->stepSimulation(timeDiff); world->stepSimulation(timeDiff);
for(unsigned i = 0; i < allPositionConstraints.size();i++)
{
if(allPositionConstraints[i].position != allPositionConstraints[i].body->getCenterOfMassPosition())
{
btVector3 dir = allPositionConstraints[i].body->getCenterOfMassPosition() - allPositionConstraints[i].position;
allPositionConstraints[i].body->applyCentralForce(dir*allPositionConstraints[i].strength);
}
}
}
void Physics::removePositionConstraint(int bodyIndice)
{
for(unsigned i = 0; i < allPositionConstraints.size();i++)
{
if(allPositionConstraints[i].body == bodies[i] )
{
allPositionConstraints.erase(allPositionConstraints.begin()+i);
}
}
}
void Physics::addPositionConstraint(int bodyIndice, float strength, glm::vec3 position)
{
positionConstraint cons;
cons.body = bodies[bodyIndice];
cons.strength = strength;
cons.position = btVector3(position.x,position.y,position.z);
allPositionConstraints.push_back(cons);
} }
//TERRAIN SUBSET //TERRAIN SUBSET
@ -45,6 +74,7 @@ void Physics::addTerrainTriangles(int width, int length, float** heightData)
btVector3 v2(i,heightData[j+1][i],j+1); btVector3 v2(i,heightData[j+1][i],j+1);
trimesh->addTriangle(v0,v1,v2); trimesh->addTriangle(v0,v1,v2);
} }
} }
for(int i = 1; i < width;i++) for(int i = 1; i < width;i++)
@ -100,17 +130,6 @@ void Physics::addTerrain(int width, int length, float** heightData)
world->addRigidBody(terrainBody, COL_TERRAIN, COL_TERRAIN | COL_OBJECTS); world->addRigidBody(terrainBody, COL_TERRAIN, COL_TERRAIN | COL_OBJECTS);
} }
void Physics::addStaticGroundPlane()
{
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 0);
btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0));
staticGroundBody = new btRigidBody(groundRigidBodyCI);
world->addRigidBody(staticGroundBody);
}
//players and objects //players and objects
void Physics::addPlayer(float friction, float rad, Entity entity, float mass, float dampningL, float dampningA, unsigned indice) void Physics::addPlayer(float friction, float rad, Entity entity, float mass, float dampningL, float dampningA, unsigned indice)
{ {
@ -129,6 +148,7 @@ void Physics::addPlayer(float friction, float rad, Entity entity, float mass, fl
btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia); btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia);
info.m_friction = friction; info.m_friction = friction;
info.m_restitution = 0.0f;
playerBall = new btRigidBody(info); playerBall = new btRigidBody(info);
@ -389,6 +409,18 @@ void Physics::rollRight(glm::vec3 camPos,float strength)
playerBall->applyTorque(-pos); playerBall->applyTorque(-pos);
} }
//not used right now
void Physics::addStaticGroundPlane()
{
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 0);
btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0));
staticGroundBody = new btRigidBody(groundRigidBodyCI);
world->addRigidBody(staticGroundBody);
}
/* /*
void kill() void kill()
{ {

View File

@ -69,6 +69,10 @@ class Physics {
void addPlayer(float friction, float rad, Entity entity, float mass, float dampningL, float dampningA, unsigned indice); //use these AFTER physicObjects.push_back(object)! if mass == 0 then the object is unmoveable void addPlayer(float friction, float rad, Entity entity, float mass, float dampningL, float dampningA, 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, float dampningL, float dampningA, unsigned indice); //The Indice should be set to physicObjects.size() void addSphere(float rad, Entity entity, float mass, float dampningL, float dampningA, unsigned indice); //The Indice should be set to physicObjects.size()
void addBox(float width, float height, float length, Entity entity, float mass, float dampningL, float dampningA, unsigned indice); //this is used to ensuer that the system is synchronized void addBox(float width, float height, float length, Entity entity, float mass, float dampningL, float dampningA, unsigned indice); //this is used to ensuer that the system is synchronized
void addPositionConstraint(int bodyIndice, float strength, glm::vec3 position);
void removePositionConstraint(int bodyIndice);
struct positionConstraint{btRigidBody* body; float strength; btVector3 position;};
private: private:
btRigidBody* playerBall; //allows for easier access to the ball btRigidBody* playerBall; //allows for easier access to the ball
@ -76,6 +80,7 @@ class Physics {
btRigidBody* cameraBody; btRigidBody* cameraBody;
std::vector<btRigidBody*> bodies; //list of all bodies. bodies are also in world, but save again to ease cleaning up process. std::vector<btRigidBody*> bodies; //list of all bodies. bodies are also in world, but save again to ease cleaning up process.
btRigidBody* staticGroundBody; btRigidBody* staticGroundBody;
std::vector<positionConstraint> allPositionConstraints;
btDynamicsWorld* world; //contains physical attributes of the world. btDynamicsWorld* world; //contains physical attributes of the world.
btDispatcher* dispatcher; // btDispatcher* dispatcher; //