📄 game_physics.cpp
字号:
#include "stdafx.h"
#include "Game.h"
#include "MyTimer.h"
#include <math.h>
extern CPoolGame theGame;
extern char hint[];
extern char congrats[];
const double b_radius=0.02875;
const double expense_ball=0.7;
const double expense_bd=0.7;
double real_time_step;
UINT physics_loop(void *p)
{
CMyTimer timer_physics;
CMyTimer wait;
double h;
const double step=0.01;
timer_physics.Resume();
while(true)
{
if ( (h=timer_physics.GetElapsedTime()) < step )
{
// MSG msg;
// GetMessage(&msg,NULL,0,0);
// TranslateMessage(&msg);
// DispatchMessage(&msg);
// if( WAIT_FAILED == WaitForSingleObject(NULL, 100) )
// {
// continue;
// }
Sleep(10);
continue;
}
real_time_step=h;
timer_physics.Update();
theGame.Collision(h);
theGame.Move(h);
int n=theGame.num_balls;
bool all_stop=true;
for (int i=0; i<n; ++i)
{
if (theGame.balls[i].status==BS_MOVING
|| theGame.balls[i].status==BS_IN_THE_HOLE)
{
all_stop=false;
break;
}
}
if (all_stop)
break;
}
////////////////////////////////////
CPlayer *theplayer;
if(theGame.player1.m_bTurn==true)theplayer = &theGame.player1;
if(theGame.player2.m_bTurn==true)theplayer = &theGame.player2;
if(theGame.first==true){
if(theGame.bdhitnum<2 && (theGame.numin==0 || theGame.numin==1 && theGame.balls[0].status==BS_REMOVED)){
wait.Resume();
while(wait.GetElapsedTime() < 1.0)continue;
wait.Update();
theGame.restart=true;
sprintf(hint,"Illegal break! Four balls must hit cushion");
theGame.normally=true;
}
else {
theGame.first=false;
}
}
if(theGame.restart)theGame.reset();
//theGame.choose : true
else if(theGame.choose==true){
if(theGame.balls[8].status==BS_REMOVED){
bool allin=true;
for(int k=theplayer->m_ballHitMin;k<=theplayer->m_ballHitMax;k++){
if(theGame.balls[k].status==BS_STATIONARY)
allin=false;
}
if(allin && theGame.numin==1)theGame.win=true;
else {
theGame.loss=true;
sprintf(hint,"foul! Eight ball potted! %s has lost the frame",theplayer->m_Name);
}
}
else if(theGame.ballhit==0){
theGame.foul=true;
sprintf(hint,"No ball hit! %s has free shot",theplayer->m_pOpponent->m_Name);
}
else if(theGame.ballhit==8){
bool allin=true;
for(int k=theplayer->m_ballHitMin;k<=theplayer->m_ballHitMax;k++){
if(theGame.balls[k].status==BS_STATIONARY)
allin=false;
}
if(allin && theGame.balls[0].status==BS_STATIONARY)theGame.normally=true;
else {
theGame.foul=true;
sprintf(hint,"foul! Eight ball hit first! %s has free shot",theplayer->m_pOpponent->m_Name);
}
}
else if(theGame.ballhit <= theplayer->m_pOpponent->m_ballHitMax && theGame.ballhit >= theplayer->m_pOpponent->m_ballHitMin){
theGame.foul=true;
sprintf(hint,"foul!m_pOpponent's ball hit first! %s has free shot",theplayer->m_pOpponent->m_Name);
}
else if(theGame.balls[0].status==BS_REMOVED){
theGame.foul=true;
sprintf(hint,"foul! Cue ball potted! %s has free shot",theplayer->m_pOpponent->m_Name);
}
else if(theGame.numin>0){
for(int j=0;j<theGame.numin;j++){
if(theGame.ballin[j] <= theplayer->m_pOpponent->m_ballHitMax
&& theGame.ballin[j] >= theplayer->m_pOpponent->m_ballHitMin){
theGame.foul=true;
sprintf(hint,"foul!m_pOpponent's ball potted! %s has free shot",theplayer->m_pOpponent->m_Name);
}
}
if(theGame.foul!=true)theGame.pot=true;
}
else theGame.normally=true;
}
//theGame.choose==false
else{
if(theGame.balls[8].status==BS_REMOVED){
theGame.loss=true;
sprintf(hint,"foul! Eight ball potted! %s has lost the frame",theplayer->m_Name);
}
else if(theGame.ballhit==0){
theGame.foul=true;
sprintf(hint,"No ball hit! %s has free shot",theplayer->m_pOpponent->m_Name);
}
else if(theGame.ballhit==8){
theGame.foul=true;
sprintf(hint,"foul! Eight ball hit first! %s has free shot",theplayer->m_pOpponent->m_Name);
}
else if((theGame.numin==1 && theGame.ballin[0]==0)){
theGame.foul=true;
sprintf(hint,"foul! Cue ball theGame.potted! %s has free shot",theplayer->m_pOpponent->m_Name);
}
else if(theGame.numin>0){
int tmp=theGame.ballin[0];
if(theGame.ballin[0]==0)tmp=theGame.ballin[1];
if(tmp<8){
theplayer->m_ballHitMin=1;theplayer->m_ballHitMax=7;
theplayer->m_pOpponent->m_ballHitMin=9;theplayer->m_pOpponent->m_ballHitMax=15;
if(theplayer==&theGame.player1)theGame.player1red=true;
}
else{
theplayer->m_ballHitMin=9;theplayer->m_ballHitMax=15;
theplayer->m_pOpponent->m_ballHitMin=1;theplayer->m_pOpponent->m_ballHitMax=7;
if(theplayer==&theGame.player2)theGame.player1red=true;
}
if(theGame.balls[0].status==BS_STATIONARY)theGame.pot=true;
else {
theGame.foul=true;
sprintf(hint,"foul! Cue ball potted! %s has free shot",theplayer->m_pOpponent->m_Name);
}
theGame.choose=true;
}
else theGame.normally=true;
}
//rule of game
if(theGame.player1.m_bTurn==true && theGame.rule==false){
if(theGame.pot==true){
sprintf(hint,"");
}
if(theGame.foul==true){
theGame.rule=true;
theGame.player1.m_bTurn=false;
theGame.player2.m_bTurn=true;
}
if(theGame.normally==true){
theGame.player1.m_bTurn=false;
theGame.player2.m_bTurn=true;
if(!theGame.restart)sprintf(hint,"");
}
if(theGame.win==true){
sprintf(congrats,"player1 has won the frame!");
}
if(theGame.loss==true){
sprintf(congrats,"player2 has won the frame!");
}
}
else if(theGame.player1.m_bTurn==true && theGame.rule==true){
if(theGame.pot==true){
theGame.rule=false;
sprintf(hint,"");
}
if(theGame.foul==true){
theGame.player1.m_bTurn=false;
theGame.player2.m_bTurn=true;
}
if(theGame.normally==true){
theGame.rule=false;
sprintf(hint,"");
}
if(theGame.win==true){
sprintf(congrats,"player1 has won the frame!");
}
if(theGame.loss==true){
sprintf(congrats,"player2 has won the frame!");
}
}
else if(theGame.player2.m_bTurn==true && theGame.rule==false){
if(theGame.pot==true){
sprintf(hint,"");
}
if(theGame.foul==true){
theGame.rule=true;
theGame.player1.m_bTurn=true;
theGame.player2.m_bTurn=false;
}
if(theGame.normally==true){
theGame.player1.m_bTurn=true;
theGame.player2.m_bTurn=false;
if(!theGame.restart)sprintf(hint,"");
}
if(theGame.win==true){
sprintf(congrats,"player2 has won the frame!");
}
if(theGame.loss==true){
sprintf(congrats,"player1 has won the frame!");
}
}
else if(theGame.player2.m_bTurn==true && theGame.rule==true){
if(theGame.pot==true){
theGame.rule=false;
sprintf(hint,"");
}
if(theGame.foul==true){
theGame.player1.m_bTurn=true;
theGame.player2.m_bTurn=false;
}
if(theGame.normally==true){
theGame.rule=false;
sprintf(hint,"");
}
if(theGame.win==true){
sprintf(congrats,"player2 has won the frame!");
}
if(theGame.loss==true){
sprintf(congrats,"player1 has won the frame!");
}
}
theGame.ballhit=0;
theGame.numin=0;
theGame.bdhitnum=0;
theGame.pot=false;
theGame.foul=false;
theGame.normally=false;
////////////////////////////////////
if(theGame.restart)theGame.restart=false;
else if(theGame.win || theGame.loss)theGame.status=GS_OVER;
else if (theGame.balls[0].status==BS_REMOVED){
wait.Resume();
while(wait.GetElapsedTime() < 1.0)continue;
wait.Update();
theGame.status=GS_INIT_REPOSITION;
}
else theGame.status=GS_STATIONARY;
return 0;
}
//math function*****************begin
double point_mul(double x1,double y1,double x2,double y2){
return (x1*x2+y1*y2);
}
//点(x0, y0)到线段的距离
double dist(double x0,double y0,double x1,double y1,double x2,double y2){
double x3=((y2-y1)*(y2-y1)*x1+(y2-y1)*(y0-y1)*(x2-x1)+(x2-x1)*(x2-x1)*x0)/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));
double y3=((x2-x1)*(x2-x1)*y1+(x2-x1)*(x0-x1)*(y2-y1)+(y2-y1)*(y2-y1)*y0)/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));
if((x3-x1)*(x3-x2)>0 || (y3-y1)*(y3-y2)>0)return -1;
//double d=((y2-y1)*(x0-x1)-(x2-x1)*(y0-y1))*((y2-y1)*(x0-x1)-(x2-x1)*(y0-y1))/((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));
double d=(x0-x3)*(x0-x3)+(y0-y3)*(y0-y3);
return d;
}
double dist_l(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
double x0=((x1-x2)*(x4-x3)*x1-(y4-y3)*(y2-y1)*x3+(y3-y1)*(y2-y1)*(x4-x3))/((x1-x2)*(x4-x3)-(y4-y3)*(y2-y1));
double y0=((y1-y2)*(y4-y3)*y1-(x4-x3)*(x2-x1)*y3+(x3-x1)*(x2-x1)*(y4-y3))/((x1-x2)*(x4-x3)-(y4-y3)*(y2-y1));
if((x0-x3)*(x0-x4)>0 || (y0-y3)*(y0-y4)>0)return -1;
double d=(x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
return d;
}
double dist_p(double x1,double y1,double x2,double y2){
return((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
//math function*****************end
bool detect_ball(CBall &b1,CBall &b2, double step){
double x1=b2.x[0]-b1.x[0];
double y1=b2.x[1]-b1.x[1];
double dvx=b2.v[0]-b1.v[0];
double dvy=b2.v[1]-b1.v[1];
if(dvx*x1+dvy*y1>0)return false;
double x2=x1+dvx*step;
double y2=y1+dvy*step;
double t=dist(0,0,x1,y1,x2,y2);
if(t>0 ){
if(t<4*b_radius*b_radius)return true;
}
else{
t=dist_p(0,0,x2,y2);
if(t<4*b_radius*b_radius)return true;
}
return false;
}
bool detect_hole(CBall &b0,CHole &h0,double step){
double x0=b0.x[0]-h0.x[0];
double y0=b0.x[1]-h0.x[1];
if(b0.v[0]*x0+b0.v[1]*y0>0)return false;
double x1=b0.x[0];
double y1=b0.x[1];
double x2=x1+b0.v[0]*step;
double y2=y1+b0.v[1]*step;
double t=dist(h0.x[0],h0.x[1],x1,y1,x2,y2);
if(t>0){
if(t<h0.r*h0.r)return true;
}
else{
t=dist_p(h0.x[0],h0.x[1],x2,y2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -