2014-11-14 13:17:04 +00:00
# include "physics.hh"
2014-12-15 15:02:04 +00:00
# include "extern/bullet/Extras/Serialize/BulletWorldImporter/btBulletWorldImporter.h"
2014-11-14 13:17:04 +00:00
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
2015-01-29 12:15:45 +00:00
void Physics : : init ( ) //prepares bullet by creating all initial classes
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 ) ;
2015-01-13 16:50:15 +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 )
2015-01-16 15:17:26 +00:00
{
2015-01-29 12:15:45 +00:00
world - > stepSimulation ( timeDiff ) ; //allows the world to be simmulated correctly indipendant of the timedifferences between frames
for ( unsigned i = 0 ; i < allPositionConstraints . size ( ) ; i + + ) //this handles the spring constraints
2015-01-16 15:17:26 +00:00
{
2015-01-29 12:15:45 +00:00
if ( allPositionConstraints [ i ] . position ! = allPositionConstraints [ i ] . body - > getCenterOfMassPosition ( ) ) //if constraint != position of the body because otherwise dir = 0
2015-01-16 15:17:26 +00:00
{
2015-01-23 11:59:28 +00:00
btVector3 dir = allPositionConstraints [ i ] . position - allPositionConstraints [ i ] . body - > getCenterOfMassPosition ( ) ;
2015-01-29 12:15:45 +00:00
allPositionConstraints [ i ] . body - > applyCentralForce ( dir * allPositionConstraints [ i ] . strength ) ; //apply a foce upon the object pushing it towards the constraint position
2015-01-16 15:17:26 +00:00
}
}
2015-01-29 12:15:45 +00:00
btVector3 position = cameraBody - > getCenterOfMassPosition ( ) - playerBall - > getCenterOfMassPosition ( ) ; //gets a vector from the player to the camera
2015-01-19 15:30:11 +00:00
position . normalize ( ) ;
position * = 5 ;
position + = playerBall - > getCenterOfMassPosition ( ) ; //is the position 5 units away from the player in the direction of the camera
btVector3 dir = cameraBody - > getCenterOfMassPosition ( ) - position ;
2015-01-29 12:15:45 +00:00
cameraBody - > applyCentralForce ( dir * cameraBody - > getInvMass ( ) ) ; //scale the force by camera mass
cameraBody - > applyCentralForce ( btVector3 ( 0 , 1 , 0 ) ) ; //applies a force to the camera keeping it in a correct position
2015-01-19 15:30:11 +00:00
2014-11-14 13:17:04 +00:00
}
2015-01-29 12:15:45 +00:00
void Physics : : removePositionConstraint ( int bodyIndice ) //remover function for deleting all pos constraints on one body
2015-01-16 15:17:26 +00:00
{
2015-01-17 12:30:33 +00:00
for ( unsigned i = 0 ; i < allPositionConstraints . size ( ) ; i + + )
2015-01-16 15:17:26 +00:00
{
2015-01-17 12:30:33 +00:00
if ( allPositionConstraints [ i ] . body = = bodies [ bodyIndice ] )
2015-01-16 15:17:26 +00:00
{
allPositionConstraints . erase ( allPositionConstraints . begin ( ) + i ) ;
}
}
}
2015-01-29 12:15:45 +00:00
void Physics : : addPositionConstraint ( int bodyIndice , float strength , glm : : vec3 position ) //function for adding position constraints
2015-01-16 15:17:26 +00:00
{
positionConstraint cons ;
cons . body = bodies [ bodyIndice ] ;
cons . strength = strength ;
cons . position = btVector3 ( position . x , position . y , position . z ) ;
allPositionConstraints . push_back ( cons ) ;
}
2015-01-29 12:15:45 +00:00
//players and objects
void Physics : : addPlayer ( float friction , float rad , Entity entity , float mass , float dampningL , float dampningA , unsigned indice )
{
if ( bodies . size ( ) = = indice )
throw std : : invalid_argument ( " Bodies out of Sync " ) ; //these error are to ensure that level can always communicate with physics without having to worry about synching errors
btSphereShape * sphere = new btSphereShape ( rad ) ; //the first thing we need for a rigid body is the shape
btVector3 inertia ( 0 , 0 , 0 ) ;
if ( mass ! = 0.0 )
{
sphere - > calculateLocalInertia ( ( btScalar ) mass , inertia ) ; //from this shape we can then calculate the innertia, as long as the mass != 0 (otherwise inertia = 0)
}
btDefaultMotionState * motion = new btDefaultMotionState ( btTransform ( btQuaternion ( 0 , 0 , 0 , 1 ) , btVector3 ( entity . getPosition ( ) . x , entity . getPosition ( ) . y , entity . getPosition ( ) . z ) ) ) ; //next we define the motionstate, wich describes the innital position and rotation
btRigidBody : : btRigidBodyConstructionInfo info ( mass , motion , sphere , inertia ) ; //next we process all data for the rigid body into info
info . m_friction = friction * 2 ; //here we modify the friction and restitution (bounciness) of the object
info . m_restitution = 0.2f ;
playerBall = new btRigidBody ( info ) ; //finally we create the rigid body using the info
2014-12-15 15:02:04 +00:00
2015-01-29 12:15:45 +00:00
playerBall - > setDamping ( dampningL , dampningA ) ; //here we can set the dampning (how much of the motion is lost)
2014-12-15 15:02:04 +00:00
2015-01-29 12:15:45 +00:00
world - > addRigidBody ( playerBall , COL_OBJECTS , COL_OBJECTS | COL_OBJECTS_NO_TERRAIN | COL_TERRAIN ) ; //then we add the rigid body to the wiorld, allowing it to be simulated
bodies . push_back ( playerBall ) ; //next we add the rigid body to our own list (for cleanup and for synchronitaation with level)
2014-12-15 15:02:04 +00:00
2015-01-29 12:15:45 +00:00
playerBall - > setSleepingThresholds ( 0 , 0 ) ; //in a final step we make sure that the body never is removed from the active rigid bodies
if ( bodies . size ( ) ! = indice )
throw std : : invalid_argument ( " Bodies out of Sync " ) ; //one last check to make sure level and physics are in synch
addCamera ( ) ; //now that the player exists add a camera for the player
2014-12-15 15:02:04 +00:00
}
2015-01-29 12:15:45 +00:00
void Physics : : addTerrain ( int width , int length , float * * heightData ) //The terrian adding function
2014-11-14 13:23:10 +00:00
{
2014-11-17 15:07:40 +00:00
2015-01-29 12:15:45 +00:00
float * heightfield = new float [ width * length ] ; //bullet only accepts data in a one dimensional array, so parse data into appropriate format
2014-11-14 13:23:10 +00:00
int highest = - 999999 , j = 0 , i = 0 ;
for ( i = 0 ; i < width ; i + + )
2014-11-17 15:07:40 +00:00
{
2015-01-29 12:15:45 +00:00
for ( j = 0 ; j < length ; j + + ) {
heightfield [ i * length + j ] = heightData [ j ] [ i ] ; //reverse order because they are loaded backwards
2014-11-17 15:07:40 +00:00
2015-01-29 12:15:45 +00:00
if ( heightData [ j ] [ i ] > highest )
highest = heightData [ j ] [ i ] ; //bullet needs to know the highest point of the heightmap
2014-11-17 15:07:40 +00:00
}
}
2014-11-17 17:54:19 +00:00
highest + + ;
2014-11-14 13:23:10 +00:00
2015-01-29 12:15:45 +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 ) ;
2015-01-29 12:15:45 +00:00
tBody - > getWorldTransform ( ) . setOrigin ( btVector3 ( 0 , ( ( float ) highest ) / 2 , 0 ) ) ; //we have to move the origin of our rigid body down, because bullet sets the origin (0,0,0) at (width/2, height/2, length/2) in the map the x and z are correct in our level, but y needs to be addapted
2014-11-14 13:23:10 +00:00
terrainBody = tBody ;
2014-11-17 15:07:40 +00:00
2015-01-29 12:15:45 +00:00
world - > addRigidBody ( terrainBody , COL_TERRAIN , COL_TERRAIN | COL_OBJECTS ) ; //COL_XXXX are collision masks, allowing us to ignore collisions between certain object groups (required for buttons)
2014-11-14 13:23:10 +00:00
}
2015-01-29 12:15:45 +00:00
void Physics : : addTriangleMeshBody ( Entity entity , std : : string path , float mass , float dampningL , float dampningA , unsigned indice , float scaling , bool rotate )
2014-11-14 13:17:04 +00:00
{
2014-12-15 15:02:04 +00:00
if ( bodies . size ( ) = = indice )
throw std : : invalid_argument ( " Bodies out of Sync " ) ;
2015-01-29 12:15:45 +00:00
std : : vector < unsigned int > vertexIndices ; //temp lists for data sets
2014-12-15 15:02:04 +00:00
std : : vector < btVector3 > temp_vertices ;
path = " ../Levels/Geometry/ " + path ;
FILE * file = fopen ( path . c_str ( ) , " r " ) ;
if ( file = = NULL ) {
2015-01-29 12:15:45 +00:00
throw std : : invalid_argument ( " Impossible to open the file " ) ; //create correct filepath and report error if cannot open
2014-12-15 15:02:04 +00:00
}
while ( 1 ) {
char lineHeader [ 128 ] ;
// read the first word of the line
int res = fscanf ( file , " %s " , lineHeader ) ;
if ( res = = EOF )
2015-01-29 12:15:45 +00:00
break ; // while not at end do loop
if ( strcmp ( lineHeader , " v " ) = = 0 ) { //if a vertex
2014-12-15 15:02:04 +00:00
glm : : vec3 vertex ;
fscanf ( file , " %f %f %f \n " , & vertex . x , & vertex . y , & vertex . z ) ;
2015-01-29 12:15:45 +00:00
temp_vertices . push_back ( btVector3 ( vertex . x , vertex . y , vertex . z ) ) ; //save vertex in array
2014-12-15 15:02:04 +00:00
}
2015-01-29 12:15:45 +00:00
else if ( strcmp ( lineHeader , " f " ) = = 0 ) { //if face (index for 3 vertexes for a triangle)
2014-12-15 15:02:04 +00:00
std : : string vertex1 , vertex2 , vertex3 ;
unsigned int vertexIndex [ 3 ] , uvIndex [ 3 ] , normalIndex [ 3 ] ;
int matches = fscanf ( file , " %d/%d/%d %d/%d/%d %d/%d/%d \n " , & vertexIndex [ 0 ] , & uvIndex [ 0 ] , & normalIndex [ 0 ] , & vertexIndex [ 1 ] , & uvIndex [ 1 ] , & normalIndex [ 1 ] , & vertexIndex [ 2 ] , & uvIndex [ 2 ] , & normalIndex [ 2 ] ) ;
vertexIndices . push_back ( vertexIndex [ 0 ] ) ;
vertexIndices . push_back ( vertexIndex [ 1 ] ) ;
2015-01-29 12:15:45 +00:00
vertexIndices . push_back ( vertexIndex [ 2 ] ) ; //save 3 indexes in array
2014-12-15 15:02:04 +00:00
}
}
//finally start making body
btTriangleMesh * triMesh = new btTriangleMesh ( ) ;
2014-12-19 14:20:05 +00:00
for ( unsigned i = 2 ; i < vertexIndices . size ( ) ; i + = 3 )
2014-12-15 15:02:04 +00:00
{
2015-01-29 12:15:45 +00:00
triMesh - > addTriangle ( temp_vertices [ vertexIndices [ i ] ] , temp_vertices [ vertexIndices [ i - 1 ] ] , temp_vertices [ vertexIndices [ i - 2 ] ] ) ; // for every face (3 elements in vertexIndices) create triangle
2014-12-15 15:02:04 +00:00
}
btBvhTriangleMeshShape * shape = new btBvhTriangleMeshShape ( triMesh , true ) ;
2015-01-29 12:15:45 +00:00
shape - > setLocalScaling ( btVector3 ( scaling , scaling , scaling ) ) ; //we need to add a scaling here because the objects seem to have diffrent sizes when loaded (no clue why, see composition.xml for exact scaling factors)
2014-12-15 15:02:04 +00:00
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 ) ;
2015-01-29 12:15:45 +00:00
if ( mass ! = 0.0 & & rotate ) //&& rotate lets certain objects get inertia (0,0,0) (not rotateable)
2014-12-15 15:02:04 +00:00
{
shape - > calculateLocalInertia ( ( btScalar ) mass , inertia ) ;
}
btRigidBody : : btRigidBodyConstructionInfo info ( mass , motion , shape , inertia ) ;
btRigidBody * body = new btRigidBody ( info ) ;
body - > setDamping ( dampningL , dampningA ) ;
bodies . push_back ( body ) ;
2015-01-19 14:24:44 +00:00
world - > addRigidBody ( body , COL_OBJECTS , objectsPhysicsCollision ) ;
2014-12-15 15:02:04 +00:00
if ( bodies . size ( ) ! = indice )
throw std : : invalid_argument ( " Bodies out of Sync " ) ;
}
2015-01-23 12:31:19 +00:00
void Physics : : addButton ( float width , float height , float length , Entity entity , float mass , float dampningL , float dampningA , unsigned indice , bool rotate )
2015-01-19 13:01:05 +00:00
{
if ( bodies . size ( ) = = indice )
throw std : : invalid_argument ( " Bodies out of Sync " ) ;
2015-01-23 11:05:43 +00:00
btBoxShape * box = new btBoxShape ( btVector3 ( width / 2 , height / 2 , length / 2 ) ) ;
2015-01-19 13:01:05 +00:00
btDefaultMotionState * motion = new btDefaultMotionState ( btTransform ( btQuaternion ( 0 , 0 , 0 , 1 ) , btVector3 ( entity . getPosition ( ) . x , entity . getPosition ( ) . y , entity . getPosition ( ) . z ) ) ) ;
2015-01-29 12:15:45 +00:00
btVector3 inertia ( 0 , 0 , 0 ) ;
2015-01-19 13:01:05 +00:00
2015-01-23 11:05:43 +00:00
btRigidBody : : btRigidBodyConstructionInfo info ( mass , motion , box , inertia ) ;
2015-01-19 13:01:05 +00:00
btRigidBody * body = new btRigidBody ( info ) ;
body - > setDamping ( dampningL , dampningA ) ;
world - > addRigidBody ( body , COL_OBJECTS_NO_TERRAIN , specialPhysicsCollision ) ;
bodies . push_back ( body ) ;
if ( bodies . size ( ) ! = indice )
throw std : : invalid_argument ( " Bodies out of Sync " ) ;
}
2015-01-23 12:31:19 +00:00
void Physics : : addBox ( float width , float height , float length , Entity entity , float mass , float dampningL , float dampningA , unsigned indice , bool rotate )
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 ) ;
2015-01-06 12:31:53 +00:00
body - > setDamping ( dampningL , dampningA ) ;
2014-11-21 11:50:11 +00:00
2015-01-19 13:01:05 +00:00
world - > addRigidBody ( body , COL_OBJECTS , objectsPhysicsCollision ) ;
2014-11-21 11:50:11 +00:00
bodies . push_back ( body ) ;
2014-11-28 15:46:35 +00:00
2014-11-21 11:50:11 +00:00
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
2015-01-23 12:31:19 +00:00
void Physics : : addSphere ( float rad , Entity entity , float mass , float dampningL , float dampningA , unsigned indice , bool rotate )
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-12-15 15:02:04 +00:00
2014-11-14 13:17:04 +00:00
btRigidBody * body = new btRigidBody ( info ) ;
2014-11-21 11:50:11 +00:00
2015-01-06 12:31:53 +00:00
body - > setDamping ( dampningL , dampningA ) ;
2014-11-14 13:17:04 +00:00
2015-01-19 13:01:05 +00:00
world - > addRigidBody ( body , COL_OBJECTS , objectsPhysicsCollision ) ;
2014-11-14 13:17:04 +00:00
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 15:46:35 +00:00
2015-01-19 15:30:11 +00:00
void Physics : : addCamera ( )
2014-11-28 12:52:03 +00:00
{
2015-01-19 15:30:11 +00:00
btSphereShape * sphere = new btSphereShape ( 0.5f ) ;
2014-11-28 12:52:03 +00:00
btVector3 inertia ( 0 , 0 , 0 ) ;
2015-01-19 15:51:35 +00:00
btDefaultMotionState * motion = new btDefaultMotionState ( btTransform ( btQuaternion ( 0 , 0 , 0 , 1 ) , playerBall - > getCenterOfMassPosition ( ) + btVector3 ( 5 , 5 , 5 ) ) ) ;
2014-11-28 12:52:03 +00:00
btRigidBody : : btRigidBodyConstructionInfo info ( 1 / ( playerBall - > getInvMass ( ) / 100 ) , motion , sphere , inertia ) ;
cameraBody = new btRigidBody ( info ) ;
2015-01-19 16:12:58 +00:00
cameraBody - > setDamping ( 1 , 0.5 ) ;
2014-11-28 12:52:03 +00:00
2015-01-19 14:24:44 +00:00
world - > addRigidBody ( cameraBody , COL_OBJECTS , objectsPhysicsCollision ) ;
2014-11-28 12:52:03 +00:00
cameraBody - > setSleepingThresholds ( 0 , 0 ) ;
2015-01-29 12:15:45 +00:00
cameraBody - > setGravity ( btVector3 ( 0 , 0 , 0 ) ) ;
2014-11-28 12:52:03 +00:00
2015-01-19 15:30:11 +00:00
/*btVector3 pivotInA(5,0,0);
2014-11-28 12:52:03 +00:00
btVector3 pivotInB ( - 5 , 0 , 0 ) ;
btDistanceConstraint * pdc = new btDistanceConstraint ( * cameraBody , * playerBall , pivotInA , pivotInB , distance ) ;
2015-01-19 15:30:11 +00:00
world - > addConstraint ( pdc ) ; */
2014-11-28 12:52:03 +00:00
}
2014-12-15 15:02:04 +00:00
//update functions
2014-11-28 12:52:03 +00:00
glm : : vec3 Physics : : getCameraPosition ( )
{
btVector3 origin = cameraBody - > getCenterOfMassPosition ( ) ;
glm : : vec3 save ( origin . getX ( ) , origin . getY ( ) , origin . getZ ( ) ) ;
return save ;
}
2015-01-19 15:51:35 +00:00
glm : : vec3 Physics : : getCameraToPlayer ( )
{
btVector3 origin = playerBall - > getCenterOfMassPosition ( ) - 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
}
2015-01-19 15:30:11 +00:00
void Physics : : updateCameraPos ( glm : : vec2 mouseMovement , float strength )
{
btVector3 change = playerBall - > getCenterOfMassPosition ( ) - cameraBody - > getCenterOfMassPosition ( ) ; ;
change . setY ( 0 ) ;
change . normalize ( ) ;
change * = mouseMovement . x ;
change = btCross ( btVector3 ( 0 , 1 , 0 ) , change ) ;
change . setY ( mouseMovement . y ) ;
change * = strength ;
2015-01-19 16:12:58 +00:00
cameraBody - > applyCentralForce ( change ) ;
2015-01-19 15:30:11 +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
}
2015-01-19 15:51:35 +00:00
2015-01-16 15:17:26 +00:00
//not used right now
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-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 ;
}
*/