我的重力模拟排斥第一和第三象限中的物体

My gravity simulation is repulsing objects in 1nd and 3rd quadrant

我正在创建简单的重力模拟。我使用 Pixi.JS 来渲染对象。我使用 Math.atan2 来计算从对象 A 到对象 B 的矢量角度。出于某种原因,当对象位于某些角度时它无法正常工作 - 我得到的是排斥力而不是吸引力。

var renderer = PIXI.autoDetectRenderer(600, 500,{backgroundColor : 0x0});
document.body.appendChild(renderer.view);

// create the root of the scene graph
var stage = new PIXI.Container();

// create a texture from an image path
var texture = PIXI.Texture.fromImage('http://pixijs.github.io/examples/_assets/basics/bunny.png');

var objects = [];
// start animating
animate();
function animate() {
    requestAnimationFrame(animate);
    // render the container
    renderer.render(stage);
}

/**
  the third argument, mass, must be in kilograms **/
function GravityWell(x,y,mass) {
  this.sprite = new PIXI.Sprite(texture);
  // center the sprite's anchor point
  this.sprite.anchor.x = 0.5;
  this.sprite.anchor.y = 0.5;

  // move the sprite to the center of the screen
  this.sprite.position.x = this.x = x;
  this.sprite.position.y = this.y = y;
  stage.addChild(this.sprite);
  
  this.rotation = 0;
  this.sprite.rotation = this.rotation;
  
  this.vx = 0;
  this.vy = 0;
  
  this.mass = mass;
}
GravityWell.prototype.G = 6.674e-11;
GravityWell.prototype.acceleration = function(object) {
  var dist = (this.x-object.x)*(this.x-object.x)+(this.y-object.y)*(this.y-object.y);
  if(dist>0)
    return (this.G*this.mass*object.mass)/dist;
  else
    return 0;
}
// This should be correct according to http://gamedev.stackexchange.com/a/33710/25920
GravityWell.prototype.angleTo = function(object) {
  return Math.atan2(-this.x+object.x, -this.y+object.y);
}

GravityWell.prototype.accelerate = function(direction, magnitude) {
  var deltaV = magnitude/this.mass;
  this.vx += deltaV*Math.cos(direction);
  this.vy += deltaV*Math.sin(direction);
}
GravityWell.prototype.move = function(dt) {
  dt /= 1000;
  this.x += this.vx*dt;
  this.y += this.vy*dt;
  this.sprite.position.x = this.x;
  this.sprite.position.y = this.y;
}
// Creating objects 
var ship;
objects.push(new GravityWell(300, 250, 1000000000));
objects.push(new GravityWell(400, 400, 20000));
objects.push(new GravityWell(100, 100, 20000));
objects.push(new GravityWell(400, 100, 20000));


var last = performance.now();
setInterval(function() {
  var dt = performance.now() - last;
  for(var i=0,l=objects.length; i<l; i++) {
    var obj = objects[i]; 
    for(var j=i+1,l=objects.length; j<l; j++) {
      var a = obj.acceleration(objects[j]);
      if(a!=0) {
        obj.accelerate(obj.angleTo(objects[j]), a);
        objects[j].accelerate(objects[j].angleTo(obj), a);
      }
    }
  }
  for(var i=0,l=objects.length; i<l; i++) {
    objects[i].move(dt*10000);
  }
  
}, 10);
<script src="//darker.github.io/asteroids/demo-elastic-bounce/pixi.js"></script>

我不确定我做错了什么。我试图改变登录加速但它没有达到预期的效果。

您的代码中有几个错误。

1.) 如果dx = r*cos(phi)dy = r*sin(phi)用于速度更新,则

    phi = atan2( dy, dx )

即参数的倒序。

2.) 对于力矢量计算,您根本不需要角度,如

var dx = this.x-object.x;
var dy = this.y-object.y;

var dist = Math.sqrt(dx*dx+dy*dy);

var deltaV = magnitude/this.mass;

var deltaVx = dx/dist;
var deltaVy = dy/dist;

3.) 您对速度的更新还需要时间步长 dtv += a*dt。插入此内容的一个合乎逻辑的点是

  var deltaV = magnitude*dt/this.mass;

或者在参数传递中的大小。