所以我有两个文件。主文件和nbody文件。

我想做的是使用SFML绘制到一个窗口上,基本上模拟了一个银轴轨道。我经常遇到的问题是,尽管从语法角度来看一切正常,但所有行星都超出了比例,并且完全不灵敏。

我不知道为什么它不起作用。
请帮助我解决问题。

这是nbody文件。

 #include SFML/Graphics.hpp
 #include SFML/Window.hpp
 #include iostream
 #include cmath
 #include cstring
 class Body:sf::Drawable{
  sf::String file;
  sf::Texture texture;

public:
  double x,y;
  double xvel,yvel,mass,scale,winsize;
  sf::Sprite sprite;
  Body(double _x,double _y,double _xvel, double _yvel, double           _mass,std::string _file,double _scale,double _winsize){
    x = _x;
    y = _y;
    xvel = _xvel;
    yvel = _yvel;
    mass = _mass;
    file = _file;
    scale = _scale;

  }
  sf::Sprite Sprite(){
    texture.loadFromFile(file);
    sprite.setTexture(texture);
    sprite.setPosition(x,y);
    return sprite;
  }

  void move(double position, std::vector<Body> galaxy, double time){
    double G = 6.67 * pow(10,-11);
    double Fx=0, Fy=0;

for(double i=0;i<galaxy.size();i++){
  if(position!=i){
double x1 = x;
double x2 = (galaxy.at(i)).x;
double dx = (x2-x1)*scale;  ////The math here works

double y1 = y;
double y2 = (galaxy.at(i)).y;
double dy = (y2-y1)*scale; /// The math here works

double m2 = (galaxy.at(i)).mass;
double m1 = mass; //results in mass

double r = sqrt((dx * dx)+(dy * dy));
double F = G*m1*m2/(r*r);


Fx += (F*dx)/r;
Fy += (F*dy)/r;

  }
  xvel += Fx/mass*time;
  yvel += Fy/mass*time;

  x+=xvel/100000;
  y+=yvel/100000;
  Sprite();
}
  }

 virtual void draw(sf::RenderTarget& target, sf::RenderStates states)      const{
   target.draw(sprite,states); //parameters should be in here
  }
};


主文件:

#include <cmath>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <string>
#include <vector>
#include "nbody.cpp"
int main(int argc, char *argv[]){
  void move(double position, std::vector<Body> galaxy,double scale);
  int f;

  std::cin>>f;
  double radius, scale, winsize;
  winsize = 800;

  std::cin>>radius;
  std::cout<<radius<<" \n";
  std::vector<Body> galaxy;

  scale = radius/(winsize/2);
  for(double i=0;i<f;i++){
    double x,y,xv,yv,m;
    std::string name;
    std::cin>>x;
    std::cin>>y;
    std::cin>>xv;
    std::cin>>yv;
    std::cin>>m;
    std::cin>>name;
    x = x/scale+(winsize/2);
    y = y/scale+(winsize/2);

    std::cout<<x<<" "<<y<< " " << xv << " " << yv<< " "<< m<< "\n";

    Body b(x,y,xv,yv,m,name,scale,winsize);
    galaxy.push_back(b);
  }

  sf::RenderWindow window(sf::VideoMode(winsize, winsize), "Universe");
  double time =0;
  while (window.isOpen())
    {
      sf::Event event;
      while (window.pollEvent(event))
    {
      if (event.type == sf::Event::Closed)
        window.close();
    }

      window.clear();
      for(double i=0;i<galaxy.size();i++){
    double position = i;
    (galaxy.at(i)).move(position,galaxy,time);
    sf::Sprite a = (galaxy.at(i).sprite);
    window.draw(a);
      }
      window.display();
      time +=.1;
    }

  return 0;
}


我有一个行星文本文件,该文件通过管道传递到函数中以生成太阳系。第一个数字是实体的数量,第二个是半径,每行中的所有其他元素是每个行星的属性:按顺序(x,y,xvel,yvel,mass,imagefile)

5
2.50e+11
0.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00  1.9890e+30      sun.gif
 1.4960e+11  0  0.0000e+00  2.9800e+04  5.9740e+24    earth.gif
 2.2790e+11  0           0.0000e+00  2.4100e+04  6.4190e+23     mars.gif
 5.7900e+10  0 0.0000e+00  4.7900e+04  3.3020e+23  mercury.gif
 1.0820e+11  0 0.0000e+00  3.5000e+04  4.8690e+24    venus.gif


谢谢

最佳答案

总是相同的问题:

首先计算所有力,然后在考虑所有相互作用后更新速度和位置。混合使用会导致以后的互动越来越多地与新职位一起计算。这就引入了一个漂移,它完全取决于集合中对象的顺序。

而且,为了皮特的缘故,请使用比辛拉式欧拉更好的东西(我想您还没有意识到)。对于某些快速的游戏物理而言,这是可以接受的,但对于半科学目的则不可接受。至少使用Verlet或更好的RK4或辛四阶方法。

标准链接:Moving stars around

10-07 12:22