javascript - 无论球的起始位置如何,我怎样才能始终以相同的速度将球移动到 (x,y) 位置

标签 javascript jquery game-physics

这张图片很好地说明了我的问题: http://i.imgur.com/2NdRXGn.gifv

球在靠近端点(橙色圆圈)时移动非常慢,但在远离端点时移动非常快。

发生这种情况的原因是因为我使用这段代码来计算球的垂直和水平速度

var startingBallSpeed = 100;
xDistance = targetX - this.x;
yDistance = targetY - this.y;

this.horizontalVelocity = (xDistance / startingBallSpeed);
this.verticalVelocity = (yDistance / startingBallSpeed);

问题:如何确保球以相同的速度行进并且仍会击中 targetX 和 targetY

当前行为:靠近端点的球移动缓慢,远离端点的球移动快速
期望的行为:无论球离终点有多近,它们都以相同的速度移动

JS: https://jsfiddle.net/7ct1ap53/1

ball1 = new Ball(400, 480, "green");
ball2 = new Ball(20, 480, "blue");

targetX = 500;
targetY = 400;
targetBall = new Ball(targetX, targetY, "magenta", radius=10)

var gameArea = {
  canvas: document.createElement("canvas"),
  start: function() {
    this.canvas.width = 500;
    this.canvas.height = 500;
    this.context = this.canvas.getContext("2d");
    document.body.insertBefore(this.canvas, document.body.childNodes[4]);
    this.interval = setInterval(updateGame, 20); //20
  }
};

function Ball(x, y, color, radius=15) {
  this.x = x;
  this.y = y;
  this.radius = radius;
  this.color = color

  this.draw = function() {
    gameArea.context.beginPath();
    gameArea.context.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
    gameArea.context.fillStyle = color;
    gameArea.context.fill();
    gameArea.context.closePath();
  }

  this.launch = function() {
    var startingBallSpeed = 100;
    xDistance = targetX - this.x;
    yDistance = targetY - this.y;

    this.horizontalVelocity = (xDistance / startingBallSpeed);
    this.verticalVelocity = (yDistance / startingBallSpeed);
  }

  this.updatePos = function() {
    this.x += this.horizontalVelocity;
    this.y += this.verticalVelocity;
  };
}


$(function() {
  startGame();
});


function updateGame() {
    gameArea.context.clearRect(0,0,500,500);
    ball1.updatePos();
  ball2.updatePos();

  ball1.draw();
  ball2.draw();
  targetBall.draw();
}

function startGame() {

  gameArea.start();
  ball1.launch();
  ball2.launch();
}

最佳答案

您需要对由 xDistance、yDistance 定义的向量进行归一化,以创建指定方向但长度为 1 的单位向量。然后您可以将其乘以所需的球速以获得速度。

除以长度归一化:

xDistance = targetX - this.x;
yDistance = targetY - this.y;
length = Math.sqrt((xDistance * xDistance) + (yDistance * yDistance));
if(length > 0) // avoid divide by zero
{
     xUnitVector = xDistance / length;
     yUnitVector = yDistance / length;

     this.horizontalVelocity = xUnitVector * startingBallSpeed;
     this.verticalVelocity = yUnitVector * startingBallSpeed;
}
else
{
     // cancel the launch because you are already at your destination
}

将你的球速调整到你需要的任何恒定速度

关于javascript - 无论球的起始位置如何,我怎样才能始终以相同的速度将球移动到 (x,y) 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37224628/

相关文章:

javascript - 如何在 Vuetify 的 v-img 中做后备 img?

.net - 在浏览器中使用 View 时,CSS 未在 ASP.NET 站点中加载

java - 改善月球车的运动

swift - 计算 SpriteKit 中的对象在受到脉冲时的轨迹

javascript - 执行通过ajax加载的内联js

javascript - 仅显示一个属性对象 - 在许多属性 objec 中,其中对象被包装在数组中

javascript - 如果字段为空 Bootstrap ,如何禁用发送按钮

jQuery PrettyPhoto 将 ID 传递给 iframe

javascript - asp.net gridview tr id 独特的 jQuery

java - 轻量级物理引擎Android