Notice
Recent Posts
Recent Comments
Link
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
관리 메뉴

codingfarm

충돌 판정 본문

Game

충돌 판정

scarecrow1992 2021. 4. 12. 22:58

2D 사각형 충돌 판정

사각형의 collider에 대한 2D의 충돌판정은 아래와 같다.

캐릭터의 collider가 (CL, CT)~(CR,CB), 적의 collider 가 (EL, ET) ~ (ER, EB)라 할때

두 캐릭터가 접촉하는 조건은 아래와 같다

$$CL \leq ER \; \&\& \; EL \leq CR \; \&\& \; CT \leq EB \; \&\& \; ET \leq CB$$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <windows.h>
 
using namespace std;
 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInst;
LPCTSTR lpszClass = TEXT("Mouse");
 
bool isBegin = true;
 
int EL, ER, ET, EB;
 
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance
    , LPSTR lpszCmdParam, int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    WNDCLASS WndClass;
    g_hInst = hInstance;
 
    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClass.hInstance = hInstance;
    WndClass.lpfnWndProc = (WNDPROC)WndProc;
    WndClass.lpszClassName = lpszClass;
    WndClass.lpszMenuName = NULL;
    WndClass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
    RegisterClass(&WndClass);
 
    hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, (HMENU)NULL, hInstance, NULL);
    ShowWindow(hWnd, nCmdShow);
 
    while (GetMessage(&Message, 000)) {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
    return Message.wParam;
}
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    static int x;
    static int y;
    static BOOL bnowDraw = FALSE;
    static TCHAR str[128];
    static int CL = 400, CR = 800, CT = 400, CB = 800;
    static RECT rt = { 006050 };
    static HBRUSH brush, obrush;
 
    switch (iMessage) {
    case WM_LBUTTONDOWN:
        hdc = GetDC(hWnd);
 
        if (isBegin) {
            brush = CreateSolidBrush(RGB(02550));
            obrush = (HBRUSH)SelectObject(hdc, brush);
 
            EL = 300, ET = 200, ER = 600, EB = 500;
            Rectangle(hdc, EL, ET, ER, EB);
 
            ReleaseDC(hWnd, hdc);
 
            isBegin = false;
        }
        else {
            x = LOWORD(lParam);
            y = HIWORD(lParam);
 
            int CL, CR, CT, CB;
            CL = x - 50;
            CT = y - 50;
            CR = x + 50;
            CB = y + 50;
            
            if( CL <= ER && EL <= CR && CT <= EB && ET <= CB )
                brush = CreateSolidBrush(RGB(25500));
            else
                brush = CreateSolidBrush(RGB(00255));
 
            obrush = (HBRUSH)SelectObject(hdc, brush);
 
            Rectangle(hdc, CL, CT, CR, CB);
 
            InvalidateRect(hWnd, &rt, TRUE);
        }
        ReleaseDC(hWnd, hdc);
        return 0;
 
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        
        SetTextAlign(hdc, TA_LEFT);        
 
        wsprintf(str, TEXT("x : %d"), x);
        TextOut(hdc, 100, str, lstrlen(str));
 
        wsprintf(str, TEXT("y : %d"), y);
        TextOut(hdc, 1020, str, lstrlen(str));
        EndPaint(hWnd, &ps);
 
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
 
        return 0;
    }
    return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
 
cs

첫번째 좌클릭시 중앙에 초록색의 커다란 사각형이 만들어지고, 그 이후 클릭할때마다 사각형이 만들어진다.

이 사각형은 초록 사각형과 접촉하면 빨간색으로 나오며, 그렇지 않으면 파란색으로 나온다.

 

 

 

 

 

Comments