Programming/수학&물리

원과 벽의 충돌체크(정반사 포함)

군자동꽃미남 2015. 12. 10. 00:36
<충돌 처리>
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
// 윗면 체크
            y                 = circle.getY();
            dist             = (float)Math.sqrt(y*y);
            if (dist < circle.getR())
            {
                circle.setY(circle.getR());
                circle.setAY(circle.getAY() * -1);
            }
            
            // 아랫면 체크
            y                = circle.getY() - cGameManager.getInstance().getGameView().getHeight();
            dist            = (float)Math.sqrt(y*y);
            if (dist < circle.getR())
            {
                circle.setY(cGameManager.getInstance().getGameView().getHeight()-circle.getR());
                circle.setAY(circle.getAY() * -1);
            }
            
            // 왼쪽면 체크
            x                = circle.getX();
            dist            = (float)Math.sqrt(x*x);
            if (dist < circle.getR())
            {
                circle.setX(circle.getR());
                circle.setAX(circle.getAX() * -1);
            }
            
            // 오른면 체크
            x                = circle.getX() - cGameManager.getInstance().getGameView().getWidth();
            dist            = (float)Math.sqrt(x*x);
            if (dist < circle.getR())
            {
                circle.setX(cGameManager.getInstance().getGameView().getWidth() - circle.getR());
                circle.setAX(circle.getAX() * -1);
            }
cs


  • 윗변과 밑변은 y축을 가지고 체크를 해주면 되기 때문에, x값은 0으로 배제를 한 상태
    • 위쪽 변을 체크할 때.
    • 밑변을 체크할 때


  • 왼쪽변과 오른쪽변을 체크할때는 마찬가지로 y축 값은 배제하고 x축을 가지고만 충돌 체크를 해준다.
    • 왼쪽 변을 체크할 때.
    • 오른쪽변을 체크할 때

<정반사>


  • 정반사는 입사각과 반사각의 크기가 같은것을 말한다.
  • 따라서, x축 변화량과 y축 변화량인 mAX, mAY를 역으로 바꿔주면 된다.
  • 위의 코드에서는 setAX와 setAX를 통해 mAX와 mAY를 역으로 바꿔준다.