-
Notifications
You must be signed in to change notification settings - Fork 0
/
n-body-physics.h
126 lines (104 loc) · 2.8 KB
/
n-body-physics.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* n-body-physics.h *
* Physics for n-body simulation *
* By: Gareth Ferneyhough *
* University of Nevada, Reno */
#include <vector>
#include <iostream>
#include <ostream>
#include <cmath>
#include <boost/serialization/serialization.hpp>
using std::cout;
using std::endl;
class Vector{
public:
Vector(){
x = 0;
y = 0;
z = 0;
}
Vector(double x_component, double y_component, double z_component){
x = x_component;
y = y_component;
z = z_component;
}
Vector(const Vector& p){
x = p.x;
y = p.y;
z = p.z;
}
Vector& operator=(const Vector& v){
x = v.x;
y = v.y;
z = v.z;
return *this;
}
double magnitude(){
return sqrt(x*x + y*y + z*z);
}
Vector operator-(const Vector& rhs){
return Vector(this->x - rhs.x, this->y - rhs.y, this->z - rhs.z);
}
Vector operator+(const Vector& rhs){
return Vector(this->x + rhs.x, this->y + rhs.y, this->z + rhs.z);
}
Vector operator+=(const Vector& rhs){
return Vector(this->x += rhs.x, this->y += rhs.y, this->z += rhs.z);
}
Vector operator*(const double& d){
return Vector(this->x *d, this->y *d, this->z *d);
}
Vector operator/(const double& d){
return Vector(this->x / d, this->y / d, this->z / d);
}
double x;
double y;
double z;
private:
friend std::ostream& operator<<(std::ostream& os, const Vector& v);
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & x;
ar & y;
ar & z;
}
};
class Body{
public:
Body() {};
explicit Body(Vector position, Vector velocity, double mass){
this->position = position, this->velocity = velocity, this->mass = mass;
total_force = Vector(0, 0, 0);
}
Vector position; // Meters
Vector velocity; // Meters / second
Vector total_force; // Newtons
double mass; // Kg
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & position;
ar & mass;
ar & velocity;
//ar & total_force; // dont send force. It is pointless.
}
};
class NBodyPhysics{
public:
void addBody(Body b);
void updateState(std::vector<Body>& state, const unsigned int index_begin, const unsigned int num_to_update, const double dt);
void updateState(double dt);
void saveState(std::vector<Body>&) const;
void printState() const;
int getNumBodies() const;
void setNewState(std::vector<Body>& state);
private:
static const double G = 6.674e-11;
std::vector<Body> bodies;
void updateForces();
void updateForces(std::vector<Body>& state, const unsigned int index_begin, const unsigned int num_to_update);
Vector calculateForce(Body &b1, Body &b2);
};