所以我用这种方式来检测方块移动和接触其他方块并做出反应。它使用勾股定理来生成第三个参数,因此当方块接触时您不会得到两个 true if 语句。我最近一直试图在正方形到长方形上使用这种方法,但似乎无法使其正常工作。我画了几条线来帮助可视化代码在做什么。有人对如何使这种碰撞正常工作有任何建议吗?
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let gravity = 1.5;
let friction = 0.9;
//CHARACTER:
class Player {
constructor(x, y, w, h, vx, vy, c, j) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.vx = vx;
this.vy = vy;
this.color = c;
this.jumping = j;
}
draw() {
context.fillStyle = this.color;
context.fillRect(this.x, this.y, this.w, this.h);
}
canvasCollision() {
if (this.x <= 0) …Run Code Online (Sandbox Code Playgroud)