broadfield-dev commited on
Commit
b2c5fcd
·
verified ·
1 Parent(s): 1414c41

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +159 -31
static/script.js CHANGED
@@ -5,11 +5,16 @@ let fluidParticles = [];
5
  let simulationRunning = false;
6
 
7
  const PARTICLE_COUNT = 5000;
8
- const SPACE_SIZE = 40; // Increased space size for solar system scale
9
  const FLUID_SPEED = 0.1;
10
- let FLUID_FRICTION = 0.9; // Adjustable friction
11
- let FLUID_DEFLECTION = 0.1; // Adjustable deflection
12
- const GRAVITY_CONSTANT = 0.1; // Scaled gravitational constant
 
 
 
 
 
13
 
14
  init();
15
  animate();
@@ -33,7 +38,7 @@ function init() {
33
  const sunMaterial = new THREE.MeshBasicMaterial({ color: 0xFFFF00 });
34
  const sun = new THREE.Mesh(sunGeometry, sunMaterial);
35
  sun.position.set(0, 0, 0);
36
- sun.userData = { mass: 1.989e4, velocity: new THREE.Vector3(0, 0, 0) };
37
  scene.add(sun);
38
  spheres.push(sun);
39
 
@@ -41,10 +46,10 @@ function init() {
41
  const earthGeometry = new THREE.SphereGeometry(0.3, 32, 32);
42
  const earthMaterial = new THREE.MeshBasicMaterial({ color: 0x0000FF });
43
  const earth = new THREE.Mesh(earthGeometry, earthMaterial);
44
- earth.position.set(10, 0, 0);
45
  earth.userData = {
46
- mass: 5.972e-2,
47
- velocity: new THREE.Vector3(0, 0, 0.0298), // Initial orbital velocity in Z direction
48
  centripetalScale: 1
49
  };
50
  scene.add(earth);
@@ -54,10 +59,10 @@ function init() {
54
  const marsGeometry = new THREE.SphereGeometry(0.25, 32, 32);
55
  const marsMaterial = new THREE.MeshBasicMaterial({ color: 0xFF4500 });
56
  const mars = new THREE.Mesh(marsGeometry, marsMaterial);
57
- mars.position.set(15, 0, 0);
58
  mars.userData = {
59
- mass: 6.417e-3,
60
- velocity: new THREE.Vector3(0, 0, 0.0241), // Initial orbital velocity in Z direction
61
  centripetalScale: 1
62
  };
63
  scene.add(mars);
@@ -100,6 +105,9 @@ function init() {
100
  resetSimulation();
101
  });
102
 
 
 
 
103
  // Update sphere positions, masses, orbital velocities, centripetal force, and fluid interactions
104
  ['sun', 'earth', 'mars'].forEach(body => {
105
  document.getElementById(`${body}-mass`).addEventListener('input', updateParams);
@@ -121,37 +129,48 @@ function init() {
121
  camera.updateProjectionMatrix();
122
  renderer.setSize(window.innerWidth - 300, window.innerHeight);
123
  });
 
 
 
124
  }
125
 
126
  function updateParams() {
127
  // Update Sun
128
- spheres[0].userData.mass = parseFloat(document.getElementById('sun-mass').value) * 1e4;
 
 
129
  spheres[0].position.set(
130
- parseFloat(document.getElementById('sun-x').value),
131
- parseFloat(document.getElementById('sun-y').value),
132
- parseFloat(document.getElementById('sun-z').value)
133
  );
134
 
135
  // Update Earth
136
- spheres[1].userData.mass = parseFloat(document.getElementById('earth-mass').value) * 1e4;
 
 
137
  spheres[1].position.set(
138
- parseFloat(document.getElementById('earth-x').value),
139
- parseFloat(document.getElementById('earth-y').value),
140
- parseFloat(document.getElementById('earth-z').value)
141
  );
142
  const earthVelocity = parseFloat(document.getElementById('earth-orbital-velocity').value);
143
- spheres[1].userData.velocity.set(0, 0, earthVelocity); // Update velocity in Z direction for orbit
 
144
  spheres[1].userData.centripetalScale = parseFloat(document.getElementById('earth-centripetal').value);
145
 
146
  // Update Mars
147
- spheres[2].userData.mass = parseFloat(document.getElementById('mars-mass').value) * 1e4;
 
 
148
  spheres[2].position.set(
149
- parseFloat(document.getElementById('mars-x').value),
150
- parseFloat(document.getElementById('mars-y').value),
151
- parseFloat(document.getElementById('mars-z').value)
152
  );
153
  const marsVelocity = parseFloat(document.getElementById('mars-orbital-velocity').value);
154
- spheres[2].userData.velocity.set(0, 0, marsVelocity); // Update velocity in Z direction for orbit
 
155
  spheres[2].userData.centripetalScale = parseFloat(document.getElementById('mars-centripetal').value);
156
 
157
  // Update fluid interaction parameters
@@ -174,10 +193,119 @@ function resetSimulation() {
174
  });
175
 
176
  // Reset positions and velocities for Earth and Mars
177
- spheres[1].position.set(10, 0, 0);
178
- spheres[1].userData.velocity.set(0, 0, 0.0298);
179
- spheres[2].position.set(15, 0, 0);
180
- spheres[2].userData.velocity.set(0, 0, 0.0241);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  }
182
 
183
  function animate() {
@@ -192,7 +320,7 @@ function animate() {
192
  // Check for interactions with spheres
193
  spheres.forEach(sphere => {
194
  let distance = position.distanceTo(sphere.position);
195
- let sphereRadius = sphere.geometry.parameters.radius + 0.5; // Interaction radius
196
 
197
  if (distance < sphereRadius) {
198
  // Apply friction
@@ -223,7 +351,7 @@ function animate() {
223
  spheres.forEach((otherSphere, j) => {
224
  if (i !== j) {
225
  let distance = sphere.position.distanceTo(otherSphere.position);
226
- if (distance > 0.1) { // Avoid division by zero
227
  let direction = otherSphere.position.clone().sub(sphere.position).normalize();
228
  let force = (GRAVITY_CONSTANT * otherSphere.userData.mass) / (distance * distance);
229
  acceleration.add(direction.multiplyScalar(force * sphere.userData.centripetalScale));
 
5
  let simulationRunning = false;
6
 
7
  const PARTICLE_COUNT = 5000;
8
+ const SPACE_SIZE = 40;
9
  const FLUID_SPEED = 0.1;
10
+ let FLUID_FRICTION = 0.9;
11
+ let FLUID_DEFLECTION = 0.1;
12
+ const GRAVITY_CONSTANT = 0.1;
13
+
14
+ // Scaling factors
15
+ const MASS_SCALE = 1e-26; // Scale down masses for simulation
16
+ const DISTANCE_SCALE = 1e-7; // Scale down distances (km to simulation units)
17
+ const VELOCITY_SCALE = 1e-3; // Scale down velocities (km/s to simulation units)
18
 
19
  init();
20
  animate();
 
38
  const sunMaterial = new THREE.MeshBasicMaterial({ color: 0xFFFF00 });
39
  const sun = new THREE.Mesh(sunGeometry, sunMaterial);
40
  sun.position.set(0, 0, 0);
41
+ sun.userData = { mass: 1.989e30 * MASS_SCALE, velocity: new THREE.Vector3(0, 0, 0) };
42
  scene.add(sun);
43
  spheres.push(sun);
44
 
 
46
  const earthGeometry = new THREE.SphereGeometry(0.3, 32, 32);
47
  const earthMaterial = new THREE.MeshBasicMaterial({ color: 0x0000FF });
48
  const earth = new THREE.Mesh(earthGeometry, earthMaterial);
49
+ earth.position.set(149.6e6 * DISTANCE_SCALE, 0, 0);
50
  earth.userData = {
51
+ mass: 5.972e24 * MASS_SCALE,
52
+ velocity: new THREE.Vector3(0, 0, 29.8 * VELOCITY_SCALE),
53
  centripetalScale: 1
54
  };
55
  scene.add(earth);
 
59
  const marsGeometry = new THREE.SphereGeometry(0.25, 32, 32);
60
  const marsMaterial = new THREE.MeshBasicMaterial({ color: 0xFF4500 });
61
  const mars = new THREE.Mesh(marsGeometry, marsMaterial);
62
+ mars.position.set(227.9e6 * DISTANCE_SCALE, 0, 0);
63
  mars.userData = {
64
+ mass: 6.417e23 * MASS_SCALE,
65
+ velocity: new THREE.Vector3(0, 0, 24.1 * VELOCITY_SCALE),
66
  centripetalScale: 1
67
  };
68
  scene.add(mars);
 
105
  resetSimulation();
106
  });
107
 
108
+ document.getElementById('save-btn').addEventListener('click', saveSettings);
109
+ document.getElementById('load-btn').addEventListener('click', loadSettings);
110
+
111
  // Update sphere positions, masses, orbital velocities, centripetal force, and fluid interactions
112
  ['sun', 'earth', 'mars'].forEach(body => {
113
  document.getElementById(`${body}-mass`).addEventListener('input', updateParams);
 
129
  camera.updateProjectionMatrix();
130
  renderer.setSize(window.innerWidth - 300, window.innerHeight);
131
  });
132
+
133
+ // Initial update to display scaled values
134
+ updateParams();
135
  }
136
 
137
  function updateParams() {
138
  // Update Sun
139
+ const sunMass = parseFloat(document.getElementById('sun-mass').value);
140
+ spheres[0].userData.mass = sunMass * MASS_SCALE;
141
+ document.getElementById('sun-mass-scaled').textContent = (sunMass * MASS_SCALE).toExponential(2);
142
  spheres[0].position.set(
143
+ parseFloat(document.getElementById('sun-x').value) * DISTANCE_SCALE,
144
+ parseFloat(document.getElementById('sun-y').value) * DISTANCE_SCALE,
145
+ parseFloat(document.getElementById('sun-z').value) * DISTANCE_SCALE
146
  );
147
 
148
  // Update Earth
149
+ const earthMass = parseFloat(document.getElementById('earth-mass').value);
150
+ spheres[1].userData.mass = earthMass * MASS_SCALE;
151
+ document.getElementById('earth-mass-scaled').textContent = (earthMass * MASS_SCALE).toExponential(2);
152
  spheres[1].position.set(
153
+ parseFloat(document.getElementById('earth-x').value) * DISTANCE_SCALE,
154
+ parseFloat(document.getElementById('earth-y').value) * DISTANCE_SCALE,
155
+ parseFloat(document.getElementById('earth-z').value) * DISTANCE_SCALE
156
  );
157
  const earthVelocity = parseFloat(document.getElementById('earth-orbital-velocity').value);
158
+ spheres[1].userData.velocity.set(0, 0, earthVelocity * VELOCITY_SCALE);
159
+ document.getElementById('earth-velocity-scaled').textContent = (earthVelocity * VELOCITY_SCALE).toFixed(4);
160
  spheres[1].userData.centripetalScale = parseFloat(document.getElementById('earth-centripetal').value);
161
 
162
  // Update Mars
163
+ const marsMass = parseFloat(document.getElementById('mars-mass').value);
164
+ spheres[2].userData.mass = marsMass * MASS_SCALE;
165
+ document.getElementById('mars-mass-scaled').textContent = (marsMass * MASS_SCALE).toExponential(2);
166
  spheres[2].position.set(
167
+ parseFloat(document.getElementById('mars-x').value) * DISTANCE_SCALE,
168
+ parseFloat(document.getElementById('mars-y').value) * DISTANCE_SCALE,
169
+ parseFloat(document.getElementById('mars-z').value) * DISTANCE_SCALE
170
  );
171
  const marsVelocity = parseFloat(document.getElementById('mars-orbital-velocity').value);
172
+ spheres[2].userData.velocity.set(0, 0, marsVelocity * VELOCITY_SCALE);
173
+ document.getElementById('mars-velocity-scaled').textContent = (marsVelocity * VELOCITY_SCALE).toFixed(4);
174
  spheres[2].userData.centripetalScale = parseFloat(document.getElementById('mars-centripetal').value);
175
 
176
  // Update fluid interaction parameters
 
193
  });
194
 
195
  // Reset positions and velocities for Earth and Mars
196
+ spheres[1].position.set(149.6e6 * DISTANCE_SCALE, 0, 0);
197
+ spheres[1].userData.velocity.set(0, 0, 29.8 * VELOCITY_SCALE);
198
+ spheres[2].position.set(227.9e6 * DISTANCE_SCALE, 0, 0);
199
+ spheres[2].userData.velocity.set(0, 0, 24.1 * VELOCITY_SCALE);
200
+
201
+ // Reset sliders to default values
202
+ document.getElementById('sun-mass').value = 1.989e30;
203
+ document.getElementById('sun-x').value = 0;
204
+ document.getElementById('sun-y').value = 0;
205
+ document.getElementById('sun-z').value = 0;
206
+ document.getElementById('earth-mass').value = 5.972e24;
207
+ document.getElementById('earth-x').value = 149.6e6;
208
+ document.getElementById('earth-y').value = 0;
209
+ document.getElementById('earth-z').value = 0;
210
+ document.getElementById('earth-orbital-velocity').value = 29.8;
211
+ document.getElementById('earth-centripetal').value = 1;
212
+ document.getElementById('mars-mass').value = 6.417e23;
213
+ document.getElementById('mars-x').value = 227.9e6;
214
+ document.getElementById('mars-y').value = 0;
215
+ document.getElementById('mars-z').value = 0;
216
+ document.getElementById('mars-orbital-velocity').value = 24.1;
217
+ document.getElementById('mars-centripetal').value = 1;
218
+ document.getElementById('fluid-friction').value = 0.9;
219
+ document.getElementById('fluid-deflection').value = 0.1;
220
+
221
+ updateParams();
222
+ }
223
+
224
+ function saveSettings() {
225
+ const settings = {
226
+ sun: {
227
+ mass: parseFloat(document.getElementById('sun-mass').value),
228
+ position: [
229
+ parseFloat(document.getElementById('sun-x').value),
230
+ parseFloat(document.getElementById('sun-y').value),
231
+ parseFloat(document.getElementById('sun-z').value)
232
+ ],
233
+ orbital_velocity: 0
234
+ },
235
+ earth: {
236
+ mass: parseFloat(document.getElementById('earth-mass').value),
237
+ position: [
238
+ parseFloat(document.getElementById('earth-x').value),
239
+ parseFloat(document.getElementById('earth-y').value),
240
+ parseFloat(document.getElementById('earth-z').value)
241
+ ],
242
+ orbital_velocity: parseFloat(document.getElementById('earth-orbital-velocity').value)
243
+ },
244
+ mars: {
245
+ mass: parseFloat(document.getElementById('mars-mass').value),
246
+ position: [
247
+ parseFloat(document.getElementById('mars-x').value),
248
+ parseFloat(document.getElementById('mars-y').value),
249
+ parseFloat(document.getElementById('mars-z').value)
250
+ ],
251
+ orbital_velocity: parseFloat(document.getElementById('mars-orbital-velocity').value)
252
+ },
253
+ fluid_speed: FLUID_SPEED,
254
+ fluid_friction: FLUID_FRICTION,
255
+ fluid_deflection: FLUID_DEFLECTION
256
+ };
257
+
258
+ fetch('/api/save', {
259
+ method: 'POST',
260
+ headers: { 'Content-Type': 'application/json' },
261
+ body: JSON.stringify(settings)
262
+ })
263
+ .then(response => response.json())
264
+ .then(data => {
265
+ document.getElementById('status-message').textContent = data.message || data.status;
266
+ document.getElementById('status-message').style.color = data.status === 'success' ? '#4CAF50' : '#FF0000';
267
+ })
268
+ .catch(error => {
269
+ document.getElementById('status-message').textContent = 'Error saving settings';
270
+ document.getElementById('status-message').style.color = '#FF0000';
271
+ });
272
+ }
273
+
274
+ function loadSettings() {
275
+ fetch('/api/load')
276
+ .then(response => response.json())
277
+ .then(data => {
278
+ if (data.status === 'success') {
279
+ const params = data.params;
280
+ document.getElementById('sun-mass').value = params.sun.mass;
281
+ document.getElementById('sun-x').value = params.sun.position[0];
282
+ document.getElementById('sun-y').value = params.sun.position[1];
283
+ document.getElementById('sun-z').value = params.sun.position[2];
284
+ document.getElementById('earth-mass').value = params.earth.mass;
285
+ document.getElementById('earth-x').value = params.earth.position[0];
286
+ document.getElementById('earth-y').value = params.earth.position[1];
287
+ document.getElementById('earth-z').value = params.earth.position[2];
288
+ document.getElementById('earth-orbital-velocity').value = params.earth.orbital_velocity;
289
+ document.getElementById('mars-mass').value = params.mars.mass;
290
+ document.getElementById('mars-x').value = params.mars.position[0];
291
+ document.getElementById('mars-y').value = params.mars.position[1];
292
+ document.getElementById('mars-z').value = params.mars.position[2];
293
+ document.getElementById('mars-orbital-velocity').value = params.mars.orbital_velocity;
294
+ document.getElementById('fluid-friction').value = params.fluid_friction;
295
+ document.getElementById('fluid-deflection').value = params.fluid_deflection;
296
+
297
+ updateParams();
298
+ document.getElementById('status-message').textContent = 'Settings loaded successfully';
299
+ document.getElementById('status-message').style.color = '#4CAF50';
300
+ } else {
301
+ document.getElementById('status-message').textContent = data.message;
302
+ document.getElementById('status-message').style.color = '#FF0000';
303
+ }
304
+ })
305
+ .catch(error => {
306
+ document.getElementById('status-message').textContent = 'Error loading settings';
307
+ document.getElementById('status-message').style.color = '#FF0000';
308
+ });
309
  }
310
 
311
  function animate() {
 
320
  // Check for interactions with spheres
321
  spheres.forEach(sphere => {
322
  let distance = position.distanceTo(sphere.position);
323
+ let sphereRadius = sphere.geometry.parameters.radius + 0.5;
324
 
325
  if (distance < sphereRadius) {
326
  // Apply friction
 
351
  spheres.forEach((otherSphere, j) => {
352
  if (i !== j) {
353
  let distance = sphere.position.distanceTo(otherSphere.position);
354
+ if (distance > 0.1) {
355
  let direction = otherSphere.position.clone().sub(sphere.position).normalize();
356
  let force = (GRAVITY_CONSTANT * otherSphere.userData.mass) / (distance * distance);
357
  acceleration.add(direction.multiplyScalar(force * sphere.userData.centripetalScale));