diff --git a/physics.cc b/physics.cc index ecb3326..5805d4a 100644 --- a/physics.cc +++ b/physics.cc @@ -105,9 +105,37 @@ void Physics::addPlayer(float rad, float x, float y, float z, float mass, unsign } -void Physics::addBox() +void Physics::addBox(float width, float height, float length, Entity entity, float mass, unsigned indice) { + if(bodies.size() != indice) + throw std::invalid_argument( "Bodies out of Sync" ); + + btBoxShape* box = new btBoxShape(btVector3(width/2,height/2,length/2)); + 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) + { + box->calculateLocalInertia((btScalar)mass,inertia); + } + + btRigidBody::btRigidBodyConstructionInfo info(mass,motion,box,inertia); + + btRigidBody* body = new btRigidBody(info); + + body->setDamping(0.8f,0.9f); + + 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" ); } void Physics::addSphere(float rad, float x, float y, float z, float mass, unsigned indice) @@ -128,6 +156,8 @@ void Physics::addSphere(float rad, float x, float y, float z, float mass, unsign //info. btRigidBody* body = new btRigidBody(info); + + body->setDamping(0.2f,0.4f); world->addRigidBody(body); diff --git a/physics.hh b/physics.hh index 10a0a8f..efb0c31 100644 --- a/physics.hh +++ b/physics.hh @@ -5,11 +5,14 @@ #include #include +#include "entity.hh" + #include "extern/bullet/src/BulletDynamics/Dynamics/btRigidBody.h" #include "extern/bullet/src/BulletDynamics/Dynamics/btDynamicsWorld.h" #include "extern/bullet/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h" #include "extern/bullet/src/BulletCollision/CollisionShapes/btSphereShape.h" +#include "extern/bullet/src/BulletCollision/CollisionShapes/btBoxShape.h" #include "extern/bullet/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h" #include "extern/bullet/src/BulletCollision/CollisionShapes/btStaticPlaneShape.h" @@ -47,7 +50,7 @@ class Physics { void addTerrain(int width, int length, float** heightData); void addPlayer(float rad, float x, float y, float z, float mass, unsigned indice); void addSphere(float rad, float x, float y, float z, float mass, unsigned indice); - void addBox(); + void addBox(float width, float height, float length, Entity entity, float mass, unsigned indice); private: btRigidBody* playerBody;