-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.cpp
More file actions
52 lines (44 loc) · 2.24 KB
/
Particle.cpp
File metadata and controls
52 lines (44 loc) · 2.24 KB
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
//
// Created by lucy on 23/01/25.
//
#include "Particle.h"
#include <iostream>
#define PARSEC 3.086e13 // Parsec in km
void Particle::update(double deltaTime) {
// Update the position and velocity of the particle using leapfrog method
// dt: time step
long double vtx = this->velX + (0.5 * this->accelX * deltaTime);
long double vty = this->velY + (0.5 * this->accelY * deltaTime);
long double vtz = this->velZ + (0.5 * this->accelZ * deltaTime);
this->posX = this->posX + (vtx * deltaTime * (1/PARSEC));
this->posY = this->posY + (vty * deltaTime * (1/PARSEC));
this->posZ = this->posZ + (vtz * deltaTime * (1/PARSEC));
this->velX = vtx + (0.5 * this->accelX * deltaTime);
this->velY = vty + (0.5 * this->accelY * deltaTime);
this->velZ = vtz + (0.5 * this->accelZ * deltaTime);
}
void Particle::updateAcceleration(double accelX, double accelY, double accelZ) {
// Calculate the acceleration of the particle given the forces acting on it
this->accelX = this->accelX + accelX;
this->accelY = this->accelY + accelY;
this->accelZ = this->accelZ + accelZ;
}
void Particle::calculateKineticEnergy() {
// Calculate the kinetic energy of the particle
this->kineticEnergy = 0.5 * mass * ((this->velX * this->velX) + (this->velY * this->velY) + (this->velZ * this->velZ));
}
void Particle::updatePotentialEnergy(double combinedPotential) {
// Calculate the potential energy of the particle
this->potentialEnergy = combinedPotential;
}
void Particle::printParticle() const {
// Print the position, velocity, acceleration, mass, kinetic energy, and potential energy of the particle
std::cout << "Particle ID: " << this->id << std::endl;
std::cout << "Position: (" << this->posX << ", " << this->posY << ", " << this->posZ << ")" << std::endl;
std::cout << "Velocity: (" << this->velX << ", " << this->velY << ", " << this->velZ << ")" << std::endl;
std::cout << "Acceleration: (" << this->accelX << ", " << this->accelY << ", " << this->accelZ << ")" << std::endl;
std::cout << "Mass: " << this->mass << std::endl;
std::cout << "Kinetic Energy: " << this->kineticEnergy << std::endl;
std::cout << "Potential Energy: " << this->potentialEnergy << std::endl;
std::cout << std::endl;
}