2014-10-20 16:48:20 +00:00
# ifndef PHYSICS_HH_INCLUDED
# define PHYSICS_HH_INCLUDED
2014-10-20 16:39:16 +00:00
2014-11-14 13:17:04 +00:00
# include <ACGL/Base/Settings.hh>
# include <ACGL/Math/Math.hh>
# include <vector>
2014-12-15 15:02:04 +00:00
# include <string>
# include <stdio.h>
2014-11-14 13:17:04 +00:00
2014-11-21 11:50:11 +00:00
# include "entity.hh"
2015-03-12 14:04:26 +00:00
# include "BulletDynamics/Dynamics/btRigidBody.h"
# include "BulletDynamics/Dynamics/btDynamicsWorld.h"
# include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h"
# include "BulletCollision/CollisionShapes/btSphereShape.h"
# include "BulletCollision/CollisionShapes/btBoxShape.h"
# include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
# include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
# include "BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h"
# include "BulletCollision/CollisionShapes/btTriangleMesh.h"
# include "BulletCollision/CollisionShapes/btCollisionShape.h"
# include "BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h"
# include "BulletCollision/CollisionShapes/btCylinderShape.h"
# include "BulletCollision/CollisionShapes/btConvexHullShape.h"
# include "BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h"
# include "BulletDynamics/ConstraintSolver/btConstraintSolver.h"
# include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h" //YAY!
# include "BulletDynamics/ConstraintSolver/btPoint2PointConstraint.h"
# include "BulletCollision/CollisionDispatch/btCollisionConfiguration.h"
# include "BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h"
# include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h"
# include "BulletCollision/BroadphaseCollision/btDbvtBroadphase.h"
# include "BulletCollision/BroadphaseCollision/btDispatcher.h"
# include "LinearMath/btScalar.h"
# include "LinearMath/btMotionState.h"
# include "LinearMath/btDefaultMotionState.h"
# include "LinearMath/btQuaternion.h"
# include "LinearMath/btVector3.h"
# include "LinearMath/btMatrix3x3.h"
2014-11-14 13:17:04 +00:00
2014-11-07 15:44:12 +00:00
class Physics {
public :
2014-11-17 11:57:16 +00:00
Physics ( ) ;
2014-11-14 13:17:04 +00:00
~ Physics ( ) ;
2015-02-06 17:00:14 +00:00
void init ( std : : string geometryPath ) ;
2014-11-28 11:06:17 +00:00
void takeUpdateStep ( float timeDiff ) ; //must be used in level.update to proagate the physics
void rollForward ( glm : : vec3 camPos , float strength ) ; //self explainitory
2014-11-17 15:07:40 +00:00
void rollLeft ( glm : : vec3 camPos , float strength ) ;
void rollRight ( glm : : vec3 camPos , float strength ) ;
void rollBack ( glm : : vec3 camPos , float strength ) ;
2014-11-14 13:23:10 +00:00
glm : : vec3 getPos ( int i ) ;
2014-11-14 14:15:29 +00:00
glm : : mat4 getRotation ( int i ) ;
2014-11-17 11:57:16 +00:00
void addStaticGroundPlane ( ) ;
2015-02-13 14:20:32 +00:00
void updateCameraPos ( glm : : vec2 mouseMovement , float strength , float distance ) ;
2014-11-28 12:52:03 +00:00
glm : : vec3 getCameraPosition ( ) ;
2015-01-06 12:31:53 +00:00
void addRigidBodyFromFile ( Entity entity , float mass , float dampningL , float dampningA , std : : string modelLocation , unsigned indice ) ;
2015-01-29 12:15:45 +00:00
void addTriangleMeshBody ( Entity entity , std : : string path , float mass , float dampningL , float dampningA , unsigned indice , float scaling , bool rotate ) ;
2014-11-14 13:23:10 +00:00
void addTerrain ( int width , int length , float * * heightData ) ;
2015-01-06 12:31:53 +00:00
void addPlayer ( float friction , float rad , Entity entity , float mass , float dampningL , float dampningA , unsigned indice ) ; //use these AFTER physicObjects.push_back(object)! if mass == 0 then the object is unmoveable
2015-01-23 12:31:19 +00:00
void addSphere ( float rad , Entity entity , float mass , float dampningL , float dampningA , unsigned indice , bool rotate ) ; //The Indice should be set to physicObjects.size()
void addBox ( float width , float height , float length , Entity entity , float mass , float dampningL , float dampningA , unsigned indice , bool rotate ) ; //this is used to ensuer that the system is synchronized
2015-01-16 15:17:26 +00:00
void addPositionConstraint ( int bodyIndice , float strength , glm : : vec3 position ) ;
void removePositionConstraint ( int bodyIndice ) ;
2015-01-23 12:31:19 +00:00
void addButton ( float width , float height , float length , Entity entity , float mass , float dampningL , float dampningA , unsigned indice , bool rotate ) ;
2015-01-19 15:51:35 +00:00
glm : : vec3 getCameraToPlayer ( ) ;
2015-01-30 12:32:16 +00:00
void kill ( ) ;
2015-01-30 16:29:48 +00:00
void addButtonFrame ( Entity entity ) ;
2015-02-13 16:22:01 +00:00
void forceMove ( glm : : vec3 newPosition , unsigned indice ) ;
2015-03-06 14:01:22 +00:00
void forceMoveCamera ( btVector3 newPosition ) ;
2015-02-27 14:58:21 +00:00
void addConvexBody ( Entity entity , std : : string path , float mass , float dampningL , float dampningA , unsigned indice , float scaling , bool rotate ) ;
2015-03-04 15:01:55 +00:00
void prepareCollisionDetection ( ) ;
bool playerWithGround ( ) ;
bool playerWithObject ( ) ;
2015-03-06 15:19:57 +00:00
void activateEndgame ( ) ;
2015-03-04 15:01:55 +00:00
void forcePlayer ( glm : : vec3 newPosition ) ;
2015-03-13 14:30:21 +00:00
btDynamicsWorld * getWorld ( ) ;
2015-01-16 15:17:26 +00:00
struct positionConstraint { btRigidBody * body ; float strength ; btVector3 position ; } ;
2014-11-14 13:23:10 +00:00
2014-11-14 13:17:04 +00:00
private :
2015-03-06 14:01:22 +00:00
btVector3 startPosition = btVector3 ( 0 , 0 , 0 ) ;
float resetHight = 0 ;
bool simulationActive = true ;
bool sinking = true ;
2015-03-06 15:19:57 +00:00
bool endgame = false ;
2015-03-09 17:57:40 +00:00
btVector3 currentDirection = btVector3 ( - 1 , 1 , 1 ) ;
2014-12-15 15:02:04 +00:00
btRigidBody * playerBall ; //allows for easier access to the ball
2014-11-28 11:06:17 +00:00
btRigidBody * terrainBody ; //duh
2014-11-28 12:52:03 +00:00
btRigidBody * cameraBody ;
2014-11-14 13:17:04 +00:00
std : : vector < btRigidBody * > bodies ; //list of all bodies. bodies are also in world, but save again to ease cleaning up process.
2015-01-30 12:32:16 +00:00
btRigidBody * staticGroundBody ;
2015-01-16 15:17:26 +00:00
std : : vector < positionConstraint > allPositionConstraints ;
2015-01-30 16:29:48 +00:00
void addCamera ( ) ; //Do NOT impliment before Player has been created;
2014-12-15 15:02:04 +00:00
2015-02-06 17:00:14 +00:00
btDynamicsWorld * world = NULL ; //contains physical attributes of the world.
2015-02-13 12:46:41 +00:00
btDispatcher * dispatcher ;
2014-11-14 13:17:04 +00:00
btCollisionConfiguration * colConfig ; //defines the type of collision detection.
btBroadphaseInterface * broadphase ; //defines how objects are culled from collision detection.
btConstraintSolver * solver ; //solver for forces and impulses.
2015-01-19 13:01:05 +00:00
int objectsPhysicsCollision = 1 | 2 | 4 ;
int specialPhysicsCollision = 2 | 4 ;
int terrainPhysicsCollision = 2 ;
2015-01-30 16:29:48 +00:00
int counter = 0 ;
2015-02-06 17:00:14 +00:00
std : : string geometryPath ;
2015-02-13 14:20:32 +00:00
float cameraDistance = 5 ; //distance of the camera to the player.
2015-03-04 15:01:55 +00:00
bool playerTerrainCol = false ;
bool playerObjectColision = false ;
2014-11-07 15:44:12 +00:00
} ;
2014-11-28 12:52:03 +00:00
2014-12-15 15:02:04 +00:00
enum collisionTypes {
COL_NOTHING = 0 ,
COL_TERRAIN = 1 ,
COL_OBJECTS = 2 ,
COL_OBJECTS_NO_TERRAIN = 4
} ;
2014-11-28 12:52:03 +00:00
2015-01-19 13:01:05 +00:00
//world->addRigidBody(playerBall,COL_OBJECTS_NO_TERRAIN, COL_OBJECTS);
2014-11-28 12:52:03 +00:00
class btDistanceConstraint : public btPoint2PointConstraint
{
protected :
2015-02-13 12:46:41 +00:00
btScalar m_distance ;
2014-11-28 12:52:03 +00:00
public :
btDistanceConstraint ( btRigidBody & rbA , btRigidBody & rbB , const btVector3 & pivotInA , const btVector3 & pivotInB , btScalar dist )
: btPoint2PointConstraint ( rbA , rbB , pivotInA , pivotInB )
{
m_distance = dist ;
}
virtual void getInfo1 ( btConstraintInfo1 * info )
{
info - > m_numConstraintRows = 1 ;
info - > nub = 5 ;
}
virtual void getInfo2 ( btConstraintInfo2 * info )
{
btVector3 relA = m_rbA . getCenterOfMassTransform ( ) . getBasis ( ) * getPivotInA ( ) ;
btVector3 relB = m_rbB . getCenterOfMassTransform ( ) . getBasis ( ) * getPivotInB ( ) ;
btVector3 posA = m_rbA . getCenterOfMassTransform ( ) . getOrigin ( ) + relA ;
btVector3 posB = m_rbB . getCenterOfMassTransform ( ) . getOrigin ( ) + relB ;
btVector3 del = posB - posA ;
btScalar currDist = btSqrt ( del . dot ( del ) ) ;
btVector3 ortho = del / currDist ;
info - > m_J1linearAxis [ 0 ] = ortho [ 0 ] ;
info - > m_J1linearAxis [ 1 ] = ortho [ 1 ] ;
info - > m_J1linearAxis [ 2 ] = ortho [ 2 ] ;
btVector3 p , q ;
p = relA . cross ( ortho ) ;
q = relB . cross ( ortho ) ;
info - > m_J1angularAxis [ 0 ] = p [ 0 ] ;
info - > m_J1angularAxis [ 1 ] = p [ 1 ] ;
info - > m_J1angularAxis [ 2 ] = p [ 2 ] ;
info - > m_J2angularAxis [ 0 ] = - q [ 0 ] ;
info - > m_J2angularAxis [ 1 ] = - q [ 1 ] ;
info - > m_J2angularAxis [ 2 ] = - q [ 2 ] ;
btScalar rhs = ( currDist - m_distance ) * info - > fps * info - > erp ;
info - > m_constraintError [ 0 ] = rhs ;
info - > cfm [ 0 ] = btScalar ( 0.f ) ;
info - > m_lowerLimit [ 0 ] = - SIMD_INFINITY ;
info - > m_upperLimit [ 0 ] = SIMD_INFINITY ;
}
} ;
2014-10-20 16:48:20 +00:00
# endif