//------------------------------------------------------------- //check collision of an dynamic object with another object //return CollisionDirection of 2nd object //------------------------------------------------------------- eCollisionDirection PlayMap::CheckCollision(GameObject *targetObj, GameObject *unknownObj, float &offsetX, float &offsetY) { float normalX, normalY; float moveX, moveY; Box dynamicBox = targetObj->GetBoundaryBox(); Box unknownBox = unknownObj->GetBoundaryBox(); //neu chua va cham if (AABB(dynamicBox, unknownBox, moveX, moveY, offsetX, offsetY) == false) { if (targetObj->IsDynamic()) { DynamicGameObject* dynamicObj = (DynamicGameObject*)targetObj; //neu unknownObj co van toc thi tru van toc 2 vat if (unknownObj->IsDynamic()) { dynamicBox.fVx -= unknownBox.fVx; dynamicBox.fVy -= unknownBox.fVy; unknownBox.fVx = 0.0f; unknownBox.fVy = 0.0f; } //lay vung khong gian cua vat 1 Box broadphasebox = getSweptBroadphaseBox(dynamicBox); //neu vat 2 nam trong vung khong gian cua vat 1 if (AABB(broadphasebox, unknownBox, moveX, moveY, offsetX, offsetY) == true) { //su dung thuat toan sweptAABB de xac dinh va cham float collisiontime = sweptAABB(dynamicBox, unknownBox, normalX, normalY); //truong hop co xay ra va cham if (collisiontime > 0.0f && collisiontime < 1.0f) { //normalY != 0.0f va moveY != 0 tuc va cham theo phuong doc if (normalY != 0.0f && moveY != 0) { //va cham top, truong hop moveY <= 0 thi khong tinh va cham if (normalY == 1.0f && moveY > 0) { return eCollisionDirection::eTop; } //va cham bot, truong hop moveY >= 0 thi khong tinh va cham else if (normalY == -1.0f && moveY < 0) { return eCollisionDirection::eBottom; } } //normalX != 0.0f va moveX != 0 tuc va cham theo phuong ngang else if (normalX != 0.0f && moveX != 0.0f) { //va cham right, truong hop moveX >= 0 thi khong tinh va cham if (normalX == 1.0f && moveX < 0) { return eCollisionDirection::eRight; } //va cham left, truong hop moveX <= 0 thi khong tinh va cham else if (normalX == -1.0f && moveX > 0) { return eCollisionDirection::eLeft; } } } } } } //neu da va cham else { //va cham theo phuong doc if (moveY != 0) { //top if (moveY > 0.0f) { return eCollisionDirection::eTop; } //bot else { return eCollisionDirection::eBottom; } } //va cham theo phuong ngang else if (moveX != 0) { //left if (moveX < 0.0f) { return eCollisionDirection::eLeft; } //right else { return eCollisionDirection::eRight; } } //moveX == 0 va move Y == 0 tuc 2 vat dang tiep xuc else { //truong hop goc cham goc thi khong xac dinh duoc huong va cham //top left dynamicbox collide with bot right unknownbox if ((dynamicBox.fY == unknownBox.fY - unknownBox.fHeight && dynamicBox.fX == unknownBox.fX + unknownBox.fWidth) //top right dynamicbox collide with bot left unknownbox || (dynamicBox.fY == unknownBox.fY - unknownBox.fHeight && dynamicBox.fX + dynamicBox.fWidth == unknownBox.fX) //bot right dynamicbox collide with top left unknowbox || (dynamicBox.fY - dynamicBox.fHeight == unknownBox.fY && dynamicBox.fX + dynamicBox.fWidth == unknownBox.fX) //bot left dynamicbox collide with top right unknowbox || (dynamicBox.fY - dynamicBox.fHeight == unknownBox.fY && dynamicBox.fX == unknownBox.fX + unknownBox.fWidth) ) return eCollisionDirection::eNone; //tiep xuc voi mat tren cua vat unknown if (dynamicBox.fY - dynamicBox.fHeight == unknownBox.fY) { return eCollisionDirection::eTop; } //tiep xuc o mat duoi cua vat unknown else if (dynamicBox.fY == unknownBox.fY - unknownBox.fHeight) { return eCollisionDirection::eBottom; } //tiep xuc o mat trai cua vat unknown else if (dynamicBox.fX + dynamicBox.fWidth == unknownBox.fX) { return eCollisionDirection::eLeft; } //tiep xuc o mat phai cua vat unknown else if (dynamicBox.fX == unknownBox.fX + unknownBox.fWidth) { return eCollisionDirection::eRight; } } } return eCollisionDirection::eNone; }
int Box::collide(Box box, Box block) { // Box is the moving Box // block is the static Box Box broadphaseBox = getSweptBroadphaseRec(box); if (aabbCheck(broadphaseBox, block)) { float normalx, normaly; float collisiontime = sweptAABB(box, block, normalx, normaly); float remainingtime = 1.0f - collisiontime; if (collisiontime <1.0f) { if (normalx == -1) return 2; //right if (normalx == 1) return 1; //left if (normaly == -1) return 4;//top if (normaly == 1) return 3; //bottom } else if (aabbCheck(box, block)) { return 5; } } return 0; }