충돌판정이란?


간단히 말해서 물체와 물체사이가 겹치나 안겹치냐를 말합니다

이걸 수학적으로 표현할수 있죠. 넹.. 그렇습니다.


충돌을 판정하는 기준은 굉장히 다양합니다. 왜냐하면 충돌판정에 따라서 기기의 지연을 일으킬 수도 있습니다

그만큼 자세하게 할경우에는 하드웨어가 성능을 따라가지 못하고 너무 대충하자니 피격판정이 애매하므로

이미지를 따로 구분한다음에 판정을 하는 방법등 googling을 하시면 많이 나오니깐 걱정하지않으셔도 됩니다


제가 이번에 사용할 건 원형 충돌 판정입니다.


물체의 걸리를 제어 원안에 포함되면 충돌하고 그렇지 않으면 충돌하지 않은 판정입니다.


boolean hit_check() {
if ((obs.position_x - player.getX()) * (obs.position_x - player.getX()) < obs.resized_width / 3 * obs.resized_width / 3) {
if ((obs.position_y - player.getY()) * (obs.position_y - player.getY()) < obs.resized_height / 3 * obs.resized_height / 3) {
return true;
}
}

if ((obs_1.position_x - player.getX()) * (obs_1.position_x - player.getX()) < obs_1.resized_width / 3 * obs_1.resized_width / 3) {
if ((obs_1.position_y - player.getY()) * (obs_1.position_y - player.getY()) < obs_1.resized_height / 3 * obs_1.resized_height / 3) {
return true;
}
}

if ((obs_2.position_x - player.getX()) * (obs_2.position_x - player.getX()) < obs_2.resized_width / 3 * obs_2.resized_width / 3) {
if ((obs_2.position_y - player.getY()) * (obs_2.position_y - player.getY()) < obs_2.resized_height / 3 * obs_2.resized_height / 3) {
return true;
}
}

if ((obs_3.position_x - player.getX()) * (obs_3.position_x - player.getX()) < obs_3.resized_width / 3 * obs_3.resized_width / 3) {
if ((obs_3.position_y - player.getY()) * (obs_3.position_y - player.getY()) < obs_3.resized_height / 3 * obs_3.resized_height / 3) {
return true;
}
}

if ((obs_4.position_x - player.getX()) * (obs_4.position_x - player.getX()) < obs_4.resized_width / 3 * obs_4.resized_width / 3) {
if ((obs_4.position_y - player.getY()) * (obs_4.position_y - player.getY()) < obs_4.resized_height / 3 * obs_4.resized_height / 3) {
return true;
}
}

if ((obs_5.position_x - player.getX()) * (obs_5.position_x - player.getX()) < obs_5.resized_width / 3 * obs_5.resized_width / 3) {
if ((obs_5.position_y - player.getY()) * (obs_5.position_y - player.getY()) < obs_5.resized_height / 3 * obs_5.resized_height / 3) {
return true;
}
}

if ((obs_6.position_x - player.getX()) * (obs_6.position_x - player.getX()) < obs_6.resized_width / 3 * obs_6.resized_width / 3) {
if ((obs_6.position_y - player.getY()) * (obs_6.position_y - player.getY()) < obs_6.resized_height / 3 * obs_6.resized_height / 3) {
return true;
}
}

if ((obs_7.position_x - player.getX()) * (obs_7.position_x - player.getX()) < obs_7.resized_width / 3 * obs_7.resized_width / 3) {
if ((obs_7.position_y - player.getY()) * (obs_7.position_y - player.getY()) < obs_7.resized_height / 3 * obs_7.resized_height / 3) {
return true;
}
}

if ((obsx2.position_x - player.getX()) * (obsx2.position_x - player.getX()) < obsx2.resized_width / 3 * obsx2.resized_width / 3) {
if ((obsx2_1.position_y - player.getY()) * (obsx2_1.position_y - player.getY()) < obsx2_1.resized_height / 3 * obsx2_1.resized_height / 3) {
return true;
}
}

if ((obsx2_1.position_x - player.getX()) * (obsx2_1.position_x - player.getX()) < obsx2_1.resized_width / 3 * obsx2_1.resized_width / 3) {
if ((obsx2_1.position_y - player.getY()) * (obsx2_1.position_y - player.getY()) < obsx2_1.resized_height / 3 * obsx2_1.resized_height / 3) {
return true;
}
}

if ((obsx2_2.position_x - player.getX()) * (obsx2_2.position_x - player.getX()) < obsx2_2.resized_width / 3 * obsx2_2.resized_width / 3) {
if ((obsx2_2.position_y - player.getY()) * (obsx2_2.position_y - player.getY()) < obsx2_2.resized_height / 3 * obsx2_2.resized_height / 3) {
return true;
}
}

if ((obsx2_3.position_x - player.getX()) * (obsx2_3.position_x - player.getX()) < obsx2_3.resized_width / 3 * obsx2_3.resized_width / 3) {
if ((obsx2_3.position_y - player.getY()) * (obsx2_3.position_y - player.getY()) < obsx2_3.resized_height / 3 * obsx2_3.resized_height / 3) {
return true;
}
}

if ((obsx2_4.position_x - player.getX()) * (obsx2_4.position_x - player.getX()) < obsx2_4.resized_width / 3 * obsx2_4.resized_width / 3) {
if ((obsx2_4.position_y - player.getY()) * (obsx2_4.position_y - player.getY()) < obsx2_4.resized_height / 3 * obsx2_4.resized_height / 3) {
return true;
}
}

if ((obsx2_5.position_x - player.getX()) * (obsx2_5.position_x - player.getX()) < obsx2_5.resized_width / 3 * obsx2_5.resized_width / 3) {
if ((obsx2_5.position_y - player.getY()) * (obsx2_5.position_y - player.getY()) < obsx2_5.resized_height / 3 * obsx2_5.resized_height / 3) {
return true;
}
}

if ((obsx2_6.position_x - player.getX()) * (obsx2_6.position_x - player.getX()) < obsx2_6.resized_width / 3 * obsx2_6.resized_width / 3) {
if ((obsx2_6.position_y - player.getY()) * (obsx2_6.position_y - player.getY()) < obsx2_6.resized_height / 3 * obsx2_6.resized_height / 3) {
return true;
}
}

if ((obsx2_7.position_x - player.getX()) * (obsx2_7.position_x - player.getX()) < obsx2_7.resized_width / 3 * obsx2_7.resized_width / 3) {
if ((obsx2_7.position_y - player.getY()) * (obsx2_7.position_y - player.getY()) < obsx2_7.resized_height / 3 * obsx2_7.resized_height / 3) {
return true;
}
}

if ((obsx3.position_x - player.getX()) * (obsx3.position_x - player.getX()) < obsx3.resized_width / 3 * obsx3.resized_width / 3) {
if ((obsx3.position_y - player.getY()) * (obsx3.position_y - player.getY()) < obsx3.resized_height / 3 * obsx3.resized_height / 3) {

return true;
}
}

if ((obsx3_1.position_x - player.getX()) * (obsx3_1.position_x - player.getX()) < obsx3_1.resized_width / 3 * obsx3_1.resized_width / 3) {
if ((obsx3_1.position_y - player.getY()) * (obsx3_1.position_y - player.getY()) < obsx3_1.resized_height / 3 * obsx3_1.resized_height / 3) {

return true;
}
}

if ((obsx3_2.position_x - player.getX()) * (obsx3_2.position_x - player.getX()) < obsx3_2.resized_width / 3 * obsx3_2.resized_width / 3) {
if ((obsx3_2.position_y - player.getY()) * (obsx3_2.position_y - player.getY()) < obsx3_2.resized_height / 3 * obsx3_2.resized_height / 3) {
return true;
}
}

if ((obsx3_3.position_x - player.getX()) * (obsx3_3.position_x - player.getX()) < obsx3_3.resized_width / 3 * obsx3_3.resized_width / 3) {
if ((obsx3_3.position_y - player.getY()) * (obsx3_3.position_y - player.getY()) < obsx3_3.resized_height / 3 * obsx3_3.resized_height / 3) {
return true;
}
}

if ((obsx3_4.position_x - player.getX()) * (obsx3_4.position_x - player.getX()) < obsx3_4.resized_width / 3 * obsx3_4.resized_width / 3) {
if ((obsx3_4.position_y - player.getY()) * (obsx3_4.position_y - player.getY()) < obsx3_4.resized_height / 3 * obsx3_4.resized_height / 3) {
return true;
}
}

if ((obsx3_5.position_x - player.getX()) * (obsx3_5.position_x - player.getX()) < obsx3_5.resized_width / 3 * obsx3_5.resized_width / 3) {
if ((obsx3_5.position_y - player.getY()) * (obsx3_5.position_y - player.getY()) < obsx3_5.resized_height / 3 * obsx3_5.resized_height / 3) {
return true;
}
}

if ((obsx3_6.position_x - player.getX()) * (obsx3_6.position_x - player.getX()) < obsx3_6.resized_width / 3 * obsx3_6.resized_width / 3) {
if ((obsx3_6.position_y - player.getY()) * (obsx3_6.position_y - player.getY()) < obsx3_6.resized_height / 3 * obsx3_6.resized_height / 3) {
return true;
}
}

if ((obsx3_7.position_x - player.getX()) * (obsx3_7.position_x - player.getX()) < obsx3_7.resized_width / 3 * obsx3_7.resized_width / 3) {
if ((obsx3_7.position_y - player.getY()) * (obsx3_7.position_y - player.getY()) < obsx3_7.resized_height / 3 * obsx3_7.resized_height / 3) {

return true;
}
}


if ((obs1.position_x - player.getX()) * (obs1.position_x - player.getX()) < obs1.resized_width / 3 * obs1.resized_width / 3) {
if ((obs1.position_y - player.getY()) * (obs1.position_y - player.getY()) < obs1.resized_height / 3 * obs1.resized_height / 3) {
return true;
}
}

if ((obs_1_1.position_x - player.getX()) * (obs_1_1.position_x - player.getX()) < obs_1_1.resized_width / 3 * obs_1_1.resized_width / 3) {
if ((obs_1_1.position_y - player.getY()) * (obs_1_1.position_y - player.getY()) < obs_1_1.resized_height / 3 * obs_1_1.resized_height / 3) {
return true;
}
}

if ((obs_2_1.position_x - player.getX()) * (obs_2_1.position_x - player.getX()) < obs_2_1.resized_width / 3 * obs_2_1.resized_width / 3) {
if ((obs_2_1.position_y - player.getY()) * (obs_2_1.position_y - player.getY()) < obs_2_1.resized_height / 3 * obs_2_1.resized_height / 3) {
return true;
}
}

if ((obs_3_1.position_x - player.getX()) * (obs_3_1.position_x - player.getX()) < obs_3_1.resized_width / 3 * obs_3_1.resized_width / 3) {
if ((obs_3_1.position_y - player.getY()) * (obs_3_1.position_y - player.getY()) < obs_3_1.resized_width / 3 * obs_3_1.resized_width / 3) {
return true;
}
}

if ((obs_4_1.position_x - player.getX()) * (obs_4_1.position_x - player.getX()) < obs_4_1.resized_width / 3 * obs_4_1.resized_width / 3) {
if ((obs_4_1.position_y - player.getY()) * (obs_4_1.position_y - player.getY()) < obs_4_1.resized_height / 3 * obs_4_1.resized_height / 3) {
return true;
}
}

if ((obs_5_1.position_x - player.getX()) * (obs_5_1.position_x - player.getX()) < obs_5_1.resized_width / 3 * obs_5_1.resized_width / 3) {
if ((obs_5_1.position_y - player.getY()) * (obs_5_1.position_y - player.getY()) < obs_5_1.resized_height / 3 * obs_5_1.resized_height / 3) {
return true;
}
}

if ((obs_6_1.position_x + obs_6_1.resized_width / 4 - player.getX()) * (obs_6_1.position_x + obs_6_1.resized_width / 4 - player.getX()) < obs_6_1.resized_width / 3 * obs_6_1.resized_width / 3) {
if ((obs_6_1.position_y + obs_6_1.resized_height / 4 - player.getY()) * (obs_6_1.position_y + obs_6_1.resized_height / 4 - player.getY()) < obs_6_1.resized_height / 3 * obs_6_1.resized_height / 3) {
return true;
}
}

if ((obs_7_1.position_x - player.getX()) * (obs_7_1.position_x - player.getX()) < obs_7_1.resized_width / 3 * obs_7_1.resized_width / 3) {
if ((obs_7_1.position_y - player.getY()) * (obs_7_1.position_y - player.getY()) < obs_7_1.resized_height / 3 * obs_7_1.resized_height / 3) {
return true;
}
}

if ((obs1x2.position_x - player.getX()) * (obs1x2.position_x - player.getX()) < obs1x2.resized_width / 3 * obs1x2.resized_width / 3) {
if ((obs1x2.position_y - player.getY()) * (obs1x2.position_y - player.getY()) < obs1x2.resized_height / 3 * obs1x2.resized_height / 3) {
return true;
}
}

if ((obsx2_1_1.position_x - player.getX()) * (obsx2_1_1.position_x - player.getX()) < obsx2_1_1.resized_width / 3 * obsx2_1_1.resized_width / 3) {
if ((obsx2_1_1.position_y - player.getY()) * (obsx2_1_1.position_y - player.getY()) < obsx2_1_1.resized_height / 3 * obsx2_1_1.resized_height / 3) {
return true;
}
}

if ((obsx2_2_1.position_x - player.getX()) * (obsx2_2_1.position_x - player.getX()) < obsx2_2_1.resized_width / 3 * obsx2_2_1.resized_width / 3) {
if ((obsx2_2_1.position_y - player.getY()) * (obsx2_2_1.position_y - player.getY()) < obsx2_2_1.resized_height / 3 * obsx2_2_1.resized_height / 3) {
return true;
}
}

if ((obsx2_3_1.position_x - player.getX()) * (obsx2_3_1.position_x - player.getX()) < obsx2_3_1.resized_width / 3 * obsx2_3_1.resized_width / 3) {
if ((obsx2_3_1.position_y - player.getY()) * (obsx2_3_1.position_y - player.getY()) < obsx2_3_1.resized_height / 3 * obsx2_3_1.resized_height / 3) {
return true;
}
}

if ((obsx2_4_1.position_x + obsx2_4_1.resized_width / 4 - player.getX()) * (obsx2_4_1.position_x - player.getX()) < obsx2_4_1.resized_width / 3 * obsx2_4_1.resized_width / 3) {
if ((obsx2_4_1.position_y + obsx2_4_1.resized_height / 4 - player.getY()) * (obsx2_4_1.position_y - player.getY()) < obsx2_4_1.resized_height / 3 * obsx2_4_1.resized_height / 3) {
return true;
}
}

if ((obsx2_5_1.position_x + obsx2_5_1.resized_width / 4 - player.getX()) * (obsx2_5_1.position_x + obsx2_5_1.resized_width / 4 - player.getX()) < obsx2_5_1.resized_width / 3 * obsx2_5_1.resized_width / 3) {
if ((obsx2_5_1.position_y + obsx2_5_1.resized_height / 4 - player.getY()) * (obsx2_5_1.position_y + obsx2_5_1.resized_height / 4 - player.getY()) < obsx2_5_1.resized_height / 3 * obsx2_5_1.resized_height / 3) {
return true;
}
}

if ((obsx2_6_1.position_x + obsx2_6_1.resized_width / 4 - player.getX()) * (obsx2_6_1.position_x + obsx2_6_1.resized_width / 4 - player.getX()) < obsx2_6_1.resized_width / 3 * obsx2_6_1.resized_width / 3) {
if ((obsx2_6_1.position_y + obsx2_6_1.resized_height / 4 - player.getY()) * (obsx2_6_1.position_y + obsx2_6_1.resized_height / 4 - player.getY()) < obsx2_6_1.resized_height / 3 * obsx2_6_1.resized_height / 3) {
return true;
}
}

if ((obsx2_7_1.position_x + obsx2_7_1.resized_width / 4 - player.getX()) * (obsx2_7_1.position_x + obsx2_7_1.resized_width / 4 - player.getX()) < obsx2_7_1.resized_width / 3 * obsx2_7_1.resized_width / 3) {
if ((obsx2_7_1.position_y + obsx2_7_1.resized_height / 4 - player.getY()) * (obsx2_7_1.position_y + obsx2_7_1.resized_height / 4 - player.getY()) < obsx2_7_1.resized_height / 3 * obsx2_7_1.resized_height / 3) {
return true;
}
}

if ((obs1x3.position_x - player.getX()) * (obs1x3.position_x + obs1x3.resized_width / 4 - player.getX()) < obs1x3.resized_width / 3 * obs1x3.resized_width / 3) {
if ((obs1x3.position_y - player.getY()) * (obs1x3.position_y + obs1x3.resized_height / 4 - player.getY()) < obs1x3.resized_height / 3 * obs1x3.resized_height / 3) {

return true;
}
}

if ((obsx3_1_1.position_x - player.getX()) * (obsx3_1_1.position_x - player.getX()) < obsx3_1_1.resized_width / 3 * obsx3_1_1.resized_width / 3) {
if ((obsx3_1_1.position_y - player.getY()) * (obsx3_1_1.position_y - player.getY()) < obsx3_1_1.resized_height / 3 * obsx3_1_1.resized_height / 3) {
return true;
}
}

if ((obsx3_2_1.position_x - player.getX()) * (obsx3_2_1.position_x - player.getX()) < obsx3_2_1.resized_width / 3 * obsx3_2_1.resized_width / 3) {
if ((obsx3_2_1.position_y - player.getY()) * (obsx3_2_1.position_y - player.getY()) < obsx3_2_1.resized_height / 3 * obsx3_2_1.resized_height / 3) {
return true;
}
}

if ((obsx3_3_1.position_x - player.getX()) * (obsx3_3_1.position_x - player.getX()) < obsx3_3_1.resized_width / 3 * obsx3_3_1.resized_width / 3) {
if ((obsx3_3_1.position_y + -player.getY()) * (obsx3_3_1.position_y - player.getY()) < obsx3_3_1.resized_height / 3 * obsx3_3_1.resized_height / 3) {
return true;
}
}

if ((obsx3_4_1.position_x - player.getX()) * (obsx3_4_1.position_x - player.getX()) < obsx3_4_1.resized_width / 3 * obsx3_4_1.resized_width / 3) {
if ((obsx3_4_1.position_y - player.getY()) * (obsx3_4_1.position_y - player.getY()) < obsx3_4_1.resized_height / 3 * obsx3_4_1.resized_height / 3) {
return true;
}
}

if ((obsx3_5_1.position_x - player.getX()) * (obsx3_5_1.position_x - player.getX()) < obsx3_5_1.resized_width / 3 * obsx3_5_1.resized_width / 3) {
if ((obsx3_5_1.position_y - player.getY()) * (obsx3_5_1.position_y - player.getY()) < obsx3_5_1.resized_height / 3 * obsx3_5_1.resized_height / 3) {
return true;
}
}

if ((obsx3_6_1.position_x - player.getX()) * (obsx3_6_1.position_x - player.getX()) < obsx3_6_1.resized_width / 3 * obsx3_6_1.resized_width / 3) {
if ((obsx3_6_1.position_y - player.getY()) * (obsx3_6_1.position_y - player.getY()) < obsx3_6_1.resized_height / 3 * obsx3_6_1.resized_height / 3) {
return true;
}
}

if ((obsx3_7_1.position_x - player.getX()) * (obsx3_7_1.position_x - player.getX()) < obsx3_7_1.resized_width / 3 * obsx3_7_1.resized_width / 3) {
if ((obsx3_7_1.position_y - player.getY()) * (obsx3_7_1.position_y - player.getY()) < obsx3_7_1.resized_height / 3 * obsx3_7_1.resized_height / 3) {
return true;
}
}
return false;
}

