Rebuild the vault as a lensed orrery with an inbound capture meteor
This commit is contained in:
@@ -1,297 +1,68 @@
|
||||
/** Flow + lock layouts in viewBox coordinates (origin center, ±~300). */
|
||||
|
||||
export type OrbitPhysics = {
|
||||
rx: number;
|
||||
ry: number;
|
||||
nodeDeg: number;
|
||||
inclination: number;
|
||||
speed: number;
|
||||
phase: number;
|
||||
layer: number;
|
||||
};
|
||||
/** Face-on orrery: two even shells around the vault. */
|
||||
|
||||
export type Vec2 = { x: number; y: number };
|
||||
|
||||
export type Pose3 = Vec2 & { z: number; scale: number };
|
||||
export type ShellBody = {
|
||||
index: number;
|
||||
shell: "inner" | "outer";
|
||||
radius: number;
|
||||
baseAngle: number;
|
||||
size: number;
|
||||
warm: boolean;
|
||||
};
|
||||
|
||||
const VIEW_R = 258;
|
||||
const INNER_R = 112;
|
||||
const OUTER_R = 178;
|
||||
|
||||
/** Inclined shells: each star has its own node, tilt, and radius. */
|
||||
export function flowOrbitPhysics(index: number, total: number): OrbitPhysics {
|
||||
const n = Math.max(total, 1);
|
||||
const layer = index % 3;
|
||||
const lane = index / Math.max(n - 1, 1);
|
||||
const rx = Math.min(108 + lane * (VIEW_R - 108), VIEW_R);
|
||||
return {
|
||||
rx,
|
||||
ry: rx * (0.96 - layer * 0.03),
|
||||
nodeDeg: (index * 53) % 360,
|
||||
inclination: 0.42 + layer * 0.32,
|
||||
speed: 0.18 + layer * 0.06 + (index % 5) * 0.015,
|
||||
phase: (index / n) * Math.PI * 2 + index * 0.35,
|
||||
layer,
|
||||
};
|
||||
}
|
||||
|
||||
/** Orbital plane → screen, with line-of-sight depth for scale and paint order. */
|
||||
export function positionOnEllipse(angle: number, phys: OrbitPhysics): Pose3 {
|
||||
const lx = Math.cos(angle) * phys.rx;
|
||||
const ly = Math.sin(angle) * phys.ry;
|
||||
const yInc = ly * Math.cos(phys.inclination);
|
||||
const zRaw = ly * Math.sin(phys.inclination);
|
||||
const node = (phys.nodeDeg * Math.PI) / 180;
|
||||
const x = lx * Math.cos(node) - yInc * Math.sin(node);
|
||||
const y = lx * Math.sin(node) + yInc * Math.cos(node);
|
||||
const depth = zRaw / Math.max(phys.rx, 1);
|
||||
const facing = (depth + 1) / 2;
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
z: depth,
|
||||
scale: 0.38 + facing * 1.05,
|
||||
};
|
||||
}
|
||||
|
||||
/** Split an inclined orbit into the arc behind the vault and the arc in front. */
|
||||
export function orbitArcs(
|
||||
phys: OrbitPhysics,
|
||||
samples = 80,
|
||||
): { back: string; front: string } {
|
||||
let back = "";
|
||||
let front = "";
|
||||
let prev: boolean | null = null;
|
||||
for (let i = 0; i <= samples; i++) {
|
||||
const p = positionOnEllipse((i / samples) * Math.PI * 2, phys);
|
||||
const near = p.z >= 0;
|
||||
const cmd = `${p.x.toFixed(1)} ${p.y.toFixed(1)}`;
|
||||
if (near) {
|
||||
front += prev === true ? `L ${cmd} ` : `M ${cmd} `;
|
||||
} else {
|
||||
back += prev === false ? `L ${cmd} ` : `M ${cmd} `;
|
||||
}
|
||||
prev = near;
|
||||
}
|
||||
return { back, front };
|
||||
}
|
||||
|
||||
function dist(a: Vec2, b: Vec2): number {
|
||||
const dx = a.x - b.x;
|
||||
const dy = a.y - b.y;
|
||||
return Math.hypot(dx, dy);
|
||||
}
|
||||
|
||||
const LAYOUT_VER = 3;
|
||||
const layoutCache = new Map<string, Vec2[]>();
|
||||
|
||||
/**
|
||||
* Lock layout: concentric rings, counts proportional to radius so
|
||||
* neighbor chord length stays nearly constant on every ring.
|
||||
*/
|
||||
export function lockedPositions(total: number): Vec2[] {
|
||||
const key = `${LAYOUT_VER}:${total}`;
|
||||
const cached = layoutCache.get(key);
|
||||
if (cached) return cached;
|
||||
|
||||
const pts: Vec2[] = [];
|
||||
if (total <= 0) {
|
||||
layoutCache.set(key, pts);
|
||||
return pts;
|
||||
}
|
||||
|
||||
const inner = 124;
|
||||
const outer = 246;
|
||||
if (total <= 7) {
|
||||
const radius = (inner + outer) / 2;
|
||||
for (let i = 0; i < total; i++) {
|
||||
const a = -Math.PI / 2 + (i / total) * Math.PI * 2;
|
||||
pts.push({ x: Math.cos(a) * radius, y: Math.sin(a) * radius });
|
||||
}
|
||||
layoutCache.set(key, pts);
|
||||
return pts;
|
||||
}
|
||||
|
||||
const rings = total <= 14 ? 2 : 3;
|
||||
const radii = Array.from({ length: rings }, (_, i) =>
|
||||
rings === 1 ? (inner + outer) / 2 : inner + ((outer - inner) * i) / (rings - 1),
|
||||
);
|
||||
const wsum = radii.reduce((a, b) => a + b, 0);
|
||||
const counts = radii.map((r) => Math.max(1, Math.round((r / wsum) * total)));
|
||||
let sum = counts.reduce((a, b) => a + b, 0);
|
||||
while (sum > total) {
|
||||
let idx = 0;
|
||||
for (let i = 1; i < counts.length; i++) {
|
||||
if (counts[i] > counts[idx]) idx = i;
|
||||
}
|
||||
if (counts[idx] <= 1) break;
|
||||
counts[idx] -= 1;
|
||||
sum -= 1;
|
||||
}
|
||||
while (sum < total) {
|
||||
let idx = 0;
|
||||
for (let i = 1; i < counts.length; i++) {
|
||||
if (counts[i] < counts[idx]) idx = i;
|
||||
}
|
||||
counts[idx] += 1;
|
||||
sum += 1;
|
||||
}
|
||||
|
||||
for (let ri = 0; ri < rings; ri++) {
|
||||
const count = counts[ri];
|
||||
const radius = radii[ri];
|
||||
const twist = (ri % 2) * (Math.PI / count);
|
||||
for (let i = 0; i < count; i++) {
|
||||
const a = -Math.PI / 2 + twist + (i / count) * Math.PI * 2;
|
||||
pts.push({ x: Math.cos(a) * radius, y: Math.sin(a) * radius });
|
||||
}
|
||||
}
|
||||
|
||||
layoutCache.set(key, pts);
|
||||
return pts;
|
||||
}
|
||||
|
||||
export function lockedSlot(index: number, total: number): Vec2 {
|
||||
const pts = lockedPositions(total);
|
||||
return pts[index] ?? { x: 0, y: 0 };
|
||||
}
|
||||
|
||||
/** Small inclined moon orbits around a parent, in parent-local space. */
|
||||
export function flowMoonOffsets(
|
||||
count: number,
|
||||
time: number,
|
||||
spread = 30,
|
||||
): Pose3[] {
|
||||
const out: Pose3[] = [];
|
||||
const inc = 0.55;
|
||||
for (let mi = 0; mi < count; mi++) {
|
||||
const ang =
|
||||
(mi / Math.max(count, 1)) * Math.PI * 2 + time * (1.05 + mi * 0.08);
|
||||
const rad = Math.max(spread, 44) * (1 + mi * 0.05);
|
||||
const lx = Math.cos(ang) * rad;
|
||||
const ly = Math.sin(ang) * rad;
|
||||
const y = ly * Math.cos(inc);
|
||||
const z = Math.sin(ang) * Math.sin(inc);
|
||||
out.push({
|
||||
x: lx,
|
||||
y,
|
||||
z,
|
||||
scale: 0.82 + (z + 1) * 0.12,
|
||||
/** Inner shell is fewer, larger, warmer. Outer shell is the rest. */
|
||||
export function assignShells(count: number): ShellBody[] {
|
||||
if (count <= 0) return [];
|
||||
let innerCount = Math.max(1, Math.round(count * 0.38));
|
||||
if (count > 4) innerCount = Math.max(3, innerCount);
|
||||
innerCount = Math.min(innerCount, count);
|
||||
const outerCount = count - innerCount;
|
||||
const bodies: ShellBody[] = [];
|
||||
for (let i = 0; i < innerCount; i++) {
|
||||
bodies.push({
|
||||
index: i,
|
||||
shell: "inner",
|
||||
radius: INNER_R,
|
||||
baseAngle: -Math.PI / 2 + (i / innerCount) * Math.PI * 2,
|
||||
size: 11,
|
||||
warm: true,
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function moonPositions(
|
||||
cx: number,
|
||||
cy: number,
|
||||
count: number,
|
||||
time: number,
|
||||
flowing: boolean,
|
||||
baseSpread = 32,
|
||||
): { x: number; y: number; angle: number }[] {
|
||||
const out: { x: number; y: number; angle: number }[] = [];
|
||||
const outward = Math.atan2(cy, cx);
|
||||
for (let mi = 0; mi < count; mi++) {
|
||||
const slice = (mi / Math.max(count, 1)) * Math.PI * 2;
|
||||
const span = Math.PI * 0.7;
|
||||
const fan =
|
||||
count <= 1 ? 0 : (mi / Math.max(count - 1, 1) - 0.5) * span;
|
||||
const angle = flowing ? slice + time * (0.85 + mi * 0.08) : outward + fan;
|
||||
const dist = flowing
|
||||
? Math.max(baseSpread, 44)
|
||||
: 48 + mi * 8;
|
||||
out.push({
|
||||
angle,
|
||||
x: cx + Math.cos(angle) * dist,
|
||||
y: cy + Math.sin(angle) * dist * 0.86,
|
||||
for (let i = 0; i < outerCount; i++) {
|
||||
bodies.push({
|
||||
index: innerCount + i,
|
||||
shell: "outer",
|
||||
radius: OUTER_R,
|
||||
baseAngle: -Math.PI / 2 + ((i + 0.5) / outerCount) * Math.PI * 2,
|
||||
size: 6.4,
|
||||
warm: false,
|
||||
});
|
||||
}
|
||||
return out;
|
||||
return bodies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep lock moons on the outward side of their star, outside the corona.
|
||||
* A moon that would cross a neighbor star is dropped (null).
|
||||
*/
|
||||
export function relaxLockedMoons(
|
||||
stars: Vec2[],
|
||||
moonsByStar: Vec2[][],
|
||||
): (Vec2 | null)[][] {
|
||||
const flat: { p: Vec2; star: number; moon: number; drop: boolean }[] = [];
|
||||
for (let si = 0; si < moonsByStar.length; si++) {
|
||||
for (let mi = 0; mi < moonsByStar[si].length; mi++) {
|
||||
flat.push({ p: { ...moonsByStar[si][mi] }, star: si, moon: mi, drop: false });
|
||||
}
|
||||
}
|
||||
|
||||
const minMoon = 20;
|
||||
const minStarMoon = 46;
|
||||
|
||||
for (let iter = 0; iter < 28; iter++) {
|
||||
for (let i = 0; i < flat.length; i++) {
|
||||
const s = stars[flat[i].star];
|
||||
const outward = Math.atan2(s.y, s.x);
|
||||
const ang = Math.atan2(flat[i].p.y - s.y, flat[i].p.x - s.x);
|
||||
let delta = ang - outward;
|
||||
while (delta > Math.PI) delta -= Math.PI * 2;
|
||||
while (delta < -Math.PI) delta += Math.PI * 2;
|
||||
if (Math.abs(delta) > Math.PI * 0.42) {
|
||||
const clamped = outward + Math.sign(delta || 1) * Math.PI * 0.42;
|
||||
const ds = Math.max(dist(flat[i].p, s), minStarMoon);
|
||||
flat[i].p.x = s.x + Math.cos(clamped) * ds;
|
||||
flat[i].p.y = s.y + Math.sin(clamped) * ds;
|
||||
}
|
||||
const ds = dist(flat[i].p, s);
|
||||
if (ds < minStarMoon) {
|
||||
const a = Math.atan2(flat[i].p.y - s.y, flat[i].p.x - s.x);
|
||||
flat[i].p.x = s.x + Math.cos(a) * minStarMoon;
|
||||
flat[i].p.y = s.y + Math.sin(a) * minStarMoon;
|
||||
}
|
||||
for (let j = i + 1; j < flat.length; j++) {
|
||||
const d = dist(flat[i].p, flat[j].p);
|
||||
if (d < minMoon && d > 0.001) {
|
||||
const push = (minMoon - d) * 0.5;
|
||||
const ux = (flat[i].p.x - flat[j].p.x) / d;
|
||||
const uy = (flat[i].p.y - flat[j].p.y) / d;
|
||||
flat[i].p.x += ux * push;
|
||||
flat[i].p.y += uy * push;
|
||||
flat[j].p.x -= ux * push;
|
||||
flat[j].p.y -= uy * push;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const moon of flat) {
|
||||
const parent = stars[moon.star];
|
||||
for (let si = 0; si < stars.length; si++) {
|
||||
if (si === moon.star) continue;
|
||||
if (dist(moon.p, stars[si]) < 40) {
|
||||
moon.drop = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (dist(moon.p, parent) < 40) moon.drop = true;
|
||||
if (moon.p.y > 128 || moon.p.y < -185) {
|
||||
const side = moon.p.x >= parent.x ? 1 : -1;
|
||||
const ds = Math.max(dist(moon.p, parent), 46);
|
||||
moon.p.x = parent.x + side * ds;
|
||||
moon.p.y = Math.max(-175, Math.min(120, parent.y));
|
||||
for (let si = 0; si < stars.length; si++) {
|
||||
if (si !== moon.star && dist(moon.p, stars[si]) < 40) moon.drop = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const out: (Vec2 | null)[][] = moonsByStar.map((row) => row.map(() => null));
|
||||
for (const f of flat) {
|
||||
out[f.star][f.moon] = f.drop ? null : f.p;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function lerpPos(a: Vec2, b: Vec2, t: number): Vec2 {
|
||||
export function shellPose(body: ShellBody, time: number): Vec2 & { angle: number } {
|
||||
const speed = body.shell === "inner" ? 0.18 : 0.11;
|
||||
const angle = body.baseAngle + time * speed;
|
||||
return {
|
||||
x: a.x * t + b.x * (1 - t),
|
||||
y: a.y * t + b.y * (1 - t),
|
||||
angle,
|
||||
x: Math.cos(angle) * body.radius,
|
||||
y: Math.sin(angle) * body.radius,
|
||||
};
|
||||
}
|
||||
|
||||
/** Point on a smooth inward spiral. u = 0 is the source; u = 1 is the shadow. */
|
||||
export function spiralPoint(source: Vec2, u: number): Vec2 {
|
||||
const clamped = Math.min(1, Math.max(0, u));
|
||||
const a0 = Math.atan2(source.y, source.x);
|
||||
const r0 = Math.hypot(source.x, source.y);
|
||||
const fall = 1 - Math.pow(clamped, 0.92);
|
||||
const spin = clamped * Math.PI * 1.05;
|
||||
const r = r0 * fall;
|
||||
const a = a0 + spin;
|
||||
return { x: Math.cos(a) * r, y: Math.sin(a) * r };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user