Spaces:
Runtime error
Runtime error
File size: 7,789 Bytes
3bd9648 |
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 |
// static/script.js
let scene, camera, renderer, controls;
let spheres = [];
let fluidParticles = [];
let simulationRunning = false;
const PARTICLE_COUNT = 5000;
const SPACE_SIZE = 20;
const FLUID_SPEED = 0.1;
const FRICTION_FACTOR = 0.9;
const GRAVITY_CONSTANT = 0.1;
init();
animate();
function init() {
// Scene setup
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 10, 20);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth - 300, window.innerHeight);
document.getElementById('scene-container').appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
// Add spheres
const brownGeometry = new THREE.SphereGeometry(0.5, 32, 32);
const brownMaterial = new THREE.MeshBasicMaterial({ color: 0x8B4513 });
const brownSphere = new THREE.Mesh(brownGeometry, brownMaterial);
brownSphere.position.set(0, 0, 0);
brownSphere.userData = { mass: 92 };
scene.add(brownSphere);
spheres.push(brownSphere);
const greenGeometry = new THREE.SphereGeometry(0.4, 32, 32);
const greenMaterial = new THREE.MeshBasicMaterial({ color: 0x00FF00 });
const greenSphere = new THREE.Mesh(greenGeometry, greenMaterial);
greenSphere.position.set(5, 0, 5);
greenSphere.userData = { mass: 29 };
scene.add(greenSphere);
spheres.push(greenSphere);
const redGeometry = new THREE.SphereGeometry(0.3, 32, 32);
const redMaterial = new THREE.MeshBasicMaterial({ color: 0xFF0000 });
const redSphere = new THREE.Mesh(redGeometry, redMaterial);
redSphere.position.set(-5, 0, -5);
redSphere.userData = { mass: 10 };
scene.add(redSphere);
spheres.push(redSphere);
// Add fluid particles
const particleGeometry = new THREE.SphereGeometry(0.05, 8, 8);
const particleMaterial = new THREE.MeshBasicMaterial({ color: 0x00BFFF, transparent: true, opacity: 0.5 });
for (let i = 0; i < PARTICLE_COUNT; i++) {
const particle = new THREE.Mesh(particleGeometry, particleMaterial);
particle.position.set(
(Math.random() - 0.5) * SPACE_SIZE,
(Math.random() - 0.5) * SPACE_SIZE,
(Math.random() - 0.5) * SPACE_SIZE
);
particle.userData = {
velocity: new THREE.Vector3(
(Math.random() - 0.5) * FLUID_SPEED,
(Math.random() - 0.5) * FLUID_SPEED,
(Math.random() - 0.5) * FLUID_SPEED
)
};
scene.add(particle);
fluidParticles.push(particle);
}
// Add grid helper for reference
const gridHelper = new THREE.GridHelper(SPACE_SIZE, 20);
gridHelper.position.y = -SPACE_SIZE / 2;
scene.add(gridHelper);
// Event listeners for controls
document.getElementById('start-btn').addEventListener('click', () => {
simulationRunning = true;
updateParams();
});
document.getElementById('reset-btn').addEventListener('click', () => {
simulationRunning = false;
resetSimulation();
});
// Update sphere positions and masses from sliders
['brown', 'green', 'red'].forEach(color => {
document.getElementById(`${color}-mass`).addEventListener('input', updateParams);
document.getElementById(`${color}-x`).addEventListener('input', updateParams);
document.getElementById(`${color}-y`).addEventListener('input', updateParams);
document.getElementById(`${color}-z`).addEventListener('input', updateParams);
});
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = (window.innerWidth - 300) / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth - 300, window.innerHeight);
});
}
function updateParams() {
spheres[0].userData.mass = parseFloat(document.getElementById('brown-mass').value);
spheres[0].position.set(
parseFloat(document.getElementById('brown-x').value),
parseFloat(document.getElementById('brown-y').value),
parseFloat(document.getElementById('brown-z').value)
);
spheres[1].userData.mass = parseFloat(document.getElementById('green-mass').value);
spheres[1].position.set(
parseFloat(document.getElementById('green-x').value),
parseFloat(document.getElementById('green-y').value),
parseFloat(document.getElementById('green-z').value)
);
spheres[2].userData.mass = parseFloat(document.getElementById('red-mass').value);
spheres[2].position.set(
parseFloat(document.getElementById('red-x').value),
parseFloat(document.getElementById('red-y').value),
parseFloat(document.getElementById('red-z').value)
);
}
function resetSimulation() {
fluidParticles.forEach(particle => {
particle.position.set(
(Math.random() - 0.5) * SPACE_SIZE,
(Math.random() - 0.5) * SPACE_SIZE,
(Math.random() - 0.5) * SPACE_SIZE
);
particle.userData.velocity.set(
(Math.random() - 0.5) * FLUID_SPEED,
(Math.random() - 0.5) * FLUID_SPEED,
(Math.random() - 0.5) * FLUID_SPEED
);
});
}
function animate() {
requestAnimationFrame(animate);
if (simulationRunning) {
// Update fluid particles
fluidParticles.forEach(particle => {
let position = particle.position;
let velocity = particle.userData.velocity;
// Check for interactions with spheres
spheres.forEach(sphere => {
let distance = position.distanceTo(sphere.position);
let sphereRadius = sphere.geometry.parameters.radius + 0.5; // Interaction radius
if (distance < sphereRadius) {
// Apply friction
velocity.multiplyScalar(FRICTION_FACTOR);
// Apply gravitational deflection
let direction = sphere.position.clone().sub(position).normalize();
let forceMagnitude = (GRAVITY_CONSTANT * sphere.userData.mass) / (distance * distance);
let force = direction.multiplyScalar(forceMagnitude);
velocity.add(force);
}
});
// Update position
position.add(velocity);
// Boundary conditions (wrap around)
if (Math.abs(position.x) > SPACE_SIZE / 2) position.x = -Math.sign(position.x) * SPACE_SIZE / 2;
if (Math.abs(position.y) > SPACE_SIZE / 2) position.y = -Math.sign(position.y) * SPACE_SIZE / 2;
if (Math.abs(position.z) > SPACE_SIZE / 2) position.z = -Math.sign(position.z) * SPACE_SIZE / 2;
});
// Update sphere positions (gravitational interaction between spheres)
spheres.forEach((sphere, i) => {
let acceleration = new THREE.Vector3();
spheres.forEach((otherSphere, j) => {
if (i !== j) {
let distance = sphere.position.distanceTo(otherSphere.position);
if (distance > 0.1) { // Avoid division by zero
let direction = otherSphere.position.clone().sub(sphere.position).normalize();
let force = (GRAVITY_CONSTANT * otherSphere.userData.mass) / (distance * distance);
acceleration.add(direction.multiplyScalar(force));
}
}
});
sphere.position.add(acceleration);
});
}
controls.update();
renderer.render(scene, camera);
} |