简体   繁体   中英

2 rotating body in C++

I have this code in C++ that is suppose to simulate the rotation of a body (body2) around another one (body1) in the xy plane, and save the x and y position of body2 in a file:

#include<iostream>
#include <vector>
#include<math.h>
#include <fstream>
using namespace std;

class Body{
    private:

    double G= 1;
    double rx;
    double ry;
    double rz;
    double vx;
    double vy;
    double vz;
    double mass;
    double fx;
    double fy;
    double fz;
    double dt=0.01;


    public:

    Body(double rx, double ry, double rz, double vx, double vy, double vz, double mass){
        this->rx=rx;
        this->ry=ry;
        this->rz=rz;
        this->vx=vx;
        this->vy=vy;
        this->vz=vz;
        this->mass=mass;
    }

    void update(){
        vx=vx+dt*fx/mass;
        vy=vy+dt*fy/mass;
        vz=vz+dt*fz/mass;
        rx=rx+vx*dt;
        ry=ry+vy*dt;
        rz=rz+vz*dt;
    }


    double get_x(){
        return rx;
    }

    double get_y(){
        return ry;
    }

    double get_z(){
        return rz;
    }

    double get_fx(){
        return fx;
    }


    void resetForce(){
        fx=0.0;
        fy=0.0;
        fx=0.0;
    }

    void addForce(Body b){
        double dx=b.rx-rx;
        double dy=b.ry-ry;
        double dz=b.rz-rz;
        double dist = sqrt(dx*dx+dy*dy+dz*dz);
        double F=G*mass*b.mass/dist*dist;
        fx=fx+F*dx/dist;
        fy=fy+F*dy/dist;
        fz=fz+F*dz/dist;
     }


};



int main(){

    Body body1(0,0,0,0,0,0,1000), body2(100,0,0,0,10,0,10);

    ofstream pos;
    pos.open ("Position.txt");


    int N=10000;
    for(int i; i<N;i++){
        body2.resetForce();
        body2.addForce(body1);
        body2.update();
        pos<<body2.get_x()<<" "<<body2.get_y()<<endl;


    }
pos.close();

}

I expected the plot to look like a circle around the origin (where body1 is located), but it looks like this: 在此处输入图片说明

I assume it is something wrong with the way I update the speed and force at each step but I couldn't find the mistake, so I appreciate any help.

Thank you!

First mistake is in line

double F=G*mass*b.mass/dist*dist;

This line will be interpretated like this double F=(G*mass*b.mass/dist)*dist; so as you see it will we incorrect result and you need this one

double F=G*mass*b.mass/(dist*dist);

Second you're increasing force on each simulation step in this lines

    fx=fx+F*dx/dist;
    fy=fy+F*dy/dist;
    fz=fz+F*dz/dist;

But I suppose you shouldn't do so you need this change it like this

    fx=F*dx/dist;
    fy=F*dy/dist;
    fz=F*dz/dist;

Third I also suppose that your bodies actually are located to far from each other in initial configuration you can decrease initial distance like this:

Body body1(0,0,0,0,0,0,1000), body2(10,0,0,0,10,0,10);

Actually you also should take into account force with which body2 is affecting on body1 but yes you can say that is too small.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM