2014-11-14 13:17:04 +00:00
# include "physics.hh"
2014-11-28 12:52:03 +00:00
2014-11-17 11:57:16 +00:00
Physics : : Physics ( ) {
}
Physics : : ~ Physics ( ) {
}
2014-11-14 13:17:04 +00:00
2014-11-17 11:57:16 +00:00
void Physics : : init ( )
2014-11-14 13:17:04 +00:00
{
colConfig = new btDefaultCollisionConfiguration ( ) ;
dispatcher = new btCollisionDispatcher ( colConfig ) ;
broadphase = new btDbvtBroadphase ( ) ;
solver = new btSequentialImpulseConstraintSolver ( ) ;
world = new btDiscreteDynamicsWorld ( dispatcher , broadphase , solver , colConfig ) ;
2014-11-17 11:57:16 +00:00
world - > setGravity ( btVector3 ( 0 , - 10 , - 0 ) ) ;
2014-11-14 13:17:04 +00:00
}
2014-11-17 11:57:16 +00:00
void Physics : : takeUpdateStep ( float timeDiff )
2014-11-21 11:31:29 +00:00
{
2014-11-14 13:17:04 +00:00
world - > stepSimulation ( timeDiff ) ;
}
2014-11-17 11:57:16 +00:00
void Physics : : addTerrain ( int width , int length , float * * heightData )
2014-11-14 13:23:10 +00:00
{
2014-11-17 15:07:40 +00:00
2014-11-14 13:23:10 +00:00
float * heightfield = new float [ width * length ] ;
int highest = - 999999 , j = 0 , i = 0 ;
for ( i = 0 ; i < width ; i + + )
2014-11-17 15:07:40 +00:00
{
2014-11-14 13:23:10 +00:00
for ( j = 0 ; j < length ; j + + ) {
2014-11-17 15:07:40 +00:00
heightfield [ i * length + j ] = heightData [ j ] [ i ] ;
if ( heightData [ j ] [ i ] > highest )
highest = heightData [ j ] [ i ] ;
}
}
2014-11-17 17:54:19 +00:00
highest + + ;
2014-11-14 13:23:10 +00:00
2014-11-17 15:07:40 +00:00
btHeightfieldTerrainShape * terrianShape = new btHeightfieldTerrainShape ( length , width , heightfield , highest , 1 , true , false ) ;
2014-11-14 13:23:10 +00:00
btRigidBody * tBody = new btRigidBody ( 0 , new btDefaultMotionState ( ) , terrianShape ) ;
2014-11-17 17:54:19 +00:00
tBody - > getWorldTransform ( ) . setOrigin ( btVector3 ( 0 , ( ( float ) highest - 1 ) / 2 , 0 ) ) ;
2014-11-14 13:23:10 +00:00
//tBody->getWoorldTransform().setRotation(btQuaternion(0,0,0,1));
terrainBody = tBody ;
2014-11-17 15:07:40 +00:00
2014-11-14 13:23:10 +00:00
world - > addRigidBody ( terrainBody ) ;
}
2014-11-17 11:57:16 +00:00
void Physics : : addStaticGroundPlane ( )
{
btCollisionShape * groundShape = new btStaticPlaneShape ( btVector3 ( 0 , 1 , 0 ) , 0 ) ;
btDefaultMotionState * groundMotionState = new btDefaultMotionState ( btTransform ( btQuaternion ( 0 , 0 , 0 , 1 ) , btVector3 ( 0 , 0 , 0 ) ) ) ;
btRigidBody : : btRigidBodyConstructionInfo groundRigidBodyCI ( 0 , groundMotionState , groundShape , btVector3 ( 0 , 0 , 0 ) ) ;
staticGroundBody = new btRigidBody ( groundRigidBodyCI ) ;
world - > addRigidBody ( staticGroundBody ) ;
}
2014-11-21 11:56:30 +00:00
void Physics : : addPlayer ( float rad , Entity entity , float mass , unsigned indice )
2014-11-14 13:17:04 +00:00
{
2014-11-21 15:22:36 +00:00
if ( bodies . size ( ) = = indice )
2014-11-14 15:23:14 +00:00
throw std : : invalid_argument ( " Bodies out of Sync " ) ;
2014-11-14 13:17:04 +00:00
btSphereShape * sphere = new btSphereShape ( rad ) ;
btVector3 inertia ( 0 , 0 , 0 ) ;
2014-11-14 15:23:14 +00:00
if ( mass ! = 0.0 )
2014-11-14 13:17:04 +00:00
{
2014-11-14 15:23:14 +00:00
sphere - > calculateLocalInertia ( ( btScalar ) mass , inertia ) ;
2014-11-14 13:17:04 +00:00
}
2014-11-14 15:23:14 +00:00
2014-11-21 11:56:30 +00:00
btDefaultMotionState * motion = new btDefaultMotionState ( btTransform ( btQuaternion ( 0 , 0 , 0 , 1 ) , btVector3 ( entity . getPosition ( ) . x , entity . getPosition ( ) . y , entity . getPosition ( ) . z ) ) ) ;
2014-11-14 15:23:14 +00:00
btRigidBody : : btRigidBodyConstructionInfo info ( mass , motion , sphere , inertia ) ;
playerBall = new btRigidBody ( info ) ;
2014-11-17 15:07:40 +00:00
playerBall - > setDamping ( 0.1f , 0.3f ) ;
2014-11-14 15:23:14 +00:00
world - > addRigidBody ( playerBall ) ;
bodies . push_back ( playerBall ) ;
2014-11-17 15:07:40 +00:00
playerBall - > setSleepingThresholds ( 0 , 0 ) ;
2014-11-21 15:22:36 +00:00
if ( bodies . size ( ) ! = indice )
2014-11-14 15:23:14 +00:00
throw std : : invalid_argument ( " Bodies out of Sync " ) ;
}
2014-11-21 11:50:11 +00:00
void Physics : : addBox ( float width , float height , float length , Entity entity , float mass , unsigned indice )
2014-11-21 11:31:29 +00:00
{
2014-11-21 11:50:11 +00:00
2014-11-21 15:22:36 +00:00
if ( bodies . size ( ) = = indice )
throw std : : invalid_argument ( " Bodies out of Sync " ) ;
2014-11-21 11:50:11 +00:00
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 ) ;
2014-11-21 11:31:29 +00:00
2014-11-21 11:50:11 +00:00
bodies . push_back ( body ) ;
if ( mass ! = 0 )
body - > setSleepingThresholds ( 0 , 0 ) ;
if ( bodies . size ( ) ! = indice )
throw std : : invalid_argument ( " Bodies out of Sync " ) ;
2014-11-21 11:31:29 +00:00
}
2014-11-14 15:23:14 +00:00
2014-11-21 11:56:30 +00:00
void Physics : : addSphere ( float rad , Entity entity , float mass , unsigned indice )
2014-11-14 15:23:14 +00:00
{
2014-11-21 15:22:36 +00:00
if ( bodies . size ( ) = = indice )
2014-11-14 15:23:14 +00:00
throw std : : invalid_argument ( " Bodies out of Sync " ) ;
btSphereShape * sphere = new btSphereShape ( rad ) ;
btVector3 inertia ( 0 , 0 , 0 ) ;
if ( mass ! = 0.0 )
2014-11-14 13:17:04 +00:00
{
sphere - > calculateLocalInertia ( ( btScalar ) mass , inertia ) ;
}
2014-11-21 11:56:30 +00:00
btDefaultMotionState * motion = new btDefaultMotionState ( btTransform ( btQuaternion ( 0 , 0 , 0 , 1 ) , btVector3 ( entity . getPosition ( ) . x , entity . getPosition ( ) . y , entity . getPosition ( ) . z ) ) ) ;
2014-11-14 13:17:04 +00:00
btRigidBody : : btRigidBodyConstructionInfo info ( mass , motion , sphere , inertia ) ;
2014-11-17 15:07:40 +00:00
//info.
2014-11-14 13:17:04 +00:00
btRigidBody * body = new btRigidBody ( info ) ;
2014-11-21 11:50:11 +00:00
body - > setDamping ( 0.2f , 0.4f ) ;
2014-11-14 13:17:04 +00:00
world - > addRigidBody ( body ) ;
bodies . push_back ( body ) ;
2014-11-17 15:07:40 +00:00
body - > setSleepingThresholds ( 0 , 0 ) ;
2014-11-14 15:23:14 +00:00
2014-11-21 15:22:36 +00:00
if ( bodies . size ( ) ! = indice )
2014-11-14 15:23:14 +00:00
throw std : : invalid_argument ( " Bodies out of Sync " ) ;
2014-11-14 13:17:04 +00:00
}
2014-11-28 12:52:03 +00:00
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.2f , 0.4f ) ;
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 ;
}
2014-11-17 11:57:16 +00:00
glm : : vec3 Physics : : getPos ( int i )
2014-11-14 13:17:04 +00:00
{
btVector3 origin = bodies [ i ] - > getCenterOfMassPosition ( ) ;
2014-11-14 13:23:10 +00:00
glm : : vec3 save ( origin . getX ( ) , origin . getY ( ) , origin . getZ ( ) ) ;
return save ;
2014-11-14 13:17:04 +00:00
}
2014-11-17 11:57:16 +00:00
glm : : mat4 Physics : : getRotation ( int i )
2014-11-14 13:17:04 +00:00
{
2014-11-14 14:15:29 +00:00
btQuaternion quat = bodies [ i ] - > getOrientation ( ) ;
glm : : mat4 matrix = glm : : rotate (
quat . getAngle ( ) ,
glm : : vec3 ( quat . getAxis ( ) . getX ( ) , quat . getAxis ( ) . getY ( ) , quat . getAxis ( ) . getZ ( ) )
) ;
2014-11-14 14:48:50 +00:00
return matrix ;
2014-11-14 13:17:04 +00:00
}
2014-11-17 15:07:40 +00:00
void Physics : : rollForward ( glm : : vec3 camPos , float strength )
2014-11-14 13:17:04 +00:00
{
2014-11-17 12:21:27 +00:00
btVector3 pos ( camPos . x , 0 , camPos . z ) ;
2014-11-21 11:31:29 +00:00
pos . normalize ( ) ;
2014-11-17 15:07:40 +00:00
pos = btCross ( pos , btVector3 ( 0 , 1 , 0 ) ) ;
2014-11-21 11:31:29 +00:00
pos * = strength ;
2014-11-17 12:12:51 +00:00
playerBall - > applyTorque ( pos ) ;
2014-11-17 15:07:40 +00:00
}
2014-11-17 12:12:51 +00:00
2014-11-17 15:07:40 +00:00
void Physics : : rollBack ( glm : : vec3 camPos , float strength )
{
btVector3 pos ( camPos . x , 0 , camPos . z ) ;
2014-11-21 11:31:29 +00:00
pos . normalize ( ) ;
2014-11-17 15:07:40 +00:00
pos = btCross ( btVector3 ( 0 , 1 , 0 ) , pos ) ;
2014-11-21 11:31:29 +00:00
pos * = strength ;
2014-11-17 15:07:40 +00:00
playerBall - > applyTorque ( pos ) ;
}
void Physics : : rollLeft ( glm : : vec3 camPos , float strength )
{
btVector3 pos ( camPos . x , 0 , camPos . z ) ;
2014-11-21 11:31:29 +00:00
pos . normalize ( ) ;
pos * = strength ;
2014-11-17 15:07:40 +00:00
playerBall - > applyTorque ( pos ) ;
}
void Physics : : rollRight ( glm : : vec3 camPos , float strength )
{
btVector3 pos ( camPos . x , 0 , camPos . z ) ;
2014-11-21 11:31:29 +00:00
pos . normalize ( ) ;
pos * = strength ;
2014-11-17 15:07:40 +00:00
playerBall - > applyTorque ( - pos ) ;
2014-11-14 13:17:04 +00:00
}
/*
void kill ( )
{
//btDynamimcWorld*
for ( int i = 0 ; i < bodies . size ( ) ; i + + )
{
world - > removeCollisionObject ( bodies [ i ] ) ; //clarification: go through the list of bodies in wordl for each body b, then remove exactly this body b from world
btMotionState * motionState = bodies [ i ] - > getMotionState ( ) ;
btCollisionShape * shape = bodies [ i ] - > getCollisionShape ( ) ;
delete shape ;
delete motionState ;
delete bodies [ i ] ;
}
delete dispatcher ;
delete colConfig ;
delete solver ;
}
delete broadphase ;
delete world ;
}
*/