2

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))
8
  • 1
    Your (new) collision function looks correct (see stackoverflow.com/questions/6082624/…). Define "work just right". Commented Sep 30, 2012 at 22:56
  • Well, the collsion isn't right. It collides sometimes and doesn't stop. I'll put it into the link for the game and you can see what I mean.I'm assuming you mean the code I posted in this post and not the paste bin part. Commented Sep 30, 2012 at 22:58
  • Yes, I meant the (original) shorter function - it only takes four comparisons to perform a rectangle intersection test. Commented Sep 30, 2012 at 23:00
  • Your javascript code won't run because of an error... Commented Sep 30, 2012 at 23:04
  • Ok, I'm pretty sure that if you use the game now it'll be using that function. The problem is it doesn't seem to work right now. Commented Sep 30, 2012 at 23:04

1 Answer 1

3

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.