diff --git a/physics.cc b/physics.cc index 03d353c..7fd76b2 100644 --- a/physics.cc +++ b/physics.cc @@ -1,5 +1,6 @@ #include "physics.hh" + Physics::Physics() { } @@ -117,11 +118,7 @@ void Physics::addBox(float width, float height, float length, Entity entity, flo world->addRigidBody(body); bodies.push_back(body); - - if(mass != 0) - body->setSleepingThresholds(0,0); - - + if(bodies.size() != indice) throw std::invalid_argument( "Bodies out of Sync" ); } @@ -158,6 +155,64 @@ void Physics::addSphere(float rad, Entity entity, float mass, unsigned indice) } +void Physics::addTriangleMeshBody(Entity entity, float mass, float dampningL, float dampningA,unsigned indice) +{ + btTriangleMesh* trimesh = new btTriangleMesh(); + + + btVector3 v0( 0, 0, 0 ); + btVector3 v1( 1, 1, 1 ); + btVector3 v2( 2, 2, 2); + + trimesh->addTriangle( v0, v1, v2 ); + + btTriangleMeshShape* shape = new btBvhTriangleMeshShape(trimesh,true); + btVector3 inertia(0,0,0); + if(mass != 0.0) + { + shape->calculateLocalInertia((btScalar)mass,inertia); + } + + 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,shape,inertia); + btRigidBody* body = new btRigidBody(info); + + body->setDamping(dampningL,dampningA); + +} + +void Physics::addCamera(float rad, float distance) +{ + btSphereShape* sphere = new btSphereShape(rad); + + btVector3 inertia(0,0,0); + btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,0,0))); + + btRigidBody::btRigidBodyConstructionInfo info(1/(playerBall->getInvMass()/100),motion,sphere,inertia); + + cameraBody = new btRigidBody(info); + + cameraBody->setDamping(0.9f,1.0f); + + world->addRigidBody(cameraBody); + + cameraBody->setSleepingThresholds(0,0); + + + btVector3 pivotInA(5,0,0); + btVector3 pivotInB(-5, 0, 0); + btDistanceConstraint* pdc = new btDistanceConstraint(*cameraBody,*playerBall,pivotInA,pivotInB, distance); + world->addConstraint(pdc); +} + +glm::vec3 Physics::getCameraPosition() +{ + btVector3 origin = cameraBody->getCenterOfMassPosition(); + glm::vec3 save(origin.getX(),origin.getY(),origin.getZ()); + return save; +} + glm::vec3 Physics::getPos(int i) { btVector3 origin = bodies[i]->getCenterOfMassPosition(); diff --git a/physics.hh b/physics.hh index a9cca30..9eff59c 100644 --- a/physics.hh +++ b/physics.hh @@ -15,9 +15,12 @@ #include "extern/bullet/src/BulletCollision/CollisionShapes/btBoxShape.h" #include "extern/bullet/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h" #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/BulletDynamics/ConstraintSolver/btConstraintSolver.h" #include "extern/bullet/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"//YAY! +#include "extern/bullet/src/BulletDynamics/ConstraintSolver/btPoint2PointConstraint.h" #include "extern/bullet/src/BulletCollision/CollisionDispatch/btCollisionConfiguration.h" #include "extern/bullet/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h" @@ -46,6 +49,9 @@ class Physics { glm::vec3 getPos(int i); glm::mat4 getRotation(int i); 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 addTerrain(int width, int length, float** heightData); 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() @@ -54,6 +60,7 @@ class Physics { private: btRigidBody* playerBall; //allows for quicker 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; @@ -64,4 +71,49 @@ class Physics { btBroadphaseInterface* broadphase; //defines how objects are culled from collision detection. btConstraintSolver* solver; //solver for forces and impulses. }; + + +class btDistanceConstraint : public btPoint2PointConstraint +{ +protected: + btScalar m_distance; +public: + btDistanceConstraint(btRigidBody& rbA,btRigidBody& rbB, const btVector3& pivotInA,const btVector3& pivotInB, btScalar dist) + : btPoint2PointConstraint(rbA, rbB, pivotInA, pivotInB) + { + m_distance = dist; + } + virtual void getInfo1 (btConstraintInfo1* info) + { + info->m_numConstraintRows = 1; + info->nub = 5; + } + virtual void getInfo2 (btConstraintInfo2* info) + { + btVector3 relA = m_rbA.getCenterOfMassTransform().getBasis() * getPivotInA(); + btVector3 relB = m_rbB.getCenterOfMassTransform().getBasis() * getPivotInB(); + btVector3 posA = m_rbA.getCenterOfMassTransform().getOrigin() + relA; + btVector3 posB = m_rbB.getCenterOfMassTransform().getOrigin() + relB; + btVector3 del = posB - posA; + btScalar currDist = btSqrt(del.dot(del)); + btVector3 ortho = del / currDist; + info->m_J1linearAxis[0] = ortho[0]; + info->m_J1linearAxis[1] = ortho[1]; + info->m_J1linearAxis[2] = ortho[2]; + btVector3 p, q; + p = relA.cross(ortho); + q = relB.cross(ortho); + info->m_J1angularAxis[0] = p[0]; + info->m_J1angularAxis[1] = p[1]; + info->m_J1angularAxis[2] = p[2]; + info->m_J2angularAxis[0] = -q[0]; + info->m_J2angularAxis[1] = -q[1]; + info->m_J2angularAxis[2] = -q[2]; + btScalar rhs = (currDist - m_distance) * info->fps * info->erp; + info->m_constraintError[0] = rhs; + info->cfm[0] = btScalar(0.f); + info->m_lowerLimit[0] = -SIMD_INFINITY; + info->m_upperLimit[0] = SIMD_INFINITY; + } +}; #endif