Little things
This commit is contained in:
+44
-2
@@ -35,6 +35,9 @@ void Simulator::update(double dt) {
|
||||
bodies[i].update(dt);
|
||||
bodies[i].addToTrajectory(bodies[i].getPosition());
|
||||
}
|
||||
|
||||
// Check for collisions
|
||||
checkCollisions();
|
||||
}
|
||||
|
||||
glm::dvec3 Simulator::calculateGravitationalForce(const CelestialBody& body1, const CelestialBody& body2) {
|
||||
@@ -52,10 +55,49 @@ glm::dvec3 Simulator::calculateGravitationalForce(const CelestialBody& body1, co
|
||||
double forceMagnitude = G * (body1.getMass() * body2.getMass()) / (distance * distance);
|
||||
|
||||
if (std::isnan(forceMagnitude) || std::isinf(forceMagnitude)) {
|
||||
std::cout << "Warning: Invalid force magnitude calculated. Distance: " << distance
|
||||
<< ", Masses: " << body1.getMass() << ", " << body2.getMass() << std::endl;
|
||||
return glm::dvec3(0.0);
|
||||
}
|
||||
|
||||
return glm::normalize(direction) * forceMagnitude;
|
||||
}
|
||||
|
||||
void Simulator::handleCollision(CelestialBody& body1, CelestialBody& body2) {
|
||||
double totalMass = body1.getMass() + body2.getMass();
|
||||
|
||||
// Calculate center of mass position
|
||||
glm::dvec3 newPosition = (body1.getPosition() * body1.getMass() + body2.getPosition() * body2.getMass()) / totalMass;
|
||||
|
||||
// Calculate new velocity (momentum conservation)
|
||||
glm::dvec3 newVelocity = (body1.getVelocity() * body1.getMass() + body2.getVelocity() * body2.getMass()) / totalMass;
|
||||
|
||||
// Calculate new radius (assuming constant density)
|
||||
double newRadius = std::pow(std::pow(body1.getRadius(), 3) + std::pow(body2.getRadius(), 3), 1.0/3.0);
|
||||
|
||||
// Create new body
|
||||
CelestialBody newBody(totalMass, newPosition, newVelocity, newRadius);
|
||||
|
||||
// Replace body1 with the new body
|
||||
body1 = newBody;
|
||||
|
||||
// Remove body2
|
||||
auto it = std::find_if(bodies.begin(), bodies.end(), [&body2](const CelestialBody& b) {
|
||||
return &b == &body2;
|
||||
});
|
||||
if (it != bodies.end()) {
|
||||
bodies.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void Simulator::checkCollisions() {
|
||||
for (size_t i = 0; i < bodies.size(); ++i) {
|
||||
for (size_t j = i + 1; j < bodies.size(); ++j) {
|
||||
CelestialBody& body1 = bodies[i];
|
||||
CelestialBody& body2 = bodies[j];
|
||||
glm::dvec3 distanceVec = body1.getPosition() - body2.getPosition();
|
||||
double distance = glm::length(distanceVec);
|
||||
if (distance < (body1.getRadius() + body2.getRadius())) {
|
||||
handleCollision(body1, body2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user