所以我有两个文件。主文件和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
由于
答案 0 :(得分:1)
总是同样的问题:
首先计算所有力,然后,当考虑所有相互作用时,更新速度和位置。将其混合会导致后来的相互作用越来越多地与新的位置进行计算。这会引入漂移,这完全取决于集合中对象的顺序。
为了Pete的缘故,使用比辛格欧拉更好的东西(而且我认为你没有意识到这一点)。这对于一些快速的肮脏游戏物理学来说是可以接受的,但不适用于半科学目的。至少使用Verlet或更好的RK4或辛4阶方法。
标准链接:Moving stars around
答案 1 :(得分:0)
x+=xvel/100000;
y+=yvel/100000;
应该是:
x+=xvel/scale*time;
y+=yvel/scale*time;
或者更好的是,保持你的x和y以米为单位进行物理操作并仅在绘制精灵时转换到屏幕空间。