Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hi im making a platformer in javascript and this is the collision function that I have

if(obj2.y > obj1.y + obj1.height ||
   obj2.y + obj2.height < obj1.y||
   obj2.x > obj1.x + obj1.width ||
   obj2.x + obj2.width < obj1.x){
    return false;
} else {
    return true;
}

so my question is how do I tell which side obj1 is colliding with obj2?.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

In your collision function, instead of returning a boolean, return a string so that you can detect each face:

function colCheck(args) {
    if(obj2.y > obj1.y + obj1.height) {
        return "u"
    }
    if(obj2.y + obj2.height < obj1.y) {
        return "d"
    }
    if(obj2.x > obj1.x + obj1.width) {
        return "r"
    }
    if(obj2.x + obj2.width < obj1.x) {
        return "l"
    } else {
        return false;
    }
}

Depending on the collision, the function will return u,d,l, or r, which you can use however you like

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.