与正方形碰撞后如何保持圆周速度?

Rya*_*hel 6 javascript algorithm math collision-detection game-physics

我正在开发一个游戏,其中玩家是一个圆圈,瓷砖是正方形。用户用键盘移动头像(圆圈),不能与瓷砖(方块)发生碰撞。

另外,我希望圆圈在碰到角落时沿着正方形滑动,这样如果玩家继续按下键向同一方向移动,他们将沿着正方形滑动而不是卡在上面。

我已经开发了我在这里面临的问题的完整再现:

let canvas = document.querySelector("canvas");
let ctx = canvas.getContext("2d");

class Vec2 {
  constructor(x, y) {
    this.x = x || 0;
    this.y = y || 0;
  }

  distance(v) {
    let x = v.x - this.x;
    let y = v.y - this.y;

    return Math.sqrt(x * x + y * y);
  }

  magnitude() { 
    return Math.sqrt(this.x * this.x + this.y * this.y);
  }

  dot(v) { 
    return this.x * v.x + this.y * v.y;
  }

  normalize() {
    let magnitude = this.magnitude();
    
    return new Vec2(this.x / magnitude, this.y / magnitude);
  }
  
  multiply(val) {
    return typeof val === "number" ? new Vec2(this.x * val, this.y * val) : new Vec2(this.x * val.x, this.y * val.y);
  }

  subtract(val) {
    return typeof val === "number" ? new Vec2(this.x - val, this.y - val) : new Vec2(this.x - val.x, this.y - val.y);
  }

  add(val) {
    return typeof val === "number" ? new Vec2(this.x + val, this.y + val) : new Vec2(this.x + val.x, this.y + val.y);
  }
}

function clamp(value, min, max) {
  return Math.min(Math.max(value, min), max);
}

function drawCircle(xCenter, yCenter, radius) {
  ctx.beginPath();
  ctx.arc(xCenter, yCenter, radius, 0, 2 * Math.PI);
  ctx.fill();
}

function drawSquare(x, y, w, h) {
  ctx.beginPath();
  ctx.rect(x, y, w, h);
  ctx.stroke();
}

function circleRectangleCollision(cX, cY, cR, rX, rY, rW, rH) {
  let x = clamp(cX, rX, rX + rW);
  let y = clamp(cY, rY, rY + rH);

  let cPos = new Vec2(cX, cY);

  return cPos.distance(new Vec2(x, y)) < cR;
}

function getCircleRectangleDisplacement(rX, rY, rW, rH, cX, cY, cR, cVel) {
  let circle = new Vec2(cX, cY);

  let nearestX = Math.max(rX, Math.min(cX, rX + rW));
  let nearestY = Math.max(rY, Math.min(cY, rY + rH));    
  let dist = new Vec2(cX - nearestX, cY - nearestY);

  let tangentVel = dist.normalize().dot(cVel);

  // The original answer had `cVel.subtract(tangentVel * 2);` here
  // but that was giving me issues as well
  return cVel.add(tangentVel);
}

let circlePos = new Vec2(150, 80);
let squarePos = new Vec2(240, 110);

let circleR = 50;

let squareW = 100;
let squareH = 100;

let circleVel = new Vec2(5, 0);

draw = () => {
  ctx.fillStyle = "#b2c7ef";
  ctx.fillRect(0, 0, 800, 800); 

  ctx.fillStyle = "#ffffff";

  drawCircle(circlePos.x, circlePos.y, circleR);
  drawSquare(squarePos.x, squarePos.y, squareW, squareH);
}

update = () => {
  draw();

  if (circleRectangleCollision(circlePos.x, circlePos.y, circleR, squarePos.x, squarePos.y, squareW, squareH)) {
    circleVel = getCircleRectangleDisplacement(squarePos.x, squarePos.y, squareW, squareH, circlePos.x, circlePos.y, circleR, circleVel);
  }

  circlePos = circlePos.add(circleVel);
}

setInterval(update, 30);
Run Code Online (Sandbox Code Playgroud)
canvas { display: flex; margin: 0 auto; }
Run Code Online (Sandbox Code Playgroud)
<canvas width="800" height="800"></canvas>
Run Code Online (Sandbox Code Playgroud)

如果您运行该代码段,您将看到圆圈正确地围绕正方形移动,但随后它向下和向右移动。我不确定为什么会这样。它应该保持完全直线移动,然后向右移动。

不幸的是,我不是很擅长数学,所以我很难弄清楚为什么会发生这种情况。我学到的主要算法通过这个答案,而且还使用了以下的答案作为参考: 两个

我注意到的另一个问题是,如果您将circlePosfrom的 y 位置更改80240,那么它仍然沿着正方形的顶部滑动,而不是沿着正方形底部滑动的更自然的路径。如果可能的话,我也想解决这个问题。

此外,理想情况下,如果圆圈直接击中瓷砖,则根本不应该有任何滑动,如果这是有道理的。在这种情况下,它应该卡在正方形上。

tri*_*cot 1

我建议进行以下更改:

在您的类中定义另外两个方法:

  crossProductZ(v) {
    return this.x * v.y - v.x * this.y;
  }
  
  perpendicular() {
    return new Vec2(this.y, -this.x);
  }
Run Code Online (Sandbox Code Playgroud)

将语句getCircleRectangleDisplacement替换return为:

return dist.perpendicular().normalize()
           .multiply(cVel.magnitude() * Math.sign(cVel.crossProductZ(dist)));
Run Code Online (Sandbox Code Playgroud)

这个想法是,圆应该垂直于通过圆心和击中点(即dist)的线移动。当然,垂直线上有两个方向:它应该是与dist当前速度矢量位于同一侧的方向。这样圆就会选择正方形的右侧。

该移动的幅度应等于当前速度的幅度(以便速度没有变化,只是方向发生变化)。

最后,还要对update函数进行以下更改:

  let nextCirclePos = circlePos.add(circleVel);
  if (circleRectangleCollision(nextCirclePos.x, nextCirclePos.y, circleR, squarePos.x, squarePos.y, squareW, squareH)) {
    let currentVel = getCircleRectangleDisplacement(squarePos.x, squarePos.y, squareW, squareH, circlePos.x, circlePos.y, circleR, circleVel);
    nextCirclePos = circlePos.add(currentVel);
  }
  circlePos = nextCirclePos;
Run Code Online (Sandbox Code Playgroud)

这里的想法是,我们首先像往常一样进行移动 ( circleVel),看看这是否意味着碰撞。在这种情况下,我们不会采取这一行动相反,我们得到距离当前位置的位移。

而且,我们从不更新currentVel。这将保证一旦障碍物消失,运动将像以前一样继续进行。

在下面的代码片段中进行了这些更改。此外,我在圆的路径中添加了第二个正方形,一旦圆超出视野,我添加了第二次运行,其中圆采用不同的路径:

  crossProductZ(v) {
    return this.x * v.y - v.x * this.y;
  }
  
  perpendicular() {
    return new Vec2(this.y, -this.x);
  }
Run Code Online (Sandbox Code Playgroud)
return dist.perpendicular().normalize()
           .multiply(cVel.magnitude() * Math.sign(cVel.crossProductZ(dist)));
Run Code Online (Sandbox Code Playgroud)
  let nextCirclePos = circlePos.add(circleVel);
  if (circleRectangleCollision(nextCirclePos.x, nextCirclePos.y, circleR, squarePos.x, squarePos.y, squareW, squareH)) {
    let currentVel = getCircleRectangleDisplacement(squarePos.x, squarePos.y, squareW, squareH, circlePos.x, circlePos.y, circleR, circleVel);
    nextCirclePos = circlePos.add(currentVel);
  }
  circlePos = nextCirclePos;
Run Code Online (Sandbox Code Playgroud)

注意:碰撞和位移函数中有一些代码重复。他们计算的结果几乎相同。这可以优化。