이렇게 되어있습니다. 그리고 boolean을 표현했습니다.

왜냐하면 나중에 피격을 받을시 시간을 조절해서 계속 공격을 받아도 공격이 무효화되는 시간을 주려고 표현한 겁니다.


@Override
public void run()에서
if (stun_player == false) {
Isdamaged_player = hit_check();
health_check(Isdamaged_player);
} else {
++health_timing_number;
if (health_timing_number % 300 == 0) {
stun_player = false;
health_timing_number = 0;
}
}
요렇게 표현하면 300즉, 20ms * 300 = 6000ms = 6초 정도 피격받고 다시 공격을 받지 않습니다.
<pre style="color: rgb(0, 0, 0); font-family: '굴림체'; font-size: 9pt; background-color: rgb(255, 255, 255);">void health_check(boolean x) {
if (x == true) {
++health_number;
stun_player = true;
}

if (health_number == 1) {
heart1.setVisibility(heart1.INVISIBLE);
} else if (health_number == 2) {
heart2.setVisibility(heart2.INVISIBLE);
} else if (health_number == 3) {
heart3.setVisibility(heart3.INVISIBLE);
}

}</pre><pre style="color: rgb(0, 0, 0); font-family: '굴림체'; font-size: 9pt; background-color: rgb(255, 255, 255);">
</pre><pre style="color: rgb(0, 0, 0); font-family: '굴림체'; font-size: 9pt; background-color: rgb(255, 255, 255);">이 함수를 호출해서 xml을 이용해 그렸던 이미지를 안보이게 함으로써 6초간 공격을 받지 않은 것과 동시에</pre><pre style="color: rgb(0, 0, 0); font-family: '굴림체'; font-size: 9pt; background-color: rgb(255, 255, 255);">목숨이 1줄었음을 표현 합니다</pre><pre style="color: rgb(0, 0, 0); font-family: '굴림체'; font-size: 9pt; background-color: rgb(255, 255, 255);">
</pre><pre style="color: rgb(0, 0, 0); font-family: '굴림체'; font-size: 9pt; background-color: rgb(255, 255, 255);">-----------------------------------------------------------------------------------------------------</pre><pre style="color: rgb(0, 0, 0); font-family: '굴림체'; font-size: 9pt; background-color: rgb(255, 255, 255);">여기까지가 충돌판정에 관한 내용이었습니다.</pre><pre style="color: rgb(0, 0, 0); font-family: '굴림체'; font-size: 9pt; background-color: rgb(255, 255, 255);">점점 의식이 흐려집니다. 마지막까지 최선을 다해 글을 써 보겠습니다....</pre>