added box in physics

This commit is contained in:
Jasper 2014-11-21 12:50:11 +01:00
parent ad4a8936bb
commit cf5d209f28
2 changed files with 35 additions and 2 deletions

View File

@ -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) void Physics::addSphere(float rad, float x, float y, float z, float mass, unsigned indice)
@ -129,6 +157,8 @@ void Physics::addSphere(float rad, float x, float y, float z, float mass, unsign
btRigidBody* body = new btRigidBody(info); btRigidBody* body = new btRigidBody(info);
body->setDamping(0.2f,0.4f);
world->addRigidBody(body); world->addRigidBody(body);
bodies.push_back(body); bodies.push_back(body);

View File

@ -5,11 +5,14 @@
#include <ACGL/Math/Math.hh> #include <ACGL/Math/Math.hh>
#include <vector> #include <vector>
#include "entity.hh"
#include "extern/bullet/src/BulletDynamics/Dynamics/btRigidBody.h" #include "extern/bullet/src/BulletDynamics/Dynamics/btRigidBody.h"
#include "extern/bullet/src/BulletDynamics/Dynamics/btDynamicsWorld.h" #include "extern/bullet/src/BulletDynamics/Dynamics/btDynamicsWorld.h"
#include "extern/bullet/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h" #include "extern/bullet/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h"
#include "extern/bullet/src/BulletCollision/CollisionShapes/btSphereShape.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/btHeightfieldTerrainShape.h"
#include "extern/bullet/src/BulletCollision/CollisionShapes/btStaticPlaneShape.h" #include "extern/bullet/src/BulletCollision/CollisionShapes/btStaticPlaneShape.h"
@ -47,7 +50,7 @@ class Physics {
void addTerrain(int width, int length, float** heightData); void addTerrain(int width, int length, float** heightData);
void addPlayer(float rad, float x, float y, float z, float mass, unsigned 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); 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: private:
btRigidBody* playerBody; btRigidBody* playerBody;