forked from kidrigger/N-Body-Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Body.hpp
92 lines (75 loc) · 2.63 KB
/
Body.hpp
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
//
// Body.hpp
// nbody_bh
//
// Created by Anish Bhobe on 6/29/17.
// Copyright © 2017 Anish Bhobe. All rights reserved.
//
#ifndef Body_h
#define Body_h
#include <vector>
#include "Eigen/Core"
#include <array>
using Eigen::Vector3d;
namespace Celestial {
class Body {
public:
// Mass of the body
double mass;
// Position of the body
Vector3d position;
// Velocity of the body
Vector3d velocity;
// Acceleration of the body
// To be reset every frame.
Vector3d acceleration;
// Constructs empty body
Body():mass(0),position(0,0,0),velocity(0,0,0),acceleration(0,0,0) {}
// Copy the body
void operator= (const Body& body) {
mass = body.mass;
position = body.position;
velocity = body.velocity;
acceleration = body.acceleration;
}
// Resets the acceleration of the body for the next frame
void ResetAcceleration() { acceleration << 0,0,0; }
// Creates the body out of a vector or array of the form:
// m rx ry rz vx vy vz
void Create(const std::vector<double> &vec) {
mass = vec[0];
position << vec[1], vec[2], vec[3];
velocity << vec[4], vec[5], vec[6];
}
void Create(std::vector<double> &&vec) {
mass = vec[0];
position << vec[1], vec[2], vec[3];
velocity << vec[4], vec[5], vec[6];
}
void Create(const std::array<double,7> &vec) {
mass = vec[0];
position << vec[1], vec[2], vec[3];
velocity << vec[4], vec[5], vec[6];
}
void Create(std::array<double,7> &&vec) {
mass = vec[0];
position << vec[1], vec[2], vec[3];
velocity << vec[4], vec[5], vec[6];
}
// Prints the object as a string
std::string ToString() const {
std::stringstream iostr;
iostr << mass << " " << position[0] << " " << position[1] << " " << velocity[0] << " " << velocity[1];
return iostr.str();
}
// Calculates the center of mass and total mass of the two bodies
static Body CalculateCG(const Body& A, const Body& B) {
Body temp;
temp.mass = A.mass + B.mass;
temp.position = (A.mass * A.position + B.mass * B.position)/temp.mass;
temp.velocity = (A.mass * A.velocity + B.mass * B.velocity)/temp.mass;
return temp;
}
};
}
#endif /* Body_h */