How Particles Move Between Two Distinct Bodies

<div id="two-body-sim-wrapper" style="max-width: 820px; margin: 20px auto; background: #0b0f19; color: #f8fafc; border-radius: 12px; padding: 16px; font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; box-shadow: 0 10px 25px rgba(0,0,0,0.35);">
  <div style="display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; padding-bottom: 10px; border-bottom: 1px solid #1e293b;">
    <div>
      <strong style="font-size: 1.05em; color: #38bdf8;">Two-Body Particle Gravity Simulation</strong>
      <div id="sim-status" style="font-size: 0.85em; color: #94a3b8; margin-top: 2px;">Click any particle or body to inspect trajectory &amp; state</div>
    </div>
    <button id="burst-btn" type="button" style="background: #2563eb; color: #fff; border: none; padding: 8px 14px; border-radius: 6px; font-weight: 600; cursor: pointer;">
      + Emit Particle Burst
    </button>
  </div>

  <canvas id="two-body-canvas" width="788" height="440" style="width: 100%; height: auto; display: block; background: radial-gradient(circle at center, #111827 0%, #060913 100%); border-radius: 8px; cursor: crosshair;"></canvas>

  <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; margin-top: 14px; background: #111827; padding: 12px; border-radius: 8px; border: 1px solid #1e293b;">
    <div>
      <label for="mass-b-slider" style="display: flex; justify-content: space-between; font-size: 0.85em; margin-bottom: 4px;">
        <span>Body B Mass Ratio</span>
        <span id="mass-b-val" style="color: #fbbf24; font-weight: 600;">350</span>
      </label>
      <input id="mass-b-slider" type="range" min="100" max="1200" step="10" value="350" style="width: 100%; accent-color: #fbbf24;" />
    </div>
    <div>
      <label for="speed-slider" style="display: flex; justify-content: space-between; font-size: 0.85em; margin-bottom: 4px;">
        <span>Launch Speed Multiplier</span>
        <span id="speed-val" style="color: #34d399; font-weight: 600;">1.0x</span>
      </label>
      <input id="speed-slider" type="range" min="0.4" max="1.8" step="0.1" value="1.0" style="width: 100%; accent-color: #34d399;" />
    </div>
  </div>
</div>

<script>
(function() {
  const canvas = document.getElementById('two-body-canvas');
  const ctx = canvas.getContext('2d');
  const statusEl = document.getElementById('sim-status');
  const massBSlider = document.getElementById('mass-b-slider');
  const massBVal = document.getElementById('mass-b-val');
  const speedSlider = document.getElementById('speed-slider');
  const speedVal = document.getElementById('speed-val');
  const burstBtn = document.getElementById('burst-btn');

  const G = 0.35;
  const bodyA = { name: 'Primary Body A', x: 264, y: 220, mass: 1200, radius: 24, color: '#38bdf8' };
  const bodyB = { name: 'Secondary Body B', x: 524, y: 220, mass: 350, radius: 13, color: '#fbbf24' };

  let particles = [];
  let selectedParticle = null;

  function spawnParticle(speedMult) {
    const mode = Math.random();
    let x, y, vx, vy;
    if (mode < 0.45) {
      // Mid-space transfer stream between Body A and Body B
      x = bodyA.x + 70 + Math.random() * 120;
      y = bodyA.y + (Math.random() - 0.5) * 140;
      vx = (Math.random() - 0.35) * 1.1 * speedMult;
      vy = (Math.random() < 0.5 ? 1 : -1) * (1.1 + Math.random() * 1.1) * speedMult;
    } else if (mode < 0.75) {
      // Orbiting / escaping Body A
      const angle = Math.random() * Math.PI * 2;
      const dist = 45 + Math.random() * 45;
      x = bodyA.x + Math.cos(angle) * dist;
      y = bodyA.y + Math.sin(angle) * dist;
      const v = Math.sqrt((G * bodyA.mass) / dist) * (0.85 + Math.random() * 0.45) * speedMult;
      vx = -Math.sin(angle) * v;
      vy = Math.cos(angle) * v;
    } else {
      // Orbiting / transfer from Body B
      const angle = Math.random() * Math.PI * 2;
      const dist = 28 + Math.random() * 35;
      x = bodyB.x + Math.cos(angle) * dist;
      y = bodyB.y + Math.sin(angle) * dist;
      const v = Math.sqrt((G * bodyB.mass) / dist) * (0.9 + Math.random() * 0.5) * speedMult;
      vx = -Math.sin(angle) * v;
      vy = Math.cos(angle) * v;
    }
    return {
      x: x,
      y: y,
      vx: vx,
      vy: vy,
      trail: [],
      state: 'Orbiting / Transferring'
    };
  }

  function initParticles(count) {
    particles = [];
    const mult = parseFloat(speedSlider.value);
    for (let i = 0; i < count; i++) {
      particles.push(spawnParticle(mult));
    }
    selectedParticle = particles[0];
  }

  massBSlider.addEventListener('input', function() {
    bodyB.mass = parseFloat(this.value);
    bodyB.radius = Math.max(9, Math.round(6 + Math.sqrt(bodyB.mass) * 0.4));
    massBVal.textContent = bodyB.mass;
  });

  speedSlider.addEventListener('input', function() {
    speedVal.textContent = parseFloat(this.value).toFixed(1) + 'x';
  });

  burstBtn.addEventListener('click', function() {
    const mult = parseFloat(speedSlider.value);
    for (let i = 0; i < 20; i++) {
      if (particles.length >= 140) particles.shift();
      particles.push(spawnParticle(mult));
    }
  });

  canvas.addEventListener('click', function(e) {
    const rect = canvas.getBoundingClientRect();
    const scaleX = canvas.width / rect.width;
    const scaleY = canvas.height / rect.height;
    const mx = (e.clientX - rect.left) * scaleX;
    const my = (e.clientY - rect.top) * scaleY;

    let closest = null;
    let minDist = 25;
    for (let i = 0; i < particles.length; i++) {
      const d = Math.hypot(particles[i].x - mx, particles[i].y - my);
      if (d < minDist) {
        minDist = d;
        closest = particles[i];
      }
    }
    if (closest) {
      selectedParticle = closest;
    }
  });

  function update() {
    const mult = parseFloat(speedSlider.value);
    for (let i = 0; i < particles.length; i++) {
      const p = particles[i];
      const dxA = bodyA.x - p.x;
      const dyA = bodyA.y - p.y;
      const distSqA = dxA * dxA + dyA * dyA;
      const distA = Math.sqrt(distSqA);

      const dxB = bodyB.x - p.x;
      const dyB = bodyB.y - p.y;
      const distSqB = dxB * dxB + dyB * dyB;
      const distB = Math.sqrt(distSqB);

      // Reset on impact or deep space escape
      if (distA < bodyA.radius + 2 || distB < bodyB.radius + 2 || p.x < -120 || p.x > canvas.width + 120 || p.y < -120 || p.y > canvas.height + 120) {
        const replacement = spawnParticle(mult);
        Object.assign(p, replacement);
        continue;
      }

      const fA = (G * bodyA.mass) / Math.max(distSqA, 180);
      const fB = (G * bodyB.mass) / Math.max(distSqB, 140);

      p.vx += (fA * (dxA / distA)) + (fB * (dxB / distB));
      p.vy += (fA * (dyA / distA)) + (fB * (dyB / distB));

      p.x += p.vx;
      p.y += p.vy;

      p.trail.push({ x: p.x, y: p.y });
      if (p.trail.length > 26) p.trail.shift();

      if (distA < 95) p.state = 'Bound to Body A';
      else if (distB < 75) p.state = 'Bound to Body B';
      else p.state = 'Inter-body Transfer';
    }
  }

  function drawBody(body) {
    const grad = ctx.createRadialGradient(body.x, body.y, body.radius * 0.2, body.x, body.y, body.radius * 2.4);
    grad.addColorStop(0, body.color);
    grad.addColorStop(1, 'rgba(0,0,0,0)');
    ctx.fillStyle = grad;
    ctx.beginPath();
    ctx.arc(body.x, body.y, body.radius * 2.4, 0, Math.PI * 2);
    ctx.fill();

    ctx.fillStyle = body.color;
    ctx.beginPath();
    ctx.arc(body.x, body.y, body.radius, 0, Math.PI * 2);
    ctx.fill();
  }

  function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // Draw L1 approximate saddle marker
    const ratio = Math.pow(bodyB.mass / (bodyA.mass + bodyB.mass), 1 / 3);
    const l1X = bodyB.x - (bodyB.x - bodyA.x) * ratio;
    ctx.strokeStyle = 'rgba(148, 163, 184, 0.35)';
    ctx.setLineDash([4, 4]);
    ctx.beginPath();
    ctx.moveTo(l1X, bodyA.y - 35);
    ctx.lineTo(l1X, bodyA.y + 35);
    ctx.stroke();
    ctx.setLineDash([]);
    ctx.fillStyle = '#94a3b8';
    ctx.font = '11px sans-serif';
    ctx.fillText('L1 Zone', l1X - 18, bodyA.y - 42);

    drawBody(bodyA);
    drawBody(bodyB);

    for (let i = 0; i < particles.length; i++) {
      const p = particles[i];
      const isSelected = (p === selectedParticle);

      if (p.trail.length > 1) {
        ctx.beginPath();
        ctx.moveTo(p.trail[0].x, p.trail[0].y);
        for (let j = 1; j < p.trail.length; j++) {
          ctx.lineTo(p.trail[j].x, p.trail[j].y);
        }
        ctx.strokeStyle = isSelected ? '#f43f5e' : 'rgba(125, 211, 252, 0.28)';
        ctx.lineWidth = isSelected ? 2 : 1;
        ctx.stroke();
      }

      ctx.fillStyle = isSelected ? '#f43f5e' : '#e0f2fe';
      ctx.beginPath();
      ctx.arc(p.x, p.y, isSelected ? 4 : 2.2, 0, Math.PI * 2);
      ctx.fill();
    }

    if (selectedParticle) {
      const spd = Math.hypot(selectedParticle.vx, selectedParticle.vy).toFixed(2);
      statusEl.textContent = 'Selected Particle: ' + selectedParticle.state + ' | Speed: ' + spd + ' u/frame';
    }
  }

  function loop() {
    update();
    draw();
    requestAnimationFrame(loop);
  }

  initParticles(80);
  loop();
})();
</script>

Comments

Popular posts from this blog

LOGICALLY CONNECTED + LOGICALLY CONNECTING
LIKE IT ^ ( exponential )
+CLOAG

ASSESSMENT

[MAIN PROGRAM] UNDONE HOUSEHOLD WORKs WEB-PROGRAM v2/MAIN