1

数日前に 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;
}

ここでの質問で見つけたもので、このような状況でうまくいくようです。しかし、この状況には小さな問題があります

それを修正する方法はありますか?私はそれを間違っていますか?別のアプローチを試す必要がありますか?どんなヒントでも役に立ちます。

4

3 に答える 3

1

API 関数を見てみましょう。

于 2013-03-21T14:56:20.153 に答える
1

ある長方形の角が他の長方形の内側にあるかどうかを明確にチェックするのは悪い考えです:

交差する長方形

代わりに、チェックを行う簡単な方法は次のとおりです。

if (a.left >= b.right || a.right <= b.left ||
    a.top >= b.bottom || a.bottom <= b.top) {

   // No intersection

} else {

   // Intersection

}
于 2013-03-21T15:04:55.840 に答える
0

この解決策は無効です。2 つの長方形を交差させても、それらのいずれかの頂点が他の長方形に横たわることはありません。たとえば、((0,0), (10,10))((5,-5), (7, 15)). 一方の長方形の辺がもう一方の長方形と交差しているかどうかを確認してみてください。

于 2013-03-21T14:57:47.283 に答える