Javascript 两个对象之间的碰撞响应

Javascript 两个对象之间的碰撞响应,javascript,html,canvas,collision-detection,Javascript,Html,Canvas,Collision Detection,我正在开发一个程序,专门处理碰撞响应。目前使用的是我发现的一个片段的修改版本,但计算结果是零速度 Console未显示任何错误,但多边形立即失去其位置和速度(值为NaN时恢复为0)。我做错了什么?对不起,忘了加小提琴了。已编辑。对不起,发生了什么以及应该发生什么?我在小提琴上看到一个圆圈在跳动和移动。怎么了?哦,对不起,我应该说得更清楚一点——我说的是两个多边形,它们在曲线旁边朝着对方移动。 Art.prototype.modules.physics.response = function(a

我正在开发一个程序,专门处理碰撞响应。目前使用的是我发现的一个片段的修改版本,但计算结果是零速度


Console未显示任何错误,但多边形立即失去其位置和速度(值为NaN时恢复为0)。我做错了什么?

对不起,忘了加小提琴了。已编辑。对不起,发生了什么以及应该发生什么?我在小提琴上看到一个圆圈在跳动和移动。怎么了?哦,对不起,我应该说得更清楚一点——我说的是两个多边形,它们在曲线旁边朝着对方移动。
Art.prototype.modules.physics.response = function(a, b) {

  var rotate = function(x, y, sin, cos, reverse) {

    return {
      x: (reverse) ? (x * cos + y * sin) : (x * cos - y * sin),
      y: (reverse) ? (y * cos - x * sin) : (y * cos + x * sin)
    };

  }

  var dx = b.x - a.x,
    dy = b.y - a.y,
    angle = Math.atan2(dy, dx),
    sin = Math.sin(angle),
    cos = Math.cos(angle),
    position = {
      x: 0,
      y: 0
    },
    _position = rotate(dx, dy, sin, cos, true),
    velocity = rotate(a.velocity.x, a.velocity.y, sin, cos, true),
    _velocity = rotate(b.velocity.x, b.velocity.y, sin, cos, true),
    total = velocity.x - _velocity.x,
    offset = a.boundaries().width,
    _offset = b.boundaries().width;

  velocity.x = ((a.mass - b.mass) * velocity.x + 2 * b.mass * _velocity.x) / (a.mass + b.mass);

  _velocity.x = total + velocity.x;

  var abs = Math.abs(velocity.x) + Math.abs(_velocity.x),
    overlap = (offset + _offset) - Math.abs(position.x - _position.x);

  position.x += velocity.x / abs * overlap;

  _position.x += _velocity.x / abs * overlap;

  var positionF = rotate(position.x, position.y, sin, cos, false),
      _positionF = rotate(_position.x, _position.y, sin, cos, false);

  b.x = a.x + _positionF.x;
  b.y = a.y + _positionF.y;
  a.x = a.x + positionF.x;
  a.y = a.y + positionF.y;

  var velocityF = rotate(velocity.x, velocity.y, sin, cos, false),
    _velocityF = rotate(_velocity.x, _velocity.y, sin, cos, false);

  a.velocity.x = velocityF.x;
  a.velocity.y = velocityF.y;
  b.velocity.x = _velocityF.x;
  b.velocity.y = _velocityF.y;

  return this.response.core;

};