Jack Davis
Jack Davis

Reputation: 81

JavaScript Collision Function

I'm making my first game in HTML5 through a tutorial. The tutorial ended abruptly, but I decided I would finish up the game with things like an end screen and ways to be killed by enemies. The problem, however, is that no matter what I read, I can't seem to figure out this collision function for checking if two rectangles have collided. At the moment, with the current collision function here. You can find the specific function itself here, and the entire program(just in case even though I know you probably won't look at that) here. I've been reading around like I'm suppose to before I post, but I can't seem to make the collision work just right. Am I doing something tottally wrong or is my logic just off. If anyone could help, I would really appreciate it. Thanks!

Ok, sorry about the error, I put in that other function logic as what's below me, but I'm pretty sure it's the same. I was just getting an error before.

(!(x1 + w1 < x2) &&
           !(x2 + w2 < x1) &&
           !(y1 + h1 < y2) &&
           !(y2 + h2 < y1))

Upvotes: 2

Views: 871

Answers (1)

Mathematica
Mathematica

Reputation: 41

Maybe can this help :)

function collision(a,b)
{
    if(a.x < b.x + b.sprite.width && a.x + a.sprite.width > b.x && a.y < b.y + b.sprite.height && a.y + a.sprite.height > b.y)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Upvotes: 3

Related Questions