Skip to content
TheChatArchiveThe Chat Archive
Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written)Hugging Face OpenAssistant Conversations (oasst2)

Implement a basic particle collision algorithm in Javascript using the HTML Canvas.

In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
  1. User

    Implement a basic particle collision algorithm in Javascript using the HTML Canvas.

  2. Assistant

    Sure, input the mathematical equations which i will use to provide a simulation, as well as the particles which will be used, and the goal to design this for.

  3. User

    No, I'd expect you to do that for me. The purpose is sufficiently clear, since I'm just asking for a basic exemplary implementation.

    Again: Implement a basic particle collision algorithm in Javascript using the HTML Canvas.

  4. Assistant
    <!DOCTYPE html>
    <html>
    <head>
      <style>
        canvas {
          border: 1px solid black;
        }
      </style>
    </head>
    <body>
      <canvas id="canvas" width="800" height="600"></canvas>
      <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
    
        class Particle {
          constructor(x, y, radius) {
            this.x = x;
            this.y = y;
            this.radius = radius;
            this.vx = Math.random() * 2 - 1;
            this.vy = Math.random() * 2 - 1;
          }
    
          update() {
            this.x += this.vx;
            this.y += this.vy;
    
            if (this.x - this.radius < 0 || this.x + this.radius > canvas.width) {
              this.vx = -this.vx;
            }
            if (this.y - this.radius < 0 || this.y + this.radius > canvas.height) {
              this.vy = -this.vy;
            }
          }
    
          draw() {
            ctx.beginPath();
            ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
            ctx.fill();
          }
        }
    
        function detectCollision(p1, p2) {
          const dx = p1.x - p2.x;
          const dy = p1.y - p2.y;
          const distance = Math.sqrt(dx * dx + dy * dy);
          return distance < p1.radius + p2.radius;
        }
    
        const particles = [
          new Particle(100, 100, 25),
          new Particle(300, 300, 25),
        ];
    
        function updateParticles() {
          for (let i = 0; i < particles.length; i++) {
            for (let j = i + 1; j < particles.length; j++) {
              if (detectCollision(particles[i], particles[j])) {
                particles[i].vx = -particles[i].vx;
                particles[i].vy = -particles[i].vy;
                particles[j].vx = -particles[j].vx;
                particles[j].vy = -particles[j].vy;
              }
            }
            particles[i].update();
          }
        }
    
        function drawParticles() {
          ctx.clearRect(0, 0, canvas.width, canvas.height);
          for (const particle of particles) {
            particle.draw();
          }
        }
    
        function loop() {
          updateParticles();
          drawParticles();
          requestAnimationFrame(loop);
        }
    
        loop();
      </script>
    </body>
    </html>
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.