Javascript canvas collision detection

前端 未结 3 1959
一生所求
一生所求 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;
    }
    

提交回复
热议问题