html canvas const particleTypes = [ { type: 0, forceMul:3, m:1, s:10, colorHSL: [0,0.5,…
- User
html canvas
const particleTypes = [ { type: 0, forceMul:3, m:1, s:10, colorHSL: [0,0.5, 0.5], connections: [2, 1] }, { type: 1, forceMul:0.5, m:1, s:10, colorHSL: [0.2,0.8, 0.7], connections: [0, 6, 2] }, { type: 2, forceMul:1, m:1, s:10, colorHSL: [0.5,0.4, 0.8], connections: [1, 2, 3] }, { type: 3, forceMul:1, m:1, s:20, colorHSL: [0.7,0.5, 0.9], connections: [2, 4] }, { type: 4, forceMul:1, m:1, s:10, colorHSL: [0.2,0.0, 0.9], connections: [1, 1, 1, 1, 1] }, ] and x,y, xx, vy,
connections: [2, 1] mean possible number of connections to other types. // Type 0: Can form 2 connections to 0. if <2 connections has stronger magnet force. if > became anti-magnet force. Type 0: Can form 1 connections to 1 Type 0: Can form 0 connections to other
forceMul - multiplier of global magnetic force parameter
datgui с параметрами
spawn rate притяжения и отталкивания начальной скорости новых частиц массы новых частиц размера новых частиц
Assistant
HTML Canvas Particle System with Connection Rules
Here's a complete implementation of a particle system with the connection rules you specified, using HTML Canvas and dat.GUI for parameter control:
<!DOCTYPE html> <html> <head> <title>Particle Connection System</title> <style> body { margin: 0; overflow: hidden; } canvas { display: block; } </style> </head> <body> <canvas id="canvas"></canvas> <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.9/dat.gui.min.js"></script> <script> // Canvas setup const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; // Particle types configuration const particleTypes = [ { type: 0, forceMul: 3, m: 1, s: 10, colorHSL: [0, 0.5, 0.5], connections: [2, 1, 0, 0, 0, 0] }, { type: 1, forceMul: 0.5, m: 1, s: 10, colorHSL: [0.2, 0.8, 0.7], connections: [0, 6, 2, 0, 0, 0] }, { type: 2, forceMul: 1, m: 1, s: 10, colorHSL: [0.5, 0.4, 0.8], connections: [1, 2, 3, 0, 0, 0] }, { type: 3, forceMul: 1, m: 1, s: 20, colorHSL: [0.7, 0.5, 0.9], connections: [0, 0, 2, 4, 0, 0] }, { type: 4, forceMul: 1, m: 1, s: 10, colorHSL: [0.2, 0.0, 0.9], connections: [0, 1, 1, 1, 1, 1] }, ]; // System parameters const params = { spawnRate: 10, attractionForce: 0.1, repulsionForce: 0.5, initialVelocity: 2, particleMass: 1, particleSize: 10, maxParticles: 200, connectionDistance: 150, maxConnections: 5, reset: function() { particles = []; } }; // GUI setup const gui = new dat.GUI(); gui.add(params, 'spawnRate', 1, 50).name('Spawn Rate'); gui.add(params, 'attractionForce', 0.01, 1).name('Attraction Force'); gui.add(params, 'repulsionForce', 0.01, 1).name('Repulsion Force'); gui.add(params, 'initialVelocity', 0, 10).name('Initial Velocity'); gui.add(params, 'particleMass', 0.1, 5).name('Particle Mass'); gui.add(params, 'particleSize', 5, 30).name('Particle Size'); gui.add(params, 'maxParticles', 50, 500).name('Max Particles'); gui.add(params, 'connectionDistance', 50, 300).name('Connection Distance'); gui.add(params, 'reset').name('Reset Simulation'); // Particle class class Particle { constructor(x, y, type) { this.x = x || Math.random() * canvas.width; this.y = y || Math.random() * canvas.height; this.vx = (Math.random() - 0.5) * params.initialVelocity; this.vy = (Math.random() - 0.5) * params.initialVelocity; this.type = type || Math.floor(Math.random() * particleTypes.length); this.mass = params.particleMass; this.size = params.particleSize; this.connections = []; this.color = this.getColor(); } getColor() { const type = particleTypes[this.type]; return `hsl(${type.colorHSL[0] * 360}, ${type.colorHSL[1] * 100}%, ${type.colorHSL[2] * 100}%)`; } update() { // Apply velocity this.x += this.vx; this.y += this.vy; // Boundary checking if (this.x < 0) { this.x = 0; this.vx *= -0.5; } else if (this.x > canvas.width) { this.x = canvas.width; this.vx *= -0.5; } if (this.y < 0) { this.y = 0; this.vy *= -0.5; } else if (this.y > canvas.height) { this.y = canvas.height; this.vy *= -0.5; } // Apply friction this.vx *= 0.99; this.vy *= 0.99; } draw() { ctx.beginPath(); ctx.arc(this.x, this.y, this.size / 2, 0, Math.PI * 2); ctx.fillStyle = this.color; ctx.fill(); ctx.closePath(); } } // System state let particles = []; let lastSpawnTime = 0; // Main animation loop function animate(timestamp) { // Clear canvas ctx.clearRect(0, 0, canvas.width, canvas.height); // Spawn new particles if (particles.length < params.maxParticles) { const now = Date.now(); if (now - lastSpawnTime > 1000 / params.spawnRate) { particles.push(new Particle()); lastSpawnTime = now; } } // Update and draw particles for (let i = 0; i < particles.length; i++) { particles[i].update(); particles[i].draw(); } // Handle connections handleConnections(); // Draw connections drawConnections(); requestAnimationFrame(animate); } function handleConnections() { // Reset connections for (const particle of particles) { particle.connections = []; } // Check all possible pairs for (let i = 0; i < particles.length; i++) { for (let j = i + 1; j < particles.length; j++) { const p1 = particles[i]; const p2 = particles[j]; // Calculate distance const dx = p2.x - p1.x; const dy = p2.y - p1.y; const distance = Math.sqrt(dx * dx + dy * dy); // Check if within connection range if (distance < params.connectionDistance) { const type1 = p1.type; const type2 = p2.type; // Get connection rules const maxConnections1 = particleTypes[type1].connections[type2]; const maxConnections2 = particleTypes[type2].connections[type1]; // Count current connections of each type const currentConnections1 = p1.connections.filter(c => c.type === type2).length; const currentConnections2 = p2.connections.filter(c => c.type === type1).length; // Check if connection is allowed if ((maxConnections1 === 0 || currentConnections1 < maxConnections1) && (maxConnections2 === 0 || currentConnections2 < maxConnections2)) { // Add connection p1.connections.push({ particle: p2, distance }); p2.connections.push({ particle: p1, distance }); // Apply forces based on connection rules applyConnectionForces(p1, p2, distance); } } } } } function applyConnectionForces(p1, p2, distance) { const type1 = p1.type; const type2 = p2.type; // Get force multipliers const forceMul1 = particleTypes[type1].forceMul; const forceMul2 = particleTypes[type2].forceMul; // Calculate direction const dx = p2.x - p1.x; const dy = p2.y - p1.y; const distanceNormalized = distance / params.connectionDistance; // Calculate force magnitude let force = 0; // If particles are too close, repel if (distance < p1.size/2 + p2.size/2) { force = -params.repulsionForce * (1 - distanceNormalized); } // Otherwise, apply attraction based on connection rules else { // Get connection rules const maxConnections1 = particleTypes[type1].connections[type2]; const maxConnections2 = particleTypes[type2].connections[type1]; // Count current connections const currentConnections1 = p1.connections.filter(c => c.particle.type === type2).length; const currentConnections2 = p2.connections.filter(c => c.particle.type === type1).length; // Calculate desired connections const desiredConnections1 = maxConnections1 > 0 ? maxConnections1 : 0; const desiredConnections2 = maxConnections2 > 0 ? maxConnections2 : 0; // Calculate force based on how many connections we want const connectionRatio1 = currentConnections1 / desiredConnections1; const connectionRatio2 = currentConnections2 / desiredConnections2; // If we have fewer connections than desired, attract if (currentConnections1 < desiredConnections1 || currentConnections2 < desiredConnections2) { force = params.attractionForce * (1 - distanceNormalized) * (forceMul1 + forceMul2) / 2; } // If we have more connections than desired, repel else { force = -params.attractionForce * (1 - distanceNormalized) * (forceMul1 + forceMul2) / 2; } } // Apply force to both particles const fx = (dx / distance) * force; const fy = (dy / distance) * force; p1.vx += fx / p1.mass; p1.vy += fy / p1.mass; p2.vx -= fx / p2.mass; p2.vy -= fy / p2.mass; } function drawConnections() { for (const particle of particles) { for (const connection of particle.connections) { const other = connection.particle; // Draw line between connected particles ctx.beginPath(); ctx.moveTo(particle.x, particle.y); ctx.lineTo(other.x, other.y); // Line color based on connection strength const distanceRatio = connection.distance / params.connectionDistance; const alpha = 0.5 * (1 - distanceRatio); ctx.strokeStyle = `rgba(255, 255, 255, ${alpha})`; ctx.lineWidth = 1; ctx.stroke(); } } } // Handle window resize window.addEventListener('resize', () => { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }); // Start animation animate(); </script> </body> </html>Key Features:
-
Particle Types System:
- Each particle type has specific connection rules
- Connection rules determine how many connections each type can form with others
- Force multipliers affect the strength of attractions/repulsions
-
Connection Logic:
- Particles can form connections based on their types
- If a particle has fewer connections than allowed, it attracts others
- If it has more connections than allowed, it repels others
- Connection rules are bidirectional but can be asymmetric
-
Physics Simulation:
- Basic velocity and boundary collision handling
- Connection forces that depend on distance and connection rules
- Repulsion when particles get too close
-
dat.GUI Controls:
- Spawn rate control
- Attraction and repulsion force adjustments
- Initial velocity and mass settings
- Particle size control
- Connection distance parameter
- Reset button to clear the simulation
-
Visualization:
- Particles are colored based on their type
- Connections are drawn as lines with opacity based on distance
The system creates an interesting dynamic where particles form structures based on their connection rules, with different types having different behaviors and interaction patterns.
-
- User
apply 0.1 multiplayer or forces between of connected particles that are close to each other so they not oscillate bouncing
Assistant
To reduce the oscillation and bouncing between connected particles, we'll modify the force calculation to apply a damping factor (0.1 multiplier) when particles are close to each other. Here's the updated
applyConnectionForcesfunction:function applyConnectionForces(p1, p2, distance) { const type1 = p1.type; const type2 = p2.type; // Get force multipliers const forceMul1 = particleTypes[type1].forceMul; const forceMul2 = particleTypes[type2].forceMul; // Calculate direction const dx = p2.x - p1.x; const dy = p2.y - p1.y; const distanceNormalized = distance / params.connectionDistance; // Calculate force magnitude let force = 0; // If particles are too close, apply damped repulsion if (distance < p1.size/2 + p2.size/2) { // Apply 0.1 multiplier to reduce bouncing force = -params.repulsionForce * (1 - distanceNormalized) * 0.1; } // Otherwise, apply attraction based on connection rules else { // Get connection rules const maxConnections1 = particleTypes[type1].connections[type2]; const maxConnections2 = particleTypes[type2].connections[type1]; // Count current connections const currentConnections1 = p1.connections.filter(c => c.particle.type === type2).length; const currentConnections2 = p2.connections.filter(c => c.particle.type === type1).length; // Calculate desired connections const desiredConnections1 = maxConnections1 > 0 ? maxConnections1 : 0; const desiredConnections2 = maxConnections2 > 0 ? maxConnections2 : 0; // Calculate force based on how many connections we want const connectionRatio1 = currentConnections1 / desiredConnections1; const connectionRatio2 = currentConnections2 / desiredConnections2; // If we have fewer connections than desired, attract if (currentConnections1 < desiredConnections1 || currentConnections2 < desiredConnections2) { force = params.attractionForce * (1 - distanceNormalized) * (forceMul1 + forceMul2) / 2; } // If we have more connections than desired, repel else { // Apply 0.1 multiplier to reduce bouncing force = -params.attractionForce * (1 - distanceNormalized) * (forceMul1 + forceMul2) / 2 * 0.1; } } // Apply force to both particles const fx = (dx / distance) * force; const fy = (dy / distance) * force; p1.vx += fx / p1.mass; p1.vy += fy / p1.mass; p2.vx -= fx / p2.mass; p2.vy -= fy / p2.mass; // Add damping to connected particles to reduce oscillation if (distance < params.connectionDistance) { // Apply damping to relative velocity const vx1 = p1.vx, vy1 = p1.vy; const vx2 = p2.vx, vy2 = p2.vy; // Calculate relative velocity in the direction of the connection const relVx = vx2 - vx1; const relVy = vy2 - vy1; // Project relative velocity onto the connection vector const connectionDirX = dx / distance; const connectionDirY = dy / distance; const relVelAlongConnection = relVx * connectionDirX + relVy * connectionDirY; // Apply damping force proportional to relative velocity const dampingForce = -relVelAlongConnection * 0.1; // 0.1 damping coefficient const dfx = connectionDirX * dampingForce; const dfy = connectionDirY * dampingForce; p1.vx += dfx / p1.mass; p1.vy += dfy / p1.mass; p2.vx -= dfx / p2.mass; p2.vy -= dfy / p2.mass; } }Key changes made:
-
Reduced repulsion force when particles are very close (colliding) by multiplying by 0.1
-
Reduced attraction force when particles have too many connections by multiplying by 0.1
-
Added velocity damping between connected particles to reduce oscillation:
- Calculates relative velocity between connected particles
- Projects this velocity onto the connection vector
- Applies a damping force opposite to this relative velocity
- The damping coefficient (0.1) can be adjusted for more/less damping
-
Maintained all other connection logic including:
- Different force multipliers for different particle types
- Connection count limits
- Distance-based force scaling
This should significantly reduce the bouncing and oscillation between connected particles while maintaining the overall connection behavior of the system. The particles will still form connections based on their types, but the interactions will be smoother and more stable.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.