Do not update objects which have a mass of zero and can therefore by definition not change position or rotation.

This commit is contained in:
Faerbit 2015-03-16 16:05:36 +01:00
parent 07e036451d
commit 1dde41d5f4
2 changed files with 87 additions and 45 deletions

View File

@ -301,7 +301,9 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa
if (physicType.compare("Player") == 0){
float radius = queryFloat(objectData, "radius");
radius *= objectScale*compScale;
level->addPhysicsObject(object);
if (mass != 0.0f) {
level->addPhysicsObject(object);
}
level->getPhysics()->addPlayer(friction, radius, *object, mass, dampningL, dampningA, level->getPhysicsObjectsVectorSize());
}else if (physicType.compare("Box") == 0){
float width = queryFloat(objectData, "width");
@ -310,7 +312,9 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa
width *= objectScale*compScale;
height *= objectScale*compScale;
length *= objectScale*compScale;
level->addPhysicsObject(object);
if (mass != 0.0f) {
level->addPhysicsObject(object);
}
level->getPhysics()->addBox(width, height, length, *object, mass, dampningL, dampningA, level->getPhysicsObjectsVectorSize(), rotate);
}else if (physicType.compare("Button") == 0){
float width = queryFloat(objectData, "width");
@ -319,10 +323,14 @@ void Loader::load(std::string filePath, Level* level, std::string compositionsPa
width *= objectScale*compScale;
height *= objectScale*compScale;
length *= objectScale*compScale;
level->addPhysicsObject(object);
if (mass != 0.0f) {
level->addPhysicsObject(object);
}
level->getPhysics()->addButton(width, height, length, *object, mass, dampningL, dampningA, level->getPhysicsObjectsVectorSize(), rotate);
}else if (physicType.compare("TriangleMesh") == 0){
level->addPhysicsObject(object);
if (mass != 0.0f) {
level->addPhysicsObject(object);
}
level->getPhysics()->addTriangleMeshBody(*object, modelPath, mass, dampningL, dampningA, level->getPhysicsObjectsVectorSize(), objectScale*compScale, rotate);
}else if (physicType.compare("None") == 0){

View File

@ -133,8 +133,10 @@ void Physics::addPositionConstraint(int bodyIndice, float strength, glm::vec3 po
//players and objects
void Physics::addPlayer(float friction, float rad, Entity entity, float mass, float dampningL, float dampningA, unsigned indice)
{
if(bodies.size() == indice)
throw std::invalid_argument( "Bodies out of Sync" ); //these error are to ensure that level can always communicate with physics without having to worry about synching errors
if (mass != 0.0f) {
if(bodies.size() == indice)
throw std::invalid_argument( "Bodies out of Sync: Before adding Player" ); //these error are to ensure that level can always communicate with physics without having to worry about synching errors
}
btSphereShape* sphere = new btSphereShape(rad); //the first thing we need for a rigid body is the shape
btVector3 inertia(0,0,0);
@ -159,14 +161,18 @@ void Physics::addPlayer(float friction, float rad, Entity entity, float mass, fl
playerBall->setDamping(dampningL, dampningA); //here we can set the dampning (how much of the motion is lost)
world->addRigidBody(playerBall,COL_OBJECTS,COL_OBJECTS|COL_OBJECTS_NO_TERRAIN|COL_TERRAIN); //then we add the rigid body to the wiorld, allowing it to be simulated
bodies.push_back(playerBall); //next we add the rigid body to our own list (for cleanup and for synchronitaation with level)
if (mass != 0.0f) {
bodies.push_back(playerBall); //next we add the rigid body to our own list (for cleanup and for synchronitaation with level)
}
//note, while we can always access playerBall through its global name, we add it to this array for synchronization purposes
playerBall->setSleepingThresholds(0,0); //in a final step we make sure that the body never is removed from the active rigid bodies
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync" ); //one last check to make sure level and physics are in synch
if (mass != 0.0f) {
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync: After adding Player" ); //one last check to make sure level and physics are in synch
}
addCamera(); //now that the player exists add a camera for the player
}
@ -202,9 +208,10 @@ void Physics::addTerrain(int width, int length, float** heightData) //The terrai
void Physics::addConvexBody(Entity entity, std::string path, float mass, float dampningL, float dampningA, unsigned indice, float scaling, bool rotate)
{
if(bodies.size() == indice)
throw std::invalid_argument( "Bodies out of Sync" );
if (mass != 0.0f) {
if(bodies.size() == indice)
throw std::invalid_argument( "Bodies out of Sync: Before adding Convex Body" );
}
SharedGeometryData geometry = loadGeometryData("../" + geometryPath + path);
@ -251,8 +258,10 @@ void Physics::addConvexBody(Entity entity, std::string path, float mass, float d
btRigidBody* body = new btRigidBody(info);
body->setDamping(dampningL,dampningA);
bodies.push_back(body);
if (mass != 0.0f) {
bodies.push_back(body);
}
world->addRigidBody(body,COL_OBJECTS, objectsPhysicsCollision);
@ -260,15 +269,19 @@ void Physics::addConvexBody(Entity entity, std::string path, float mass, float d
{
body->setAngularFactor(btVector3(0,0,0));
}
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync" );
if (mass != 0.0f) {
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync: After adding Convex Body" );
}
}
void Physics::addTriangleMeshBody(Entity entity, std::string path, float mass, float dampningL, float dampningA,unsigned indice,float scaling, bool rotate)
{
if(bodies.size() == indice)
throw std::invalid_argument( "Bodies out of Sync" );
if (mass != 0.0f) {
if(bodies.size() == indice)
throw std::invalid_argument( "Bodies out of Sync: Before adding Triangle Mesh Body" );
}
SharedGeometryData geometry = loadGeometryData("../" + geometryPath + path);
@ -316,8 +329,10 @@ void Physics::addTriangleMeshBody(Entity entity, std::string path, float mass, f
btRigidBody* body = new btRigidBody(info);
body->setDamping(dampningL,dampningA);
bodies.push_back(body);
if (mass != 0.0f) {
bodies.push_back(body);
}
world->addRigidBody(body,COL_OBJECTS, objectsPhysicsCollision);
@ -325,16 +340,19 @@ void Physics::addTriangleMeshBody(Entity entity, std::string path, float mass, f
{
body->setAngularFactor(btVector3(0,0,0));
}
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync" );
if (mass != 0.0f) {
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync: After adding Triangle Mesh Body" );
}
}
void Physics::addButton(float width, float height, float length, Entity entity, float mass, float dampningL, float dampningA, unsigned indice,bool rotate)
{
if(bodies.size() == indice)
throw std::invalid_argument( "Bodies out of Sync" );
if (mass != 0.0f) {
if(bodies.size() == indice)
throw std::invalid_argument( "Bodies out of Sync: Before adding Button" );
}
btBoxShape* box = new btBoxShape(btVector3(width/2,height/2,length/2));
@ -358,22 +376,28 @@ void Physics::addButton(float width, float height, float length, Entity entity,
world->addRigidBody(body,COL_OBJECTS_NO_TERRAIN, specialPhysicsCollision); //the specialPhysicsCollision allows these objects to not collide with the terrain
bodies.push_back(body);
if (mass != 0.0f) {
bodies.push_back(body);
}
if(!rotate)
{
body->setAngularFactor(btVector3(0,0,0));
}
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync" );
if (mass != 0.0f) {
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync: After adding Button" );
}
}
void Physics::addBox(float width, float height, float length, Entity entity, float mass, float dampningL, float dampningA, unsigned indice,bool rotate)
{
//similar to other constructors
if(bodies.size() == indice)
throw std::invalid_argument( "Bodies out of Sync" );
if (mass != 0.0f) {
if(bodies.size() == indice)
throw std::invalid_argument( "Bodies out of Sync: Before adding Box" );
}
glm::quat glmQuat = glm::quat_cast(entity.getRotation());
btBoxShape* box = new btBoxShape(btVector3(width/2,height/2,length/2));
@ -393,22 +417,28 @@ void Physics::addBox(float width, float height, float length, Entity entity, flo
body->setDamping(dampningL, dampningA);
world->addRigidBody(body,COL_OBJECTS, objectsPhysicsCollision);
bodies.push_back(body);
if (mass != 0.0f) {
bodies.push_back(body);
}
if(!rotate)
{
body->setAngularFactor(btVector3(0,0,0));
}
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync" );
if (mass != 0.0f) {
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync: After adding Box" );
}
}
void Physics::addSphere(float rad, Entity entity, float mass, float dampningL, float dampningA, unsigned indice,bool rotate)
{
if(bodies.size() == indice) //(user's initial) height, not the actual height. More...
throw std::invalid_argument( "Bodies out of Sync" );
if (mass != 0.0f) {
if(bodies.size() == indice) //(user's initial) height, not the actual height. More...
throw std::invalid_argument( "Bodies out of Sync: Before adding Sphere" );
}
btSphereShape* sphere = new btSphereShape(rad);
btVector3 inertia(0,0,0);
@ -429,7 +459,9 @@ void Physics::addSphere(float rad, Entity entity, float mass, float dampningL, f
world->addRigidBody(body,COL_OBJECTS, objectsPhysicsCollision);
bodies.push_back(body);
if (mass != 0.0f) {
bodies.push_back(body);
}
if(!rotate)//rotate lets certain objects get inertia (0,0,0) (not rotateable)
{
@ -437,9 +469,11 @@ void Physics::addSphere(float rad, Entity entity, float mass, float dampningL, f
}
body->setSleepingThresholds(0,0);
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync" );
if (mass != 0.0f) {
if(bodies.size() != indice)
throw std::invalid_argument( "Bodies out of Sync: After adding Sphere" );
}
}
void Physics::prepareCollisionDetection()