-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolarSystem.cs
More file actions
296 lines (258 loc) · 10.3 KB
/
SolarSystem.cs
File metadata and controls
296 lines (258 loc) · 10.3 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using OpenGL;
using System.Text.Json;
namespace Licenta_ver1
{
public class CelestialBodyData
{
public string Name { get; set; }
public Vertex3f Position { get; set; }
public Vertex3f Velocity { get; set; }
public double Radius { get; set; }
public Color Color { get; set; }
public double Mass { get; set; }
public string TexturePath { get; set; }
// new properties in degrees
public float OrbitInclinationDeg { get; set; }
public float SpinAxisDeg { get; set; }
public float SpinPhaseDeg { get; set; }
}
public struct Vec3 { public double x, y, z; }
/// <summary>
/// Manages a collection of celestial bodies: loading, simulation, rendering.
/// </summary>
public class SolarSystem : IDisposable
{
private readonly Renderer _renderer;
private readonly List<PlanetLoader.CelestialBodyData> _bodies;
public float DistanceScale = 1e-10f;
public float PlanetRadiusScale = 6e-7f;
public float SunRadiusScale = 5e-6f;
public SolarSystem(string jsonPath)
{
// 1) Load raw JSON data
_bodies = PlanetLoader.LoadFromJson(jsonPath);
// 3) Build the Sphere list
var spheres = new List<Sphere>(_bodies.Count);
foreach (var b in _bodies)
{
// convert tilts & spins to radians
float orbitTiltRad = b.OrbitInclinationDeg * MathF.PI / 180f;
float spinAxisRad = b.SpinAxisDeg * MathF.PI / 180f;
float spinPhaseRad = b.SpinPhaseDeg * MathF.PI / 180f;
// compute orbit normal & spin axis (tilted Y axis)
var orbitNormal = new Vertex3f(
MathF.Sin(orbitTiltRad),
MathF.Cos(orbitTiltRad),
0f
);
var spinAxis = new Vertex3f(
MathF.Sin(spinAxisRad),
MathF.Cos(spinAxisRad),
0f
);
// apply distance and radius scaling
var center = new Vertex3f(
(float)b.Position.x * DistanceScale,
(float)b.Position.y * DistanceScale,
(float)b.Position.z * DistanceScale
);
float radius;
if (Path.GetFileNameWithoutExtension(b.TexturePath).ToLower() == "sun")
radius = b.Radius * SunRadiusScale;
else
radius = b.Radius * PlanetRadiusScale;
// add to list
spheres.Add(new Sphere(
center,
radius,
b.TexturePath,
orbitNormal,
spinAxis,
spinPhaseRad
));
}
// 4) Create the renderer with all spheres at once
_renderer = new Renderer(spheres);
}
public void Render(int w, int h, string primaryfocus, string? secondaryfocus, float yaw, float pitch, float dist)
=> _renderer.Render(w, h, primaryfocus, secondaryfocus, yaw, pitch, dist);
public void Dispose()
=> _renderer.Dispose();
/// <summary>
/// Advance the physics simulation by dt seconds (simple Euler integrator).
/// </summary>
public void Simulate(double dt)
{
int n = _bodies.Count;
if (n < 2) return;
const double G = 6.674e-11;
// zero forces
for (int i = 0; i < n; i++)
_bodies[i].Force = new Vertex3f(0, 0, 0);
// pairwise gravity
for (int i = 0; i < n; i++)
{
var bi = _bodies[i];
for (int j = i + 1; j < n; j++)
{
var bj = _bodies[j];
float dx = bj.Position.x - bi.Position.x;
float dy = bj.Position.y - bi.Position.y;
float dz = bj.Position.z - bi.Position.z;
double dist2 = dx * dx + dy * dy + dz * dz;
if (dist2 <= 0) continue;
double dist = Math.Sqrt(dist2);
if (dist <= bi.Radius + bj.Radius) continue;
double mag = G * bi.Mass * bj.Mass / dist2;
float ux = (float)(dx / dist);
float uy = (float)(dy / dist);
float uz = (float)(dz / dist);
var fij = new Vertex3f(
(float)(mag * ux),
(float)(mag * uy),
(float)(mag * uz)
);
bj.SpinPhaseDeg = (bj.SpinPhaseDeg + bj.SpinRateDegPerSec * (float)dt) % 360f;
bi.Force += fij;
bj.Force -= fij;
}
}
// integrate
for (int i = 0; i < n; i++)
{
var b = _bodies[i];
var acc = b.Force / b.Mass;
b.Velocity += acc * (float)dt;
b.Position += b.Velocity * (float)dt;
}
}
/// <summary>
/// Builds a scaled snapshot of all bodies and pushes it into the GPU renderer.
/// </summary>
public void UpdateRenderer()
{
var scaledData = new List<PlanetLoader.CelestialBodyData>(_bodies.Count);
foreach (var b in _bodies)
{
scaledData.Add(new PlanetLoader.CelestialBodyData
{
Name = b.Name,
Position = new Vertex3f(
b.Position.x * DistanceScale,
b.Position.y * DistanceScale,
b.Position.z * DistanceScale
),
Velocity = b.Velocity,
Radius = b.Radius * (Path.GetFileNameWithoutExtension(b.TexturePath) == "sun" ? SunRadiusScale : PlanetRadiusScale),
Color = b.Color,
Mass = b.Mass,
TexturePath = b.TexturePath,
OrbitInclinationDeg = b.OrbitInclinationDeg,
SpinAxisDeg = b.SpinAxisDeg,
SpinPhaseDeg = b.SpinPhaseDeg
});
}
// push all scaled data to TestBody in one go:
_renderer.UpdateAllSphereData(scaledData);
}
}
public static class PlanetLoader
{
public class PlanetJson
{
public string Name { get; set; }
public class Pos
{
public double x { get; set; }
public double y { get; set; }
public double z { get; set; }
}
public Pos Position { get; set; }
public class Vel
{
public double vx { get; set; }
public double vy { get; set; }
public double vz { get; set; }
}
public Vel Velocity { get; set; }
public double Radius { get; set; }
public class Col
{
public float r { get; set; }
public float g { get; set; }
public float b { get; set; }
}
public Col Color { get; set; }
public double Mass { get; set; }
public string TexturePath { get; set; }
public float OrbitInclinationDeg { get; set; }
public float SpinAxisDeg { get; set; }
public float SpinPhaseDeg { get; set; }
public float SpinRateDegPerSec { get; set; }
}
public class CelestialBodyData
{
public string Name;
public Vertex3f Position;
public Vertex3f Velocity;
public float Radius;
public Color Color; // System.Drawing.Color
public double Mass;
public string TexturePath;
public float OrbitInclinationDeg;
public float SpinAxisDeg;
public float SpinPhaseDeg;
public Vertex3f Force;
public float SpinRateDegPerSec;
}
public static List<CelestialBodyData> LoadFromJson(string jsonPath)
{
if (!File.Exists(jsonPath))
throw new FileNotFoundException("JSON file not found", jsonPath);
var json = File.ReadAllText(jsonPath);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true
};
var raw = JsonSerializer
.Deserialize<List<PlanetJson>>(json, options)
?? throw new InvalidDataException("Failed to parse JSON");
var list = new List<CelestialBodyData>(raw.Count);
foreach (var p in raw)
{
// convert JSON color (0…1 floats) → 0…255 ints safely
int R = (int)Math.Clamp(p.Color.r * 255f, 0f, 255f);
int G = (int)Math.Clamp(p.Color.g * 255f, 0f, 255f);
int B = (int)Math.Clamp(p.Color.b * 255f, 0f, 255f);
var col = System.Drawing.Color.FromArgb(R, G, B);
// explicit casts from double → float
var pos = new Vertex3f(
(float)p.Position.x,
(float)p.Position.y,
(float)p.Position.z
);
var vel = new Vertex3f(
(float)p.Velocity.vx,
(float)p.Velocity.vy,
(float)p.Velocity.vz
);
list.Add(new CelestialBodyData
{
Name = p.Name,
Position = pos,
Velocity = vel,
Radius = (float)p.Radius,
Color = col,
Mass = p.Mass,
TexturePath = p.TexturePath ?? "",
OrbitInclinationDeg = p.OrbitInclinationDeg,
SpinAxisDeg = p.SpinAxisDeg,
SpinPhaseDeg = p.SpinPhaseDeg,
SpinRateDegPerSec = p.SpinRateDegPerSec
});
}
return list;
}
}
}