数日前に Win32 GUI プログラミングを始めたばかりです。2 つのオブジェクト間の衝突を検出する必要がある単純なゲームを作成しようとしています。だから私は構造体を使って自分のキャラクターを作りましたRECT
それらが衝突するかどうかを検出するために、私は使用しました:
// Returns 1 if the point (x, y) lies within the rectangle, 0 otherwise
int is_point_in_rectangle(RECT r, int x, int y) {
if ((r.left <= x && r.right >= x) &&
(r.bottom <= y && r.top >= y))
return 1;
return 0;
}
// Returns 1 if the rectangles overlap, 0 otherwise
int do_rectangles_intersect(RECT a, RECT b) {
if ( is_point_in_rectangle(a, b.left , b.top ) ||
is_point_in_rectangle(a, b.right, b.top ) ||
is_point_in_rectangle(a, b.left , b.bottom) ||
is_point_in_rectangle(a, b.right, b.bottom))
return 1;
if ( is_point_in_rectangle(b, a.left , a.top ) ||
is_point_in_rectangle(b, a.right, a.top ) ||
is_point_in_rectangle(b, a.left , a.bottom) ||
is_point_in_rectangle(b, a.right, a.bottom))
return 1;
return 0;
}
ここでの質問で見つけたもので、このような状況でうまくいくようです。しかし、この状況には小さな問題があります
それを修正する方法はありますか?私はそれを間違っていますか?別のアプローチを試す必要がありますか?どんなヒントでも役に立ちます。