Below is an updated version of the particle system with high-velocity animations and interactive toggles to control various parameters in real-time. The system now includes:
- High-velocity particles with adjustable speed limits.
- A user interface (using HTML sliders) to toggle parameters like speed, orbital strength, separation, cohesion, alignment, and more.
- Visual feedback to see how changes affect the particle behavior.
Here's the complete HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>High-Velocity Particle System with Toggles</title>
<style>
body { margin: 0; overflow: hidden; background: #000; }
canvas { display: block; }
#controls {
position: absolute;
top: 10px;
left: 10px;
background: rgba(255, 255, 255, 0.8);
padding: 10px;
border-radius: 5px;
font-family: Arial, sans-serif;
font-size: 14px;
}
#controls label {
display: block;
margin: 5px 0;
}
#controls input[type="range"] {
width: 200px;
}
</style>
</head>
<body>
<div id="controls">
<label>Max Speed: <span id="maxSpeedValue">0.15</span><br>
<input type="range" id="maxSpeed" min="0.01" max="1.0" step="0.01" value="0.15">
</label>
<label>Max Force: <span id="maxForceValue">0.01</span><br>
<input type="range" id="maxForce" min="0.001" max="0.1" step="0.001" value="0.01">
</label>
<label>Separation Distance: <span id="separationDistanceValue">1.0</span><br>
<input type="range" id="separationDistance" min="0.1" max="5.0" step="0.1" value="1.0">
</label>
<label>Separation Strength: <span id="separationStrengthValue">0.08</span><br>
<input type="range" id="separationStrength" min="0.01" max="0.5" step="0.01" value="0.08">
</label>
<label>Cohesion Strength: <span id="cohesionStrengthValue">0.005</span><br>
<input type="range" id="cohesionStrength" min="0.001" max="0.05" step="0.001" value="0.005">
</label>
<label>Alignment Strength: <span id="alignmentStrengthValue">0.03</span><br>
<input type="range" id="alignmentStrength" min="0.01" max="0.2" step="0.01" value="0.03">
</label>
<label>Orbital Strength: <span id="orbitalStrengthValue">0.02</span><br>
<input type="range" id="orbitalStrength" min="0.001" max="0.1" step="0.001" value="0.02">
</label>
<label>Flattening Strength: <span id="flatteningStrengthValue">0.01</span><br>
<input type="range" id="flatteningStrength" min="0.001" max="0.1" step="0.001" value="0.01">
</label>
<label>Direction Change Speed: <span id="directionChangeSpeedValue">0.001</span><br>
<input type="range" id="directionChangeSpeed" min="0.0001" max="0.01" step="0.0001" value="0.001">
</label>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r134/three.min.js"></script>
<script>
// Scene Setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Parameters (will be controlled by sliders)
let params = {
particleCount: 500,
maxSpeed: 0.15,
maxForce: 0.01,
separationDistance: 1.0,
boundary: 30,
alignmentStrength: 0.03,
cohesionStrength: 0.005,
separationStrength: 0.08,
orbitalStrength: 0.02,
flatteningStrength: 0.01,
directionChangeSpeed: 0.001
};
// Particle Data
const positions = new Float32Array(params.particleCount * 3);
const velocities = new Float32Array(params.particleCount * 3);
// Initialize particles with random positions and velocities
for(let i=0; i<params.particleCount; i++) {
// Start particles in a disk-like distribution
const radius = Math.random() * 20;
const angle = Math.random() * Math.PI * 2;
positions[i*3] = radius * Math.cos(angle);
positions[i*3 + 1] = (Math.random() - 0.5) * 2; // Thin disk in y-axis
positions[i*3 + 2] = radius * Math.sin(angle);
// Initial velocity perpendicular to radius for orbital motion
const speed = Math.sqrt(params.orbitalStrength / radius) * 5;
velocities[i*3] = -speed * Math.sin(angle);
velocities[i*3 + 1] = 0;
velocities[i*3 + 2] = speed * Math.cos(angle);
}
// BufferGeometry and Points
const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
geometry.setAttribute('velocity', new THREE.BufferAttribute(velocities, 3));
const material = new THREE.PointsMaterial({
color: 0xffaa00,
size: 0.1,
transparent: true,
opacity: 0.8
});
const points = new THREE.Points(geometry, material);
scene.add(points);
camera.position.set(0, 20, 40);
camera.lookAt(0, 0, 0);
// Helper functions
function limitVector(vec, max) {
const length = vec.length();
if(length > max) {
vec.multiplyScalar(max / length);
}
}
// General flock direction vector (will slowly change)
let flockDirection = new THREE.Vector3(1, 0, 0);
// Setup sliders
function updateSliderDisplay(sliderId, valueId) {
const slider = document.getElementById(sliderId);
const valueDisplay = document.getElementById(valueId);
valueDisplay.textContent = slider.value;
return slider;
}
const maxSpeedSlider = updateSliderDisplay('maxSpeed', 'maxSpeedValue');
const maxForceSlider = updateSliderDisplay('maxForce', 'maxForceValue');
const separationDistanceSlider = updateSliderDisplay('separationDistance', 'separationDistanceValue');
const separationStrengthSlider = updateSliderDisplay('separationStrength', 'separationStrengthValue');
const cohesionStrengthSlider = updateSliderDisplay('cohesionStrength', 'cohesionStrengthValue');
const alignmentStrengthSlider = updateSliderDisplay('alignmentStrength', 'alignmentStrengthValue');
const orbitalStrengthSlider = updateSliderDisplay('orbitalStrength', 'orbitalStrengthValue');
const flatteningStrengthSlider = updateSliderDisplay('flatteningStrength', 'flatteningStrengthValue');
const directionChangeSpeedSlider = updateSliderDisplay('directionChangeSpeed', 'directionChangeSpeedValue');
// Update params on slider change
maxSpeedSlider.addEventListener('input', () => {
params.maxSpeed = parseFloat(maxSpeedSlider.value);
document.getElementById('maxSpeedValue').textContent = params.maxSpeed;
});
maxForceSlider.addEventListener('input', () => {
params.maxForce = parseFloat(maxForceSlider.value);
document.getElementById('maxForceValue').textContent = params.maxForce;
});
separationDistanceSlider.addEventListener('input', () => {
params.separationDistance = parseFloat(separationDistanceSlider.value);
document.getElementById('separationDistanceValue').textContent = params.separationDistance;
});
separationStrengthSlider.addEventListener('input', () => {
params.separationStrength = parseFloat(separationStrengthSlider.value);
document.getElementById('separationStrengthValue').textContent = params.separationStrength;
});
cohesionStrengthSlider.addEventListener('input', () => {
params.cohesionStrength = parseFloat(cohesionStrengthSlider.value);
document.getElementById('cohesionStrengthValue').textContent = params.cohesionStrength;
});
alignmentStrengthSlider.addEventListener('input', () => {
params.alignmentStrength = parseFloat(alignmentStrengthSlider.value);
document.getElementById('alignmentStrengthValue').textContent = params.alignmentStrength;
});
orbitalStrengthSlider.addEventListener('input', () => {
params.orbitalStrength = parseFloat(orbitalStrengthSlider.value);
document.getElementById('orbitalStrengthValue').textContent = params.orbitalStrength;
});
flatteningStrengthSlider.addEventListener('input', () => {
params.flatteningStrength = parseFloat(flatteningStrengthSlider.value);
document.getElementById('flatteningStrengthValue').textContent = params.flatteningStrength;
});
directionChangeSpeedSlider.addEventListener('input', () => {
params.directionChangeSpeed = parseFloat(directionChangeSpeedSlider.value);
document.getElementById('directionChangeSpeedValue').textContent = params.directionChangeSpeed;
});
function animate() {
requestAnimationFrame(animate);
const pos = geometry.attributes.position.array;
const vel = geometry.attributes.velocity.array;
// 1. Compute center of mass and average velocity
let centerOfMass = new THREE.Vector3(0,0,0);
let avgVelocity = new THREE.Vector3(0,0,0);
for(let i=0; i<params.particleCount; i++) {
centerOfMass.x += pos[i*3];
centerOfMass.y += pos[i*3 + 1];
centerOfMass.z += pos[i*3 + 2];
avgVelocity.x += vel[i*3];
avgVelocity.y += vel[i*3 + 1];
avgVelocity.z += vel[i*3 + 2];
}
centerOfMass.multiplyScalar(1/params.particleCount);
avgVelocity.multiplyScalar(1/params.particleCount);
avgVelocity.normalize();
// Slowly change flockDirection towards a random target direction
const randomTarget = new THREE.Vector3(
Math.sin(Date.now() * 0.0001),
Math.cos(Date.now() * 0.00013),
Math.sin(Date.now() * 0.00017)
).normalize();
flockDirection.lerp(randomTarget, params.directionChangeSpeed);
flockDirection.normalize();
// 2. Update each particle
for(let i=0; i<params.particleCount; i++) {
const i3 = i*3;
const position = new THREE.Vector3(pos[i3], pos[i3+1], pos[i3+2]);
const velocity = new THREE.Vector3(vel[i3], vel[i3+1], vel[i3+2]);
// --- Separation: steer away from nearby particles ---
let separationForce = new THREE.Vector3(0,0,0);
let neighbors = 0;
for(let j=0; j<params.particleCount; j++) {
if(i === j) continue;
const j3 = j*3;
const otherPos = new THREE.Vector3(pos[j3], pos[j3+1], pos[j3+2]);
const distance = position.distanceTo(otherPos);
if(distance < params.separationDistance && distance > 0) {
let diff = position.clone().sub(otherPos).normalize().divideScalar(distance);
separationForce.add(diff);
neighbors++;
}
}
if(neighbors > 0) {
separationForce.divideScalar(neighbors);
separationForce.setLength(params.separationStrength);
}
// --- Cohesion: steer towards center of mass ---
let cohesionForce = centerOfMass.clone().sub(position);
const distanceToCenter = cohesionForce.length();
cohesionForce.normalize().multiplyScalar(params.cohesionStrength * distanceToCenter);
// --- Alignment: steer towards flockDirection ---
let alignmentForce = flockDirection.clone().sub(velocity).multiplyScalar(params.alignmentStrength);
// --- Orbital Force: simulate gravitational orbit ---
const toCenter = centerOfMass.clone().sub(position);
const distToCenter = toCenter.length();
if(distToCenter > 0) {
// Gravitational attraction
const gravityForce = toCenter.normalize().multiplyScalar(params.orbitalStrength / (distToCenter * distToCenter));
// Tangential velocity (orbital motion)
const tangential = new THREE.Vector3(-toCenter.z, 0, toCenter.x).normalize();
const orbitalForce = tangential.multiplyScalar(Math.sqrt(params.orbitalStrength / distToCenter));
velocity.add(gravityForce);
velocity.add(orbitalForce);
}
// --- Flattening Force: keep particles in a disk ---
const flatteningForce = new THREE.Vector3(0, -position.y, 0).multiplyScalar(params.flatteningStrength);
// Sum forces
velocity.add(separationForce);
velocity.add(cohesionForce);
velocity.add(alignmentForce);
velocity.add(flatteningForce);
// Limit speed
limitVector(velocity, params.maxSpeed);
// Update position and velocity arrays
pos[i3] += velocity.x;
pos[i3+1] += velocity.y;
pos[i3+2] += velocity.z;
vel[i3] = velocity.x;
vel[i3+1] = velocity.y;
vel[i3+2] = velocity.z;
// Boundary wrap-around
if(pos[i3] > params.boundary) pos[i3] = -params.boundary;
else if(pos[i3] < -params.boundary) pos[i3] = params.boundary;
if(pos[i3+1] > params.boundary) pos[i3+1] = -params.boundary;
else if(pos[i3+1] < -params.boundary) pos[i3+1] = params.boundary;
if(pos[i3+2] > params.boundary) pos[i3+2] = -params.boundary;
else if(pos[i3+2] < -params.boundary) pos[i3+2] = params.boundary;
}
geometry.attributes.position.needsUpdate = true;
geometry.attributes.velocity.needsUpdate = true;
renderer.render(scene, camera);
}
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate();
</script>
</body>
</html>