broadfield-dev commited on
Commit
9cb0542
·
verified ·
1 Parent(s): 4380157

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +93 -51
static/script.js CHANGED
@@ -5,10 +5,11 @@ let fluidParticles = [];
5
  let simulationRunning = false;
6
 
7
  const PARTICLE_COUNT = 5000;
8
- const SPACE_SIZE = 20;
9
  const FLUID_SPEED = 0.1;
10
- const FRICTION_FACTOR = 0.9;
11
- const GRAVITY_CONSTANT = 0.1;
 
12
 
13
  init();
14
  animate();
@@ -17,7 +18,7 @@ function init() {
17
  // Scene setup
18
  scene = new THREE.Scene();
19
  camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
20
- camera.position.set(0, 10, 20);
21
 
22
  renderer = new THREE.WebGLRenderer({ antialias: true });
23
  renderer.setSize(window.innerWidth - 300, window.innerHeight);
@@ -27,30 +28,40 @@ function init() {
27
  controls.enableDamping = true;
28
  controls.dampingFactor = 0.05;
29
 
30
- // Add spheres
31
- const brownGeometry = new THREE.SphereGeometry(0.5, 32, 32);
32
- const brownMaterial = new THREE.MeshBasicMaterial({ color: 0x8B4513 });
33
- const brownSphere = new THREE.Mesh(brownGeometry, brownMaterial);
34
- brownSphere.position.set(0, 0, 0);
35
- brownSphere.userData = { mass: 92 };
36
- scene.add(brownSphere);
37
- spheres.push(brownSphere);
38
-
39
- const greenGeometry = new THREE.SphereGeometry(0.4, 32, 32);
40
- const greenMaterial = new THREE.MeshBasicMaterial({ color: 0x00FF00 });
41
- const greenSphere = new THREE.Mesh(greenGeometry, greenMaterial);
42
- greenSphere.position.set(5, 0, 5);
43
- greenSphere.userData = { mass: 29 };
44
- scene.add(greenSphere);
45
- spheres.push(greenSphere);
46
-
47
- const redGeometry = new THREE.SphereGeometry(0.3, 32, 32);
48
- const redMaterial = new THREE.MeshBasicMaterial({ color: 0xFF0000 });
49
- const redSphere = new THREE.Mesh(redGeometry, redMaterial);
50
- redSphere.position.set(-5, 0, -5);
51
- redSphere.userData = { mass: 10 };
52
- scene.add(redSphere);
53
- spheres.push(redSphere);
 
 
 
 
 
 
 
 
 
 
54
 
55
  // Add fluid particles
56
  const particleGeometry = new THREE.SphereGeometry(0.05, 8, 8);
@@ -89,14 +100,21 @@ function init() {
89
  resetSimulation();
90
  });
91
 
92
- // Update sphere positions and masses from sliders
93
- ['brown', 'green', 'red'].forEach(color => {
94
- document.getElementById(`${color}-mass`).addEventListener('input', updateParams);
95
- document.getElementById(`${color}-x`).addEventListener('input', updateParams);
96
- document.getElementById(`${color}-y`).addEventListener('input', updateParams);
97
- document.getElementById(`${color}-z`).addEventListener('input', updateParams);
 
 
 
 
98
  });
99
 
 
 
 
100
  // Handle window resize
101
  window.addEventListener('resize', () => {
102
  camera.aspect = (window.innerWidth - 300) / window.innerHeight;
@@ -106,26 +124,39 @@ function init() {
106
  }
107
 
108
  function updateParams() {
109
- spheres[0].userData.mass = parseFloat(document.getElementById('brown-mass').value);
 
110
  spheres[0].position.set(
111
- parseFloat(document.getElementById('brown-x').value),
112
- parseFloat(document.getElementById('brown-y').value),
113
- parseFloat(document.getElementById('brown-z').value)
114
  );
115
 
116
- spheres[1].userData.mass = parseFloat(document.getElementById('green-mass').value);
 
117
  spheres[1].position.set(
118
- parseFloat(document.getElementById('green-x').value),
119
- parseFloat(document.getElementById('green-y').value),
120
- parseFloat(document.getElementById('green-z').value)
121
  );
 
 
 
122
 
123
- spheres[2].userData.mass = parseFloat(document.getElementById('red-mass').value);
 
124
  spheres[2].position.set(
125
- parseFloat(document.getElementById('red-x').value),
126
- parseFloat(document.getElementById('red-y').value),
127
- parseFloat(document.getElementById('red-z').value)
128
  );
 
 
 
 
 
 
 
129
  }
130
 
131
  function resetSimulation() {
@@ -141,6 +172,12 @@ function resetSimulation() {
141
  (Math.random() - 0.5) * FLUID_SPEED
142
  );
143
  });
 
 
 
 
 
 
144
  }
145
 
146
  function animate() {
@@ -159,11 +196,11 @@ function animate() {
159
 
160
  if (distance < sphereRadius) {
161
  // Apply friction
162
- velocity.multiplyScalar(FRICTION_FACTOR);
163
 
164
  // Apply gravitational deflection
165
  let direction = sphere.position.clone().sub(position).normalize();
166
- let forceMagnitude = (GRAVITY_CONSTANT * sphere.userData.mass) / (distance * distance);
167
  let force = direction.multiplyScalar(forceMagnitude);
168
  velocity.add(force);
169
  }
@@ -178,8 +215,10 @@ function animate() {
178
  if (Math.abs(position.z) > SPACE_SIZE / 2) position.z = -Math.sign(position.z) * SPACE_SIZE / 2;
179
  });
180
 
181
- // Update sphere positions (gravitational interaction between spheres)
182
  spheres.forEach((sphere, i) => {
 
 
183
  let acceleration = new THREE.Vector3();
184
  spheres.forEach((otherSphere, j) => {
185
  if (i !== j) {
@@ -187,11 +226,14 @@ function animate() {
187
  if (distance > 0.1) { // Avoid division by zero
188
  let direction = otherSphere.position.clone().sub(sphere.position).normalize();
189
  let force = (GRAVITY_CONSTANT * otherSphere.userData.mass) / (distance * distance);
190
- acceleration.add(direction.multiplyScalar(force));
191
  }
192
  }
193
  });
194
- sphere.position.add(acceleration);
 
 
 
195
  });
196
  }
197
 
 
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();
 
18
  // Scene setup
19
  scene = new THREE.Scene();
20
  camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
21
+ camera.position.set(0, 20, 40);
22
 
23
  renderer = new THREE.WebGLRenderer({ antialias: true });
24
  renderer.setSize(window.innerWidth - 300, window.innerHeight);
 
28
  controls.enableDamping = true;
29
  controls.dampingFactor = 0.05;
30
 
31
+ // Add Sun
32
+ const sunGeometry = new THREE.SphereGeometry(1, 32, 32);
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
+
40
+ // Add Earth
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);
51
+ spheres.push(earth);
52
+
53
+ // Add Mars
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);
64
+ spheres.push(mars);
65
 
66
  // Add fluid particles
67
  const particleGeometry = new THREE.SphereGeometry(0.05, 8, 8);
 
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);
106
+ document.getElementById(`${body}-x`).addEventListener('input', updateParams);
107
+ document.getElementById(`${body}-y`).addEventListener('input', updateParams);
108
+ document.getElementById(`${body}-z`).addEventListener('input', updateParams);
109
+ if (body !== 'sun') {
110
+ document.getElementById(`${body}-orbital-velocity`).addEventListener('input', updateParams);
111
+ document.getElementById(`${body}-centripetal`).addEventListener('input', updateParams);
112
+ }
113
  });
114
 
115
+ document.getElementById('fluid-friction').addEventListener('input', updateParams);
116
+ document.getElementById('fluid-deflection').addEventListener('input', updateParams);
117
+
118
  // Handle window resize
119
  window.addEventListener('resize', () => {
120
  camera.aspect = (window.innerWidth - 300) / window.innerHeight;
 
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
158
+ FLUID_FRICTION = parseFloat(document.getElementById('fluid-friction').value);
159
+ FLUID_DEFLECTION = parseFloat(document.getElementById('fluid-deflection').value);
160
  }
161
 
162
  function resetSimulation() {
 
172
  (Math.random() - 0.5) * FLUID_SPEED
173
  );
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() {
 
196
 
197
  if (distance < sphereRadius) {
198
  // Apply friction
199
+ velocity.multiplyScalar(FLUID_FRICTION);
200
 
201
  // Apply gravitational deflection
202
  let direction = sphere.position.clone().sub(position).normalize();
203
+ let forceMagnitude = (FLUID_DEFLECTION * sphere.userData.mass) / (distance * distance);
204
  let force = direction.multiplyScalar(forceMagnitude);
205
  velocity.add(force);
206
  }
 
215
  if (Math.abs(position.z) > SPACE_SIZE / 2) position.z = -Math.sign(position.z) * SPACE_SIZE / 2;
216
  });
217
 
218
+ // Update sphere positions (gravitational interaction and orbital dynamics)
219
  spheres.forEach((sphere, i) => {
220
+ if (i === 0) return; // Sun is stationary
221
+
222
  let acceleration = new THREE.Vector3();
223
  spheres.forEach((otherSphere, j) => {
224
  if (i !== j) {
 
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));
230
  }
231
  }
232
  });
233
+
234
+ // Update velocity and position
235
+ sphere.userData.velocity.add(acceleration);
236
+ sphere.position.add(sphere.userData.velocity);
237
  });
238
  }
239