From ca98df63ac47606fc23121ff5bd45878a7c8876d Mon Sep 17 00:00:00 2001 From: Jasper Date: Mon, 19 Jan 2015 14:01:05 +0100 Subject: [PATCH] added collision masks and button creator --- physics.cc | 40 ++++++++++++++++++++++++++++++++++------ physics.hh | 7 +++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/physics.cc b/physics.cc index 793b621..4d25f43 100644 --- a/physics.cc +++ b/physics.cc @@ -87,7 +87,7 @@ void Physics::addTerrainTriangles(int width, int length, float** heightData) terrainBody = tBody; - world->addRigidBody(tBody); + world->addRigidBody(tBody,COL_TERRAIN,COL_OBJECTS); } @@ -145,7 +145,7 @@ void Physics::addPlayer(float friction, float rad, Entity entity, float mass, fl playerBall->setDamping(dampningL, dampningA); - world->addRigidBody(playerBall); + world->addRigidBody(playerBall,COL_OBJECTS,COL_OBJECTS|COL_OBJECTS_NO_TERRAIN|COL_TERRAIN); bodies.push_back(playerBall); @@ -202,7 +202,7 @@ void Physics::addTriangleMeshBody(Entity entity, std::string path, float mass, f } btBvhTriangleMeshShape* shape = new btBvhTriangleMeshShape(triMesh,true); - + shape->setLocalScaling(btVector3(0.5f,0.5f,0.5f)); 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); @@ -226,6 +226,34 @@ void Physics::addTriangleMeshBody(Entity entity, std::string path, float mass, f throw std::invalid_argument( "Bodies out of Sync" ); } +void Physics::addButton(float radius, float height, Entity entity, float mass, float dampningL, float dampningA, unsigned indice) +{ + + if(bodies.size() == indice) + throw std::invalid_argument( "Bodies out of Sync" ); + btCylinderShape* shape = new btCylinderShape(btVector3(height/2, radius,radius)); + 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); + + world->addRigidBody(body,COL_OBJECTS_NO_TERRAIN, specialPhysicsCollision); + + bodies.push_back(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, float dampningL, float dampningA, unsigned indice) { @@ -248,8 +276,8 @@ void Physics::addBox(float width, float height, float length, Entity entity, flo body->setDamping(dampningL, dampningA); - world->addRigidBody(body); - + world->addRigidBody(body,COL_OBJECTS, objectsPhysicsCollision); + bodies.push_back(body); if(bodies.size() != indice) @@ -277,7 +305,7 @@ void Physics::addSphere(float rad, Entity entity, float mass, float dampningL, f body->setDamping(dampningL, dampningA); - world->addRigidBody(body); + world->addRigidBody(body,COL_OBJECTS, objectsPhysicsCollision); bodies.push_back(body); diff --git a/physics.hh b/physics.hh index ec94bdd..51574eb 100644 --- a/physics.hh +++ b/physics.hh @@ -21,6 +21,7 @@ #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/BulletCollision/CollisionShapes/btCylinderShape.h" #include "extern/bullet/src/BulletDynamics/ConstraintSolver/btConstraintSolver.h" #include "extern/bullet/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"//YAY! @@ -64,6 +65,7 @@ class Physics { 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); + void addButton(float radius, float height, Entity entity, float mass, float dampningL, float dampningA, unsigned indice); struct positionConstraint{btRigidBody* body; float strength; btVector3 position;}; @@ -80,6 +82,10 @@ class Physics { btCollisionConfiguration* colConfig; //defines the type of collision detection. btBroadphaseInterface* broadphase; //defines how objects are culled from collision detection. btConstraintSolver* solver; //solver for forces and impulses. + int objectsPhysicsCollision = 1 | 2 | 4; + int specialPhysicsCollision = 2 | 4; + int terrainPhysicsCollision = 2; + }; enum collisionTypes{ @@ -89,6 +95,7 @@ enum collisionTypes{ COL_OBJECTS_NO_TERRAIN = 4 }; +//world->addRigidBody(playerBall,COL_OBJECTS_NO_TERRAIN, COL_OBJECTS); class btDistanceConstraint : public btPoint2PointConstraint { protected: