-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlanet.cpp
More file actions
96 lines (78 loc) · 2.99 KB
/
Copy pathPlanet.cpp
File metadata and controls
96 lines (78 loc) · 2.99 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
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
#include "Planet.h"
#include <cmath>
Planet::Planet(std::string n, float radius, float distance, float speed, sf::Color color, Planet* parent, bool hasTrail, PlanetInfo planetInfo)
: name(n), orbitDistance(distance), rotationSpeed(speed), currentAngle(0.f), parentPlanet(parent), showTrail(hasTrail), info(planetInfo)
{
shape.setRadius(radius);
shape.setOrigin({ radius, radius });
shape.setFillColor(color);
if (showTrail) {
trail.setPrimitiveType(sf::PrimitiveType::LineStrip);
}
}
void Planet::update(float deltaTime, sf::Vector2f centerOffset) {
// Return early if simulation is paused or time is stopped (fixes trail disappearance bug)
if (deltaTime <= 0.f) return;
float finalRotationSpeed = rotationSpeed;
// POSITION UPDATE
currentAngle += finalRotationSpeed * deltaTime;
float x = std::cos(currentAngle) * orbitDistance;
float y = std::sin(currentAngle) * orbitDistance;
sf::Vector2f newPos(x, y);
if (parentPlanet != nullptr) {
newPos += parentPlanet->getPosition();
}
else {
newPos += centerOffset;
}
shape.setPosition(newPos);
// PLANETARY TRAIL LOGIC (Only processed when moving)
if (showTrail) {
// Append the new position vertex
trail.append(sf::Vertex(shape.getPosition(), shape.getFillColor()));
// Cap maximum trail length (e.g., 300 points)
if (trail.getVertexCount() > 300) {
// Shift vertices left to remove the oldest point
for (size_t i = 0; i < trail.getVertexCount() - 1; i++) {
trail[i] = trail[i + 1];
}
trail.resize(trail.getVertexCount() - 1);
}
// Recalculate alpha gradient for fading effect
size_t count = trail.getVertexCount();
for (size_t i = 0; i < count; ++i) {
float progress = static_cast<float>(i) / static_cast<float>(count);
sf::Color color = shape.getFillColor();
color.a = static_cast<unsigned char>(255.f * progress);
trail[i].color = color;
}
}
}
void Planet::draw(sf::RenderWindow& window) {
// Check visibility state
if (!isVisible) return;
// Draw orbital trail if enabled and populated
if (showTrail && trail.getVertexCount() > 0) {
window.draw(trail);
}
// Draw planetary body
window.draw(shape);
// Visual effect: Saturn's Rings
// Target Saturn specifically by checking its string identifier
if (name == "Saturno") {
sf::CircleShape ring(shape.getRadius() * 1.8f);
ring.setOrigin({ ring.getRadius(), ring.getRadius() });
ring.setPosition(shape.getPosition());
ring.setFillColor(sf::Color::Transparent);
ring.setOutlineThickness(2.f);
ring.setOutlineColor(sf::Color(200, 200, 150, 150));
ring.setScale({ 1.f, 0.4f });
window.draw(ring);
}
}
sf::Vector2f Planet::getPosition() const {
return shape.getPosition();
}
float Planet::getRadius() const {
return shape.getRadius();
}