HTML Canvas,小行星上的对象运动

时间:2019-03-19 22:27:07

标签: javascript html5-canvas

这更多是运动物理问题。但是我有一个小错误,在我正在制作的Asteroids游戏中无法弄清楚。对于我的船,当您按下向上箭头时,我会启用推力功能:

let rotationAngle = 0;

function thrust() {
  velocity[0] += Math.cos(rotationAngle);
  velocity[1] += Math.sin(rotationAngle);
}

据推测,这可以使飞船前进。 在我的游戏循环中调用的更新函数是:

function update() {
  spec.center.x += velocity[0] * .1;
  spec.center.y += velocity[1] * .1; //.1 is the acceleration
  if (spec.center.x > MyGame.graphics.canvas.width) spec.center.x = 0; //wrapping
  if (spec.center.x < 0) spec.center.x = MyGame.graphics.canvas.width;
  if (spec.center.y > MyGame.graphics.canvas.height) spec.center.y = 0;
  if (spec.center.y < 0) spec.center.y = MyGame.graphics.canvas.height;
}

这样可以使船舶在松开推力按钮时保持漂浮。这一切似乎都可以正常工作,除了它不是向上移动而是向右移动,即,如果我按下推力按钮使其向前移动,它就会向侧面移动。

我在这里错过了一些简单的东西吗?如果我需要提供有关渲染的更多详细信息,而我将很乐意这样做。

谢谢。

0 个答案:

没有答案