From 3eb406efd11f65284b8dd0ec6a86bf5a510820f2 Mon Sep 17 00:00:00 2001 From: Jasper Date: Fri, 14 Nov 2014 16:23:14 +0100 Subject: [PATCH] Added addPlayer analog to addSphere and added a check to make sure incice stay synchronized (possibly dangerous but needs to be tested) --- physics.cc | 39 ++++++++++++++++++++++++++++++++++++--- physics.hh | 3 ++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/physics.cc b/physics.cc index 774d9a9..58d7d0f 100644 --- a/physics.cc +++ b/physics.cc @@ -70,14 +70,42 @@ void addTerrain(int width, int length, float** heightData) } -void addSphere(float rad, float x, float y, float z, float mass, int indice) //TODO add indice check +void addPlayer(float rad, float x, float y, float z, float mass, unsigned indice) { + if(bodies.size() != indice) + throw std::invalid_argument( "Bodies out of Sync" ); + btSphereShape* sphere = new btSphereShape(rad); btVector3 inertia(0,0,0); - if(mass == 0.0) + if(mass != 0.0) { + sphere->calculateLocalInertia((btScalar)mass,inertia); } - else + + btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(x,y,z))); + + btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia); + + playerBall = new btRigidBody(info); + + world->addRigidBody(playerBall); + + bodies.push_back(playerBall); + + if(bodies.size() == indice) + throw std::invalid_argument( "Bodies out of Sync" ); + +} + + +void addSphere(float rad, float x, float y, float z, float mass, unsigned indice) +{ + if(bodies.size() != indice) + throw std::invalid_argument( "Bodies out of Sync" ); + + btSphereShape* sphere = new btSphereShape(rad); + btVector3 inertia(0,0,0); + if(mass != 0.0) { sphere->calculateLocalInertia((btScalar)mass,inertia); } @@ -91,6 +119,11 @@ void addSphere(float rad, float x, float y, float z, float mass, int indice) //T world->addRigidBody(body); bodies.push_back(body); + + + if(bodies.size() == indice) + throw std::invalid_argument( "Bodies out of Sync" ); + } glm::vec3 getPos(int i) diff --git a/physics.hh b/physics.hh index 90136ad..afb3b5c 100644 --- a/physics.hh +++ b/physics.hh @@ -40,7 +40,8 @@ class Physics { glm::mat4 getRotation(int i); void rollForward(glm::mat3 rotCamera); void addTerrain(int width, int length, float** heightData); - void addSphere(float rad, float x, float y, float z, float mass, int indice); + 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); private: btRigidBody* playerBody;