JavaScript로 게임 엔진에서 작업 중이며 현재 충돌 시스템에서 작업 중입니다. 플레이어와 감지 된 충돌을 해결하는 알고리즘을 만들었지 만 의도 한 동작을 얻지는 못했습니다. 다음은 코드입니다.AABB 충돌 감지/해결 코드에 어떤 문제가 있습니까?
if (gameObjects [i].hasComponent ("BoxCollider") == true) {
for (var x = 0; x < gameObjects.length; x++) {
if (gameObjects [x].hasComponent ("BoxCollider") == true && gameObjects [i] != gameObjects [x]) {
if (gameObjects [i].transform.xPos - (gameObjects [i].getComponent ("BoxCollider").width/2) < gameObjects [x].transform.xPos + (gameObjects [x].getComponent ("BoxCollider").width/2) && gameObjects [i].transform.xPos + (gameObjects [i].getComponent ("BoxCollider").width/2) > gameObjects [x].transform.xPos - (gameObjects [x].getComponent ("BoxCollider").width/2) && gameObjects [i].transform.yPos - (gameObjects [i].getComponent ("BoxCollider").height/2) < gameObjects [x].transform.yPos + (gameObjects [x].getComponent ("BoxCollider").height/2) && gameObjects [i].transform.yPos + (gameObjects [i].getComponent ("BoxCollider").height/2) > gameObjects [x].transform.yPos - (gameObjects [x].getComponent ("BoxCollider").height/2)) {
//collision
if (gameObjects [i] == player) {
var xPenetration = 0;
var yPenetration = 0;
//i is left of x
if (gameObjects [i].transform.xPos < gameObjects [x].transform.xPos) {
xPenetration = -1 * ((/*i right edge*/gameObjects [i].transform.xPos + gameObjects [i].boxCollider.width/2) - (/*x left edge*/gameObjects [x].transform.xPos - gameObjects [x].boxCollider.width/2));
}
//i is right of x
else if (gameObjects [i].transform.xPos > gameObjects [x].transform.xPos) {
xPenetration = ((/*x right edge*/gameObjects [x].transform.xPos + gameObjects [x].boxCollider.width/2) - (/*i left edge*/gameObjects [i].transform.xPos - gameObjects [i].boxCollider.width/2));
}
//i is top of x
if (gameObjects [i].transform.yPos < gameObjects [x].transform.yPos) {
yPenetration = -1 * ((/*i bottom edge*/gameObjects [i].transform.yPos + gameObjects [i].boxCollider.height/2) - (/*x top edge*/gameObjects [x].transform.yPos - gameObjects [x].boxCollider.height/2));
}
//i is bottom of x
else if (gameObjects [i].transform.yPos > gameObjects [x].transform.yPos) {
yPenetration = ((/*x bottom edge*/gameObjects [x].transform.yPos + gameObjects [x].boxCollider.height/2) - (/*i top edge*/gameObjects [i].transform.yPos - gameObjects [i].boxCollider.height/2));
}
if (Math.abs (xPenetration) > Math.abs (yPenetration)) {
gameObjects [i].transform.xPos += xPenetration;
} else {
gameObjects [i].transform.yPos += yPenetration;
}
}
}
}
}
}
}
"하지만 의도 한 동작을 얻지는 못했습니다." 문제가 무엇인지 이해하는 데 도움이되지 않습니다. 실행 가능한 예제가 도움이 될 수 있습니다. – InferOn
'내 [...] 코드에 문제가 있습니까? '코드에 목적/지정된 동작을 나타내는 설명이 없습니다. 코드는 SE의 코드 엿보기에서 특히 읽기가 어렵습니다. 테스트'gameObjects [i] == player'는 변하지 않습니다. 루프 밖으로 이동하십시오. 들여 쓰기 수준 줄이기 - 리팩토링이 부족하고,'continue' 문을 사용하십시오. 소스 코드 수준에서'(gameObjects [i] .getComponent ("BoxCollider") .width/2)와 같은 공통 하위 표현식을 "제거"합니다. 디버깅 지원을 요청할 때 필수적이고 관찰 된 동작을 언급하지 않으면 질문을 닫을 수 있습니다. – greybeard
무슨 일입니까? 충돌을 전혀 감지하지 못합니까? 가양 성 충돌을 감지합니까? 그것을 감지하고 있지만 잘못 응답하고 있습니까? – samgak