📄 physics.h
字号:
#define BLOCK_POINT_5_Z BLOCK_POINT_1_Z
#define BLOCK_POINT_6_X BLOCK_POINT_4_X
#define BLOCK_POINT_6_Y BLOCK_POINT_2_Y
#define BLOCK_POINT_6_Z BLOCK_POINT_1_Z
#define BLOCK_POINT_7_X BLOCK_POINT_4_X
#define BLOCK_POINT_7_Y BLOCK_POINT_2_Y
#define BLOCK_POINT_7_Z BLOCK_POINT_0_Z
/************************************************************************/
/* the id index of the variable Bodies */
/************************************************************************/
#define ID_BALL 0
#define ID_TENPIN_0 1
#define ID_TENPIN_1 2
#define ID_TENPIN_2 3
#define ID_TENPIN_3 4
#define ID_TENPIN_4 5
#define ID_TENPIN_5 6
#define ID_TENPIN_6 7
#define ID_TENPIN_7 8
#define ID_TENPIN_8 9
#define ID_TENPIN_9 10
#define ID_TENPIN_10 11
#define NUM_TOTAL TOTAL_OBJECT
#define NUMBODIES NUM_TOTAL
#define NUM_COLLISION NUMBODIES*16
/************************************************************************/
/* Physics class for simulation */
/************************************************************************/
class Physics{
private:
//------------------------------------------------------------------------//
// Rigid body structure
//------------------------------------------------------------------------//
typedef struct _RigidBody {
PhType fMass; // total mass (constant)
Matrix3x3 mInertia; // mass moment of inertia in body coordinates (constant)
Matrix3x3 mInertiaInverse;// inverse of mass moment of inertia matrix (constant)
Vector vPosition; // position in earth coordinates
Vector vVelocity; // velocity in earth coordinates
Vector vVelocityBody; // velocity in body coordinates
Vector vAcceleration; // acceleration of obj in earth space
Vector vAngularAcceleration; //angular acceleration in body coordinates
Vector vAngularVelocity;// angular velocity in body coordinates
Vector vEulerAngles; // Euler angles in body coordinates
PhType fSpeed; // speed (magnitude of the velocity)
Quaternion qOrientation; // orientation in earth coordinates
Vector vForces; // total force on body
Vector vMoments; // total moment (torque) on body
Matrix3x3 mIeInverse; // inverse of moment of inertia in earth coordinates
Matrix3x3 R;
Vector vAngularVelocityGlobal; // angular velocity in terms of earth fixed coords.
Vector vAngularAccelerationGlobal; // angular acceleration in terms of earth fixed coords.
//--------for collision use(shape vertex)---------------------//
PhType fRadius; // collision radius
//Vector vVertexList[8];
int status; // bodies status
} RigidBody, *pRigidBody;
typedef struct _Collision {
int body1; //id of the body1
int body2; //id of the body2
Vector vCollisionNormal; // outward from face of body2
Vector vCollisionPoint; // in global coordinates
Vector vRelativeVelocity;// the relative velocity of the 2 objects
Vector vRelativeAcceleration;// the relative acceleration of the 2 objects
Vector vCollisionTangent;// the Tangent of the collision point
} Collision, *pCollision;
typedef enum _collisionState{// the state of the collision
UNCHECKED=0,
NO_CONTACT,
HAVE_COLLISION
} collisionState;
public:
/*----------------------------------------------------------------------------------
rigid body [0] is the ball; rigid body [1] to rigid body [10] are the tenpins
//////rigid body [11] is groud ; rigid body [12] to rigid body [15] are the enclosures
----------------------------------------------------------------------------------*/
RigidBody Bodies[TOTAL_OBJECT]; //
int isCollided; // true if the ball hit the tenpin. used to play first collision sound
private:
Collision Collisions[NUM_COLLISION];// the collision data, used to record the collided object collision information
int NumCollisions ; // the total number of collision data structure
//PhType tol;
int colState[TOTAL_OBJECT][TOTAL_OBJECT]; // for collision check
int objStatus[TOTAL_OBJECT]; // the current status of the tenpins. used for cscene.cpp to end the simulation
public:
/*init all the rigid data.
pInitTenpinPosition: the initial position of all the tenpins.
*/
void InitializeObjects(int const ((*pInitTenpinPosition)[2]));
/*step dt time in the simulation loop.
dttime: the delta time of the simulation.
*/
void StepSimulation(PhType dtime);
//for scene
/* test the tenpin shaking.
Id: the index of the test tenpin.
return true if the tenpin is shaking.
*/
bool isTenpinShaking(int Id);
/*
set the weight of the ball
weight: the wait index of the current ball.
*/
void setBallWeight(int weight);
private:
// calculate total forces and moments, update the ground collision and contact
void CalcObjectForces(void);
/*
set the mass property of the given object.(the mass and ineria)
obj: the point to the given RigidBody type object.
isBall: if the current obj is ball.
*/
void setMassProperty (RigidBody* obj, boolean isBall);
/*
set the collision data of the given object. (set status and the collision radius)
obj: the point to the given RigidBody type object.
isBall: if the current obj is ball.
*/
void setCollisionData(RigidBody* obj, boolean isBall);
//for collision
// resolve the ground collision
void resolveGroundCollisions();
// check all the bodies, set all the collided object to the Collisions variable
int checkForCollisions();
// return 1 if the sphere with the centre p1 and radius r1 is collide with the sphere with the centre p2 and radius r2. else 0;
int dCollideSpheres(Vector p1, PhType r1, Vector p2, PhType r2);
/* return 1 if cylinder id1 collide with sphere id2. else 0;
if collision occured, the collisionData will filled with the collision data and the count is the the collision point number.
*/
int dCollideCylinderSphere(int id1, int id2, pCollision collisionData,int &count);
/* return 1 if cylinder id1 and id2 collide. else 0;
if collision occured, the collisionData will filled with the collision data and the count is the the collision point number.
*/
int dCollideCylinders(int id1,int id2, pCollision collisionData,int &count);
/* return 1 if cylinder id collide with ground. else 0;
if collision occured, the collisionData will filled with the collision data and the count is the the collision point number.
*/
int dCollideCylinderPlane(int id, pCollision collisionData);
/* return 1 if sphere id collide with ground. else 0;
if collision occured, the collisionData will filled with the collision data and the count is the the collision point number.
*/
int dCollideSpherePlane(int id, pCollision collisionData);
/* return 1 if cylinder id collide with top ceiling. else 0;
if collision occured, the collisionData will filled with the collision data and the count is the the collision point number.
*/
int dCollideCylinderTopBlock(int id, pCollision collisionData,int &count);
/* return 1 if cylinder id collide with left and right bound. else 0;
if collision occured, the collisionData will filled with the collision data and the count is the the collision point number.
*/
int dCollideCylinderLRBlock(int id, pCollision collisionData,int &count);
/* return 1 if cylinder id collide with other bound. else 0;
if collision occured, the collisionData will filled with the collision data and the count is the the collision point number.
*/
int dCollideCylinderBlock(int id, pCollision collisionData,int &count);
/*
resolve the all the collision data which is filled by the checkForCollisions(). update velocity and vAngularVelocity of the collided object.
*/
void ResolveCollisions(void);
/*
limit all the object(Bodies: tenpins and balls) in the collision box.
*/
void limitation(int i);
void noSink();
boolean isVelAndAngIn(int bodyID, int lmtVel, int lmtAng);
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -