"It needs more detail" was not a pixel problem. A molecule was a set of atoms at whatever angles the storm left them — three circles near each other, never a thing — and the room was a vacuum with objects in it. VSEPR. Bond springs only ever fixed bond LENGTH. Angular springs now splay the bonds around each atom as far apart as they can go, which is what produces the famous silhouettes: water bent at 104.5, CO2 a straight bar, methane a caltrop, a closed carbon ring a hexagon. Two bonds is where lone pairs matter, so O/N/S carry an explicit `bend`; three or more is just even angular spacing, which lands trigonal at 120 and the caltrop at 90 for free. The springs are deliberately weak (ANGLE_SPRING is ~15% of BOND_SPRING) so geometry loses to the storm, to a blast and to Molly's hands. A rigid molecule that ignores being hit reads as fake instantly. They also needed DAMPING, which was the real bug. Undamped, the mean angle measured correct while water actually swung through a 61 degree range — a broken hinge, not a molecule. Damping the tangential relative velocity (as bondSprings already does radially) cuts the spread from sd 16.5 to 1.3. Two measurement traps worth recording, since both nearly sent me the wrong way: a single-frame angle reading is meaningless in a Brownian storm and showed water at 58 degrees when its mean was 105; and starting a two-arm molecule at exactly 180 degrees is a degenerate equilibrium where the cross product vanishes and it cannot pick a side to fold toward, which looked exactly like a systematic bias. THE SOLVENT. 600 motes advected by the Coulomb field the sim already computes. Deliberately NOT particles: no charge, no valence, no collisions, nothing can touch them, so 600 cost 0.16ms where 116 real atoms cost ~5ms. They carry what the atoms cannot — you see the field move before anything charged does. Drawn under everything and never additive, because 600 additive motes would stack into a wash that eats the element hues and the radicals' white. 13 new tests in test/geometry.test.mjs; 81 passing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
75 lines
2.0 KiB
JavaScript
75 lines
2.0 KiB
JavaScript
import { CFG } from './config.js';
|
|
import { clamp, random } from './util.js';
|
|
import { elementOf } from './elements.js';
|
|
|
|
let NEXT_ID = 1;
|
|
|
|
export class Particle {
|
|
constructor(o = {}) {
|
|
this.id = NEXT_ID++;
|
|
this.x = o.x ?? 0;
|
|
this.y = o.y ?? 0;
|
|
this.vx = 0; this.vy = 0;
|
|
this.charge = o.charge ?? 0;
|
|
this.displayCharge = this.charge;
|
|
|
|
// element drives colour / valence / radius / mass unless overridden
|
|
this.el = o.el || null;
|
|
const E = this.el ? elementOf(this.el) : null;
|
|
this.col = E ? E.col : CFG.COL_NEU;
|
|
this.noble = !!(E && E.noble);
|
|
|
|
this.r = o.r ?? (E ? E.r : CFG.P_RADIUS);
|
|
this.slots = o.slots ?? (E ? E.slots : 1);
|
|
this.bonds = [];
|
|
this.zoneTimer = 0;
|
|
this.grace = 0; // brief immunity after the player acts
|
|
this.grabbedBy = null; // 'L' | 'R' | null
|
|
this.fixed = !!o.fixed;
|
|
this.tag = o.tag ?? '';
|
|
this.mass = o.mass ?? (E ? E.mass : 1);
|
|
this.spin = random() * Math.PI * 2;
|
|
this.flash = 0;
|
|
// ideal angle between two bonds on this atom, radians. see elements.js
|
|
this.bend = E && E.bend ? (E.bend * Math.PI) / 180 : 0;
|
|
}
|
|
|
|
get freeSlots() { return Math.max(0, this.slots - this.bonds.length); }
|
|
get charged() { return this.charge !== 0; }
|
|
|
|
setCharge(c) {
|
|
c = clamp(c, -1, 1);
|
|
if (c === this.charge) return false;
|
|
this.charge = c;
|
|
this.grace = CFG.ZONE_GRACE;
|
|
this.zoneTimer = 0;
|
|
this.flash = 1;
|
|
return true;
|
|
}
|
|
|
|
addForce(fx, fy, dt) {
|
|
if (this.fixed) return;
|
|
this.vx += (fx / this.mass) * dt;
|
|
this.vy += (fy / this.mass) * dt;
|
|
}
|
|
|
|
addImpulse(ix, iy) {
|
|
if (this.fixed) return;
|
|
this.vx += ix / this.mass;
|
|
this.vy += iy / this.mass;
|
|
}
|
|
|
|
isBondedTo(p) { return this.bonds.includes(p); }
|
|
}
|
|
|
|
export function bond(a, b) {
|
|
if (a === b || a.isBondedTo(b)) return false;
|
|
a.bonds.push(b); b.bonds.push(a);
|
|
return true;
|
|
}
|
|
|
|
export function unbond(a, b) {
|
|
const i = a.bonds.indexOf(b); if (i >= 0) a.bonds.splice(i, 1);
|
|
const j = b.bonds.indexOf(a); if (j >= 0) b.bonds.splice(j, 1);
|
|
}
|