あたり判定の関数
bool HitLineandCircle(BAR barin, BALL ballin){
Vector2 avec, bvec,svec;
avec = SubVector(ballin.pos, barin.startpos); //ベクトルの引き算 ballin.pos - barin.startpos
bvec = SubVector(barin.endpos, barin.startpos);
float dot = DotProduct(avec, bvec); //内積を求める
float bl = VectorLengthSquare(bvec); //ベクトルbの長さの二乗を求める
if( (dot > -ZEROVALUE) && (dot < bl) ){
//円と線の距離を求める
//ボールの中心から垂直におろした線の交点を求める
Vector2 bnvec = Normalize(bvec); //ベクトルsの正規化
float dot2 = DotProduct(avec, bnvec);
bnvec.x *= dot2;
bnvec.y *= dot2;
Point2D kouten = AddVector(barin.startpos, bnvec); //ベクトルの足し算
Vector2 dist = SubVector(ballin.pos, kouten);
float dl = VectorLengthSquare(dist);
if ( dl < (ballin.hankei * ballin.hankei) ){
return TRUE;
}
}
return FALSE;
}
typedef struct BAR{
double x,y; //画像を表示する位置
Point2D center,startpos,endpos; //線分の中心と端
int longth; //バーの長さ
int speed;
}BAR;
//ボール情報
typedef struct BALL{
int x,y; //画像を表示する位置
double dx,dy;
bool shot_flag;
Point2D pos; //円の中心
int state;
float hankei; //半径
}BALL;
void hitbar(){
bar.startpos.x = bar.center.x - bar.longth; bar.startpos.y = bar.center.y;
bar.endpos.x = bar.center.x + bar.longth; bar.endpos.y = bar.center.y;
//バーとボールの接触判定
if(HitLineandCircle(bar,ball) == TRUE){
//if(bar.center.x < ball.x){
//
//}
//if(bar.center.x > ball.x){
//}
ball.dy*= -1;
func_state = STATE_TITLE;
}
}