Javascript canvas collision detection

前端 未结 3 1962
一生所求
一生所求 2020-12-31 21:46

I\'m building a game in Javascript using canvas which requires collision detection, in this case if the player sprite hits a box, the player must not be allowed through the

相关标签:
3条回答
  • 2020-12-31 22:01

    In my opinion, I'm not a fan of functions that require many parameters.

    Here's how I would do it :

    function collisionCheckRectRect(rectOne, rectTwo){
    
        var x1=rectOne.x, y1 = rectOne.y, height1 = rectOne.height, width1 = rectOne.width;
        var x2=rectTwo.x, y2 = rectTwo.y, height2 = rectTwo.height, width2 = rectTwo.width; 
    
        return x1 < x2+width2 && x2 < x1+width1 && y1 < y2+height2 && y2 < y1+height1;
    }
    
    0 讨论(0)
  • 2020-12-31 22:10

    I use the following function for collision detection between two rectangles:

    rect_collision = function(x1, y1, size1, x2, y2, size2) {
      var bottom1, bottom2, left1, left2, right1, right2, top1, top2;
      left1 = x1 - size1;
      right1 = x1 + size1;
      top1 = y1 - size1;
      bottom1 = y1 + size1;
      left2 = x2 - size2;
      right2 = x2 + size2;
      top2 = y2 - size2;
      bottom2 = y2 + size2;
      return !(left1 > right2 || left2 > right1 || top1 > bottom2 || top2 > bottom1);
    };
    

    This determines whether two squares, centered at (x1, y1) and (x2, y2), with side lengths 2*size1 and 2*size2, respectively, are overlapping. It should be easy enough to alter the definitions of left1, right1, etc. to deal with general rectangles rather than just squares and to use a different data format.

    Specifically, left1 is the left side of the the first square, right1 the right side, etc. Note that, in my coordinate system, the y-axis is inverted (top1 < bottom1).

    0 讨论(0)
  • 2020-12-31 22:15

    You just want to know if two rectangles overlap?

    Here is a bulletproof function for you:

    // returns true if there is any overlap
    // params: x,y,w,h of two rectangles
    function intersects(x1, y1, w1, h1, x2, y2, w2, h2) {
      if (w2 !== Infinity && w1 !== Infinity) {
        w2 += x2;
        w1 += x1;
        if (isNaN(w1) || isNaN(w2) || x2 > w1 || x1 > w2) return false;
      }
      if (y2 !== Infinity && h1 !== Infinity) {
        h2 += y2;
        h1 += y1;
        if (isNaN(h1) || isNaN(y2) || y2 > h1 || y1 > h2) return false;
      }
      return true;
    }
    

    If your program can be certain that the numbers will always be finite you can use a simpler version:

    // returns true if there is any overlap
    // params: x,y,w,h of two rectangles
    function intersects(x1, y1, w1, h1, x2, y2, w2, h2) {
        w2 += x2;
        w1 += x1;
        if (x2 > w1 || x1 > w2) return false;
        h2 += y2;
        h1 += y1;
        if (y2 > h1 || y1 > h2) return false;
      return true;
    }
    

    What its doing is finding the where the right side and bottom side of the two rectangles are, then it sees if the second one starts outside of the first one or if the first one starts outside of the second one.

    If either rectangle begins after the other one has ended, then there is no collision. Otherwise there must be a collision.

    0 讨论(0)
提交回复
热议问题