Layer inclined orbits in flow and even-ring lock spacing
This commit is contained in:
@@ -3,65 +3,74 @@
|
||||
export type OrbitPhysics = {
|
||||
rx: number;
|
||||
ry: number;
|
||||
tiltDeg: number;
|
||||
nodeDeg: number;
|
||||
inclination: number;
|
||||
speed: number;
|
||||
phase: number;
|
||||
layer: number;
|
||||
};
|
||||
|
||||
export type Vec2 = { x: number; y: number };
|
||||
|
||||
const GOLDEN = 2.399963229728653;
|
||||
export type Pose3 = Vec2 & { z: number; scale: number };
|
||||
|
||||
const VIEW_R = 258;
|
||||
|
||||
/** Per-star ellipses from inner rim to ~90% of viewport radius. */
|
||||
/** 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 band = index % 5;
|
||||
const rx =
|
||||
92 +
|
||||
lane * (VIEW_R - 92) +
|
||||
band * 7 +
|
||||
((index * 11) % 5) * 4;
|
||||
const ry =
|
||||
68 +
|
||||
((index * 13) % 9) * 14 +
|
||||
lane * 72 +
|
||||
(index % 3) * 9;
|
||||
const tiltDeg = -42 + ((index * 23 + band * 17) % 84);
|
||||
const rx = Math.min(108 + lane * (VIEW_R - 108), VIEW_R);
|
||||
return {
|
||||
rx: Math.min(rx, VIEW_R),
|
||||
ry: Math.min(ry, VIEW_R * 0.88),
|
||||
tiltDeg,
|
||||
speed: 0.14 + (index % 9) * 0.018 + band * 0.008,
|
||||
phase: (index / n) * Math.PI * 2 + index * GOLDEN * 0.15,
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
export function positionOnEllipse(
|
||||
angle: number,
|
||||
rx: number,
|
||||
ry: number,
|
||||
tiltDeg: number,
|
||||
): Vec2 {
|
||||
const ex = Math.cos(angle) * rx;
|
||||
const ey = Math.sin(angle) * ry;
|
||||
const rad = (tiltDeg * Math.PI) / 180;
|
||||
/** 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);
|
||||
return {
|
||||
x: ex * Math.cos(rad) - ey * Math.sin(rad),
|
||||
y: ex * Math.sin(rad) + ey * Math.cos(rad),
|
||||
x,
|
||||
y,
|
||||
z: depth,
|
||||
scale: 0.7 + (depth + 1) * 0.3,
|
||||
};
|
||||
}
|
||||
|
||||
function slotSeed(index: number, total: number): Vec2 {
|
||||
const angle = -Math.PI / 2 + index * GOLDEN;
|
||||
const band = index % 4;
|
||||
const radius = 118 + band * 46 + Math.floor(index / 4) * 10;
|
||||
const squash = 0.9 + (index % 3) * 0.05;
|
||||
return {
|
||||
x: Math.cos(angle) * Math.min(radius, VIEW_R),
|
||||
y: Math.sin(angle) * Math.min(radius, VIEW_R) * squash,
|
||||
};
|
||||
/** 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 {
|
||||
@@ -70,45 +79,68 @@ function dist(a: Vec2, b: Vec2): number {
|
||||
return Math.hypot(dx, dy);
|
||||
}
|
||||
|
||||
const LAYOUT_VER = 2;
|
||||
const LAYOUT_VER = 3;
|
||||
const layoutCache = new Map<string, Vec2[]>();
|
||||
|
||||
/** Golden-angle seeds relaxed for minimum star separation (lock mode). */
|
||||
/**
|
||||
* 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 = Array.from({ length: total }, (_, i) => slotSeed(i, total));
|
||||
const minSep = total > 12 ? 56 : 62;
|
||||
const pts: Vec2[] = [];
|
||||
if (total <= 0) {
|
||||
layoutCache.set(key, pts);
|
||||
return pts;
|
||||
}
|
||||
|
||||
for (let iter = 0; iter < 56; iter++) {
|
||||
const inner = 124;
|
||||
const outer = 246;
|
||||
if (total <= 7) {
|
||||
const radius = (inner + outer) / 2;
|
||||
for (let i = 0; i < total; i++) {
|
||||
for (let j = i + 1; j < total; j++) {
|
||||
const d = dist(pts[i], pts[j]);
|
||||
if (d < minSep && d > 0.001) {
|
||||
const push = (minSep - d) * 0.55;
|
||||
const ux = (pts[i].x - pts[j].x) / d;
|
||||
const uy = (pts[i].y - pts[j].y) / d;
|
||||
pts[i].x += ux * push;
|
||||
pts[i].y += uy * push;
|
||||
pts[j].x -= ux * push;
|
||||
pts[j].y -= uy * push;
|
||||
}
|
||||
}
|
||||
const r = dist(pts[i], { x: 0, y: 0 });
|
||||
const vaultPad = 96;
|
||||
if (r < vaultPad && r > 0.001) {
|
||||
const s = vaultPad / r;
|
||||
pts[i].x *= s;
|
||||
pts[i].y *= s;
|
||||
}
|
||||
const edge = dist(pts[i], { x: 0, y: 0 });
|
||||
if (edge > VIEW_R) {
|
||||
const s = VIEW_R / edge;
|
||||
pts[i].x *= s;
|
||||
pts[i].y *= s;
|
||||
}
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +149,34 @@ export function lockedPositions(total: number): Vec2[] {
|
||||
}
|
||||
|
||||
export function lockedSlot(index: number, total: number): Vec2 {
|
||||
return lockedPositions(total)[index] ?? slotSeed(index, total);
|
||||
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.85;
|
||||
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 = spread * (0.82 + (mi % 3) * 0.1);
|
||||
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,
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function moonPositions(
|
||||
|
||||
Reference in New Issue
Block a user