我已经为我的玩家创建了碰撞检查功能,每当游戏结束时,物体(在本例中是向量圆)接触到玩家(也是向量圆)。
我已经成功地创建了一个逻辑,并且有一个碰撞检查正在工作,但是它并没有计算元素之间的实际距离(而不是当它们实际接触到游戏结束时,当它们彼此相距一定距离时结束游戏。
两个物体都在移动-障碍物在移动x+=6,玩家在跟随跑道,所以速度会有所不同。
我试着稍微调整距离,当障碍物的x是玩家的触摸x时,我设法结束了游戏,但当寄宿者触摸时,我实际上无法做到这一点。我附上了下面的代码;
class Player {
constructor(x, y, r) {
this.pos = createVector(x, y);
this.r = r;
this.vel = createVector(500, 500);
this.mag = 3;
this.velLerp = 0.1;
}
update() {
let mouse = createVector(mouseX - width / 2,
mouseY - height / 2);
mouse.setMag(this.mag);
this.vel.lerp(mouse, this.velLerp);
this.pos.add(this.vel);
collisionCheck(obstacle) {
let d = p5.Vector.dist(this.pos, obstacle.pos);
if (d < this.r + obstacle.r) {
console.log("GAME OVER");
return true;
}发布于 2020-11-05 20:10:25
该问题是由于圆的移动是3 (this.mag)像素这一事实引起的。你只是偶然得到了确切的接触点。大多数时候,它们是相交的。
如果检测到碰撞,将播放器的位置稍微改变this.r + obstacle.r - d的偏移量
collisionCheck(obstacle) {
let d = p5.Vector.dist(this.pos, obstacle.pos);
if (d < this.r + obstacle.r) {
let shift = p5.Vector.sub(this.pos, obstacle.pos);
shift.setMag(this.r + obstacle.r - d);
this.pos.add(shift);
console.log("GAME OVER");
return true;
}
}https://stackoverflow.com/questions/64696584
复制相似问题