[英]Collision Detection using 2D Canvas
我有一些运行良好的代码,但是有人可以解释一下如何防止视频游戏中的碰撞吗? 此代码实际上是什么意思?
var checkCollision = function (bugs) {
if(player.y + 131 >= bugs.y + 90
&& player.x + 25 <= bugs.x + 88
&& player.y + 73 <= bugs.y + 135
&& player.x + 76 >= bugs.x + 11) {
console.log('YOU LOSE!');
}
}
这是一种常见的碰撞检测方法。 首先,每个对象,即player
和bug
,都有其位置以及width
和height
:
现在,如果我们查看以下形式的条件之一: player.x + A >= bugs.x + B
我们会有一行:
将四行放在一起,您将获得一个碰撞盒:
这是碰撞趋向的样子:
if (player.x < bug.x + bug.width // Player X is to the left of the bug's right
&& player.x + player.width > bug.x // Player X is the to right of the bug's left
&& player.y < bug.y + bug.height // Player Y is above the bottom of the bug
&& player.height + player.y > bug.y) { // Player Y is below the top of the bug
// found a collision
}
这是player
和bug
之间的重叠:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.