プログラムを書いてみたのですが、判定がとれたり取れなかったりとバグがあるようで原因がわかりません。
どちらも回転する可能性があるので、線分交差を使い実装しております。
バグがありそうな箇所お分かりになる方おりませんでしょうか。
HitCheckLine・・・線分同士の当たり反転 (a1 a2)、(b1 b2)それぞれを結ぶ線があたっているか。
HitCheckLineBox・・・直線の座標と四角形の4点の位置座標を引数に設定。一箇所でも当たっていればtrue
HitCheckLineBox・・・HitCheckLineBoxを使いやすくした関数
class POS{
public:
float x,y;
void Init(){ this->x = 0.0f; this->y = 0.0f; }
void Init( float pram ){ this->x = pram; this->y = pram; }
void Init( float x, float y ){ this->x = x; this->y = y; }
};
// 線分交差
bool GAME::HitCheckLine( POS a1, POS a2, POS b1, POS b2 ){
float tc=(a1.x-a2.x)*(b1.y-a1.y)+(a1.y-a2.y)*(a1.x-b1.x);
float td=(a1.x-a2.x)*(b2.y-a1.y)+(a1.y-a2.y)*(a1.x-b2.x);
// tcとtdの符号が違ったら
if( tc*td<0 ) return true;
return false;
}
// 線と短形の当たり判定
bool GAME::HitCheckLineBox( POS l1, POS l2, POS bLeftUp, POS bRightUp, POS bLeftDown, POS bRightDown ){
// 各辺ごとの線分交差
if( this->HitCheckLine( l1, l2, bLeftUp, bRightUp ) ) return true;
if( this->HitCheckLine( l1, l2, bRightUp, bRightDown ) ) return true;
if( this->HitCheckLine( l1, l2, bLeftDown, bRightDown ) ) return true;
if( this->HitCheckLine( l1, l2, bLeftUp, bLeftDown ) ) return true;
return false;
}
// 線と短形の当たり判定
bool GAME::HitCheckLineBox( POS l1, POS l2, POS bCenter, int width, int height ){
// 各辺ごとの線分交差
POS bLeftUp, bRightUp, bLeftDown, bRightDown;
bLeftUp.x = bCenter.x - width/2;
bLeftUp.y = bCenter.y - height/2;
bRightUp.x = bCenter.x + width/2;
bRightUp.y = bCenter.y - height/2;
bLeftDown.x = bCenter.x - width/2;
bLeftDown.y = bCenter.y + height/2;
bRightDown.x = bCenter.x + width/2;
bRightDown.y = bCenter.y + height/2;
if( this->HitCheckLineBox( l1, l2, bLeftUp, bRightUp, bLeftDown, bRightDown ) ) return true;
return false;
}