Javascript游戏碰撞检测墙

时间:2019-01-11 13:34:27

标签: javascript collision-detection

我一直在遵循W3schools教程,以在画布https://www.w3schools.com/graphics/game_obstacles.asp中创建JavaScript游戏

我已经到了他们在其中添加障碍物的阶段。目前,它具有碰撞检测功能,当撞到墙壁时会停止游戏。我正在尝试找到一种方法,将其像一面墙一样,盒子可能会撞到墙,不再移动该方向并继续游戏,从而使墙起作用。

我以前曾尝试检测哪个方向撞在墙上并停止该方向的移动,但是当我按住箭头键时,它会在其中移动。

这里是我到目前为止所获得的:https://jsfiddle.net/j9cy1mne/1/

<body onload="startGame()">
  <script>
    var myGamePiece;
    var myObstacle;

    var speed = 3;

    function startGame() {
      myGamePiece = new component(30, 30, "red", 10, 120);
      myObstacle = new component(10, 200, "green", 300, 120);
      myGameArea.start();
    }

    var myGameArea = {
      canvas: document.createElement("canvas"),
      start: function() {
        this.canvas.width = 480;
        this.canvas.height = 270;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.interval = setInterval(updateGameArea, 20);
      },
      clear: function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
      },
      stop: function() {
        clearInterval(this.interval);
      }
    }

    function component(width, height, color, x, y) {
      this.width = width;
      this.height = height;
      this.speedX = 0;
      this.speedY = 0;
      this.x = x;
      this.y = y;
      this.update = function() {
        ctx = myGameArea.context;
        ctx.fillStyle = color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
      }
      this.crashWith = function(otherobj) {
        var myleft = this.x;
        var myright = this.x + (this.width);
        var mytop = this.y;
        var mybottom = this.y + (this.height);
        var otherleft = otherobj.x;
        var otherright = otherobj.x + (otherobj.width);
        var othertop = otherobj.y;
        var otherbottom = otherobj.y + (otherobj.height);
        var crash = true;

        if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
          crash = false;

        }


        return crash;
      }
    }

    function updateGameArea() {
      if (myGamePiece.crashWith(myObstacle)) {
        console.log("crash");
      } else {
        myGameArea.clear();
        myObstacle.update();
        myGamePiece.x += myGamePiece.speedX;
        myGamePiece.y += myGamePiece.speedY;
        myGamePiece.update();
      }
    }
    document.onkeydown = checkKeyD;

    function checkKeyD(e) {

      e = e || window.event;

      if (e.keyCode == '38') {
        // up arrow
        myGamePiece.speedY = -speed;
      } else if (e.keyCode == '40') {
        // down arrow
        myGamePiece.speedY = speed;
      } else if (e.keyCode == '37') {
        // left arrow
        myGamePiece.speedX = -speed;
      } else if (e.keyCode == '39') {
        // right arrow
        myGamePiece.speedX = speed;
      }

    }

    document.onkeyup = clearmove;

    function clearmove() {
      myGamePiece.speedX = 0;
      myGamePiece.speedY = 0;
    }

  </script>

</body>

1 个答案:

答案 0 :(得分:2)

问题在这里:

  if (myGamePiece.crashWith(myObstacle)) {
    console.log("crash");
  } else {

在真实的物理引擎中,您可以检测到碰撞,然后解决碰撞。最简单的“解决碰撞”是将零件移回碰撞之前的位置。像这样:

  if (myGamePiece.crashWith(myObstacle)) {
    resolveCollision(myGamePiece, myObstacle);
  } else {

但是要做到这一点,您需要修改物理引擎和运动函数以使用速度矢量。这意味着该函数将设置速度矢量,而不是function checkKeyD(e)移动片段。然后crashWith()将确定位置和速度矢量是否要崩溃,并确定碰撞是否会使其崩溃。

相关问题