Prompt specification
Build or customize the Three.js 3D scene "Two charged-sphere force model" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Two charged-sphere force model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-charged-sphere-force-model-x1609
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: two charged-sphere force model. Place two labeled point-charge proxies on a track with equal-and-opposite force arrows; use Coulomb's law with stated units and keep visual sphere size separate from point-charge assumptions. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (two-charged-sphere-force-model-x1609):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Low-Poly Coulomb Force Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07101c;
--panel: rgba(10, 20, 34, 0.82);
--panel-border: rgba(159, 207, 236, 0.17);
--text: #eef7ff;
--muted: #91a8bc;
--cyan: #49dcff;
--pink: #ff668f;
--lime: #b7f36b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#app canvas {
touch-action: none;
cursor: grab;
}
#app canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.brand {
position: absolute;
top: 24px;
left: 28px;
max-width: 430px;
text-shadow: 0 2px 20px rgba(0, 0, 0, 0.55);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 20px;
height: 2px;
background: var(--cyan);
box-shadow: 0 0 12px rgba(73, 220, 255, 0.75);
}
h1 {
margin: 7px 0 3px;
font-size: clamp(28px, 3.2vw, 48px);
line-height: 1;
letter-spacing: -0.045em;
font-weight: 760;
}
.brand p {
margin: 8px 0 0;
color: var(--muted);
font-size: 13px;
letter-spacing: 0.015em;
}
.panel {
background:
linear-gradient(145deg, rgba(22, 39, 58, 0.77), rgba(7, 16, 28, 0.9));
border: 1px solid var(--panel-border);
box-shadow:
0 20px 60px rgba(0, 0, 0, 0.32),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
#readout {
position: absolute;
top: 24px;
right: 28px;
width: 354px;
padding: 17px 18px 16px;
border-radius: 17px;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 12px;
color: #a9bfd1;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.live-dot {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--lime);
letter-spacing: 0.1em;
}
.live-dot::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--lime);
box-shadow: 0 0 10px rgba(183, 243, 107, 0.9);
}
.equation {
padding: 13px 14px;
border: 1px solid rgba(126, 177, 208, 0.15);
border-radius: 11px;
background: rgba(3, 10, 18, 0.48);
font-family: Georgia, "Times New Roman", serif;
font-size: 20px;
letter-spacing: 0.015em;
}
.equation .fraction {
display: inline-flex;
flex-direction: column;
align-items: center;
vertical-align: middle;
margin: 0 5px;
font-size: 14px;
line-height: 1.05;
}
.fraction span:first-child {
width: 100%;
padding-bottom: 2px;
border-bottom: 1px solid currentColor;
text-align: center;
}
.constant {
margin-top: 9px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.calculation {
margin-top: 11px;
padding: 9px 10px;
border-left: 2px solid rgba(73, 220, 255, 0.55);
color: #bed0df;
background: rgba(73, 220, 255, 0.045);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
line-height: 1.55;
overflow-wrap: anywhere;
}
.force-row {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 16px;
margin-top: 13px;
}
.force-number {
line-height: 0.9;
font-variant-numeric: tabular-nums;
}
#forceValue {
font-size: 39px;
font-weight: 780;
letter-spacing: -0.055em;
}
#forceUnit {
margin-left: 7px;
color: var(--cyan);
font-size: 17px;
font-weight: 750;
}
.pair-state {
max-width: 120px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
text-align: right;
}
.vector-badge {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
padding: 9px 10px;
border-radius: 9px;
color: #cfe0ed;
background: rgba(183, 243, 107, 0.065);
font-size: 11px;
}
.vector-badge strong {
color: var(--lime);
font-size: 12px;
white-space: nowrap;
}
.assumption {
margin-top: 11px;
padding-top: 11px;
border-top: 1px solid rgba(159, 207, 236, 0.11);
color: #8fa7bb;
font-size: 10px;
line-height: 1.52;
}
.assumption strong {
color: #c6d8e6;
}
#controls {
position: absolute;
left: 50%;
bottom: 24px;
width: min(760px, calc(100vw - 32px));
padding: 15px 17px 13px;
border-radius: 17px;
transform: translateX(-50%);
pointer-events: auto;
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-bottom: 12px;
}
.control-heading h2 {
margin: 0;
font-size: 11px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.control-heading h2 span {
margin-left: 8px;
color: var(--muted);
font-size: 9px;
font-weight: 600;
letter-spacing: 0.07em;
}
#resetView {
padding: 6px 10px;
border: 1px solid rgba(159, 207, 236, 0.2);
border-radius: 8px;
color: #c9dbe8;
background: rgba(255, 255, 255, 0.035);
cursor: pointer;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
#resetView:hover {
border-color: rgba(73, 220, 255, 0.55);
color: white;
background: rgba(73, 220, 255, 0.08);
}
.control-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 13px;
}
.control {
min-width: 0;
padding: 10px 11px 8px;
border: 1px solid rgba(159, 207, 236, 0.1);
border-radius: 10px;
background: rgba(3, 10, 18, 0.32);
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
margin-bottom: 8px;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.control-label output {
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.control.negative .control-label output {
color: var(--pink);
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 3px 0 1px;
border-radius: 999px;
accent-color: var(--cyan);
cursor: pointer;
}
.control.negative input[type="range"] {
accent-color: var(--pink);
}
.control-note {
margin-top: 11px;
color: #7891a6;
font-size: 9px;
line-height: 1.45;
}
.control-note strong {
color: #b9ccda;
font-weight: 650;
}
#hint {
position: absolute;
left: 28px;
bottom: 27px;
color: rgba(170, 196, 215, 0.58);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: #b9d4e5;
background: #07101c;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 0.45s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 920px) {
#hint {
display: none;
}
#readout {
width: 320px;
}
}
@media (max-width: 700px) {
.brand {
top: 15px;
left: 16px;
}
.brand p {
display: none;
}
#readout {
top: 82px;
right: 12px;
left: 12px;
width: auto;
padding: 13px;
}
.constant,
.calculation {
display: none;
}
.equation {
padding: 9px 11px;
font-size: 17px;
}
#forceValue {
font-size: 31px;
}
.assumption {
margin-top: 8px;
padding-top: 8px;
font-size: 9px;
}
#controls {
bottom: 10px;
width: calc(100vw - 20px);
padding: 11px;
}
.control-heading h2 span,
.control-note {
display: none;
}
.control-grid {
gap: 7px;
}
.control {
padding: 8px 8px 7px;
}
.control-label {
display: block;
font-size: 8px;
}
.control-label output {
display: block;
margin-top: 3px;
font-size: 10px;
}
}
@media (max-height: 650px) {
.assumption,
.constant,
.calculation {
display: none;
}
#readout {
padding: 12px;
}
}
</style>
</head>
<body>
<main id="app"></main>
<div class="hud">
<header class="brand">
<div class="eyebrow">Interactive / Electrostatics</div>
<h1>Coulomb Force</h1>
<p>Two low-poly charged-sphere proxies on a metered track.</p>
</header>
<section id="readout" class="panel" aria-live="polite">
<div class="panel-title">
<span>Live Coulomb calculation</span>
<span class="live-dot">Model active</span>
</div>
<div class="equation">
F =
<i>k</i><sub>e</sub>
<span class="fraction">
<span>|q<sub>+</sub>q<sub>−</sub>|</span>
<span>r²</span>
</span>
</div>
<div class="constant">
k<sub>e</sub> = 8.9875517923 × 10<sup>9</sup> N·m²/C²<br>
Charge: nC · Separation: m · Force: nN, µN or mN
</div>
<div id="calculation" class="calculation"></div>
<div class="force-row">
<div class="force-number">
<span id="forceValue">71.9</span><span id="forceUnit">µN</span>
</div>
<div class="pair-state">
Opposite signs<br>
<strong>Attractive pair</strong>
</div>
</div>
<div class="vector-badge">
<span>Vector balance</span>
<strong>ΣF⃗ = 0 N</strong>
</div>
<div class="assumption">
<strong>Point-charge assumption:</strong>
each charge is treated as a point at its sphere’s center. The rendered
sphere radius is schematic and is <em>not</em> used in Coulomb’s law.
</div>
</section>
<section id="controls" class="panel">
<div class="control-heading">
<h2>Model Inputs <span>drag to adjust</span></h2>
<button id="resetView" type="button">Reset view</button>
</div>
<div class="control-grid">
<label class="control">
<span class="control-label">
Positive charge
<output id="qPlusOut">+2.00 nC</output>
</span>
<input
id="qPlus"
type="range"
min="0.5"
max="6"
step="0.25"
value="2"
aria-label="Positive charge in nanocoulombs"
>
</label>
<label class="control negative">
<span class="control-label">
Negative charge
<output id="qMinusOut">−2.00 nC</output>
</span>
<input
id="qMinus"
type="range"
min="0.5"
max="6"
step="0.25"
value="2"
aria-label="Magnitude of negative charge in nanocoulombs"
>
</label>
<label class="control">
<span class="control-label">
Separation
<output id="separationOut">0.50 m</output>
</span>
<input
id="separation"
type="range"
min="0.35"
max="1"
step="0.01"
value="0.5"
aria-label="Center-to-center separation in meters"
>
</label>
</div>
<div class="control-note">
<strong>Display scale:</strong>
track spacing is linear in meters. Arrow length uses a compressed
visual scale for readability; the numeric force uses Coulomb’s law directly.
</div>
</section>
<div id="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</div>
<div id="loading">Building low-poly force lab…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.querySelector("#app");
const loading = document.querySelector("#loading");
// All lengths used by the renderer are scene units. The physical
// separation shown to the user is converted independently from this scale.
const K = 8.9875517923e9; // N·m²/C²
const WORLD_UNITS_PER_METER = 2.8;
const SPHERE_RADIUS = 0.19; // Visual proxy only; never used in Coulomb's law.
const SPHERE_Y = 0.42;
const TRACK_LENGTH = 7.15;
const state = {
qPlus: 2.0, // nC
qMinus: 2.0, // magnitude in nC
r: 0.50 // m
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101c);
scene.fog = new THREE.Fog(0x07101c, 13, 28);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
70
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly scene showing two opposite charges and equal opposite Coulomb force arrows"
);
app.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xa9dcff, 0x10121b, 1.8);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xf4fbff, 3.2);
keyLight.position.set(4.5, 7, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 24;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const cyanRim = new THREE.PointLight(0x35d9ff, 28, 9, 2);
cyanRim.position.set(-4.5, 2.5, 2.5);
scene.add(cyanRim);
const pinkRim = new THREE.PointLight(0xff4f86, 28, 9, 2);
pinkRim.position.set(4.5, 2.3, -1.8);
scene.add(pinkRim);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 20),
new THREE.MeshStandardMaterial({
color: 0x08111e,
roughness: 0.92,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.065;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 48, 0x2a4960, 0x173044);
grid.position.y = -0.05;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
const track = new THREE.Group();
scene.add(track);
const railBase = new THREE.Mesh(
new THREE.BoxGeometry(TRACK_LENGTH, 0.15, 0.46),
new THREE.MeshStandardMaterial({
color: 0x17283a,
roughness: 0.68,
metalness: 0.35,
flatShading: true
})
);
railBase.position.y = 0.07;
railBase.castShadow = true;
railBase.receiveShadow = true;
track.add(railBase);
const railTop = new THREE.Mesh(
new THREE.BoxGeometry(TRACK_LENGTH - 0.12, 0.026, 0.07),
new THREE.MeshStandardMaterial({
color: 0x4e7188,
emissive: 0x183548,
emissiveIntensity: 0.45,
roughness: 0.5,
metalness: 0.45
})
);
railTop.position.y = 0.157;
railTop.receiveShadow = true;
track.add(railTop);
const tickMaterial = new THREE.MeshStandardMaterial({
color: 0x7ea0b7,
emissive: 0x173548,
emissiveIntensity: 0.35,
roughness: 0.6
});
for (let meters = -1.25; meters <= 1.251; meters += 0.25) {
const major = Math.abs(meters * 2 - Math.round(meters * 2)) < 0.01;
const tick = new THREE.Mesh(
new THREE.BoxGeometry(
0.014,
major ? 0.075 : 0.042,
major ? 0.30 : 0.19
),
tickMaterial
);
tick.position.set(
meters * WORLD_UNITS_PER_METER,
0.19,
0
);
track.add(tick);
}
// Canvas-backed sprites keep all labels inside the Three.js scene.
function roundedRectPath(ctx, x, y, w, h, r) {
const radius = Math.min(r, w / 2, h / 2);
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + w - radius, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + radius);
ctx.lineTo(x + w, y + h - radius);
ctx.quadraticCurveTo(x + w, y + h, x + w - radius, y + h);
ctx.lineTo(x + radius, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function createPanelSprite() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 160;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 30;
function draw(title, value, accent) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.shadowColor = accent;
ctx.shadowBlur = 18;
ctx.fillStyle = "rgba(6, 15, 26, 0.90)";
roundedRectPath(ctx, 5, 5, 502, 150, 22);
ctx.fill();
ctx.restore();
ctx.strokeStyle = accent;
ctx.lineWidth = 3;
roundedRectPath(ctx, 5, 5, 502, 150, 22);
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = accent;
ctx.font = "700 22px system-ui, sans-serif";
ctx.fillText(title.toUpperCase(), 256, 47);
ctx.fillStyle = "#f3f9ff";
ctx.font = "750 43px system-ui, sans-serif";
ctx.fillText(value, 256, 102);
texture.needsUpdate = true;
}
return { sprite, draw };
}
function createMeterLabel(text) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 64;
const ctx = canvas.getContext("2d");
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "700 25px ui-monospace, monospace";
ctx.fillStyle = "rgba(178, 205, 222, 0.78)";
ctx.fillText(text, 128, 32);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
opacity: 0.85
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(0.66, 0.165, 1);
sprite.renderOrder = 15;
return sprite;
}
function createChargeSign(symbol, accent) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "800 92px system-ui, sans-serif";
ctx.lineWidth = 8;
ctx.strokeStyle = "rgba(4, 12, 22, 0.75)";
ctx.strokeText(symbol, 64, 65);
ctx.fillStyle = accent;
ctx.fillText(symbol, 64, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(0.26, 0.26, 1);
sprite.renderOrder = 35;
return sprite;
}
for (const meter of [-1, -0.5, 0, 0.5, 1]) {
const label = createMeterLabel(
meter === 0 ? "0 m" : `${meter > 0 ? "+" : "−"}${Math.abs(meter).toFixed(1)} m`
);
label.position.set(
meter * WORLD_UNITS_PER_METER,
0.285,
0.37
);
track.add(label);
}
function makeProxy(accent, sign) {
const group = new THREE.Group();
const geometry = new THREE.IcosahedronGeometry(SPHERE_RADIUS, 1);
const material = new THREE.MeshStandardMaterial({
color: accent,
emissive: accent,
emissiveIntensity: 0.25,
roughness: 0.38,
metalness: 0.18,
flatShading: true
});
const sphere = new THREE.Mesh(geometry, material);
sphere.position.y = SPHERE_Y;
sphere.castShadow = true;
sphere.receiveShadow = true;
group.add(sphere);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
new THREE.LineBasicMaterial({
color: 0xe7f8ff,
transparent: true,
opacity: 0.2
})
);
edges.position.copy(sphere.position);
group.add(edges);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(SPHERE_RADIUS * 1.13, 0.012, 6, 24),
new THREE.MeshStandardMaterial({
color: accent,
emissive: accent,
emissiveIntensity: 0.3,
roughness: 0.45,
metalness: 0.25,
flatShading: true
})
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.215;
group.add(ring);
const chargeSign = createChargeSign(sign, "#ffffff");
chargeSign.position.set(0, SPHERE_Y, 0.01);
group.add(chargeSign);
scene.add(group);
return { group, sphere, material };
}
const positive = makeProxy(0x49dcff, "+");
const negative = makeProxy(0xff668f, "−");
const positiveLabel = createPanelSprite();
const negativeLabel = createPanelSprite();
const positiveForceLabel = createPanelSprite();
const negativeForceLabel = createPanelSprite();
const distanceLabel = createPanelSprite();
positiveLabel.sprite.scale.set(1.34, 0.42, 1);
negativeLabel.sprite.scale.set(1.34, 0.42, 1);
positiveForceLabel.sprite.scale.set(1.12, 0.35, 1);
negativeForceLabel.sprite.scale.set(1.12, 0.35, 1);
distanceLabel.sprite.scale.set(1.08, 0.34, 1);
scene.add(
positiveLabel.sprite,
negativeLabel.sprite,
positiveForceLabel.sprite,
negativeForceLabel.sprite,
distanceLabel.sprite
);
function createForceArrow(color) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.22,
roughness: 0.42,
metalness: 0.14,
flatShading: true
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.027, 0.027, 1, 8),
material
);
shaft.castShadow = true;
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.092, 0.24, 8),
material
);
head.castShadow = true;
group.add(shaft, head);
scene.add(group);
return { group, shaft, head };
}
const leftArrow = createForceArrow(0x49dcff);
const rightArrow = createForceArrow(0xff668f);
function setArrow(arrow, direction, length) {
const headLength = 0.24;
const shaftLength = Math.max(0.08, length - headLength);
arrow.shaft.scale.set(1, shaftLength, 1);
arrow.shaft.rotation.z = direction > 0 ? -Math.PI / 2 : Math.PI / 2;
arrow.shaft.position.set(direction * shaftLength * 0.5, 0, 0);
arrow.head.rotation.z = direction > 0 ? -Math.PI / 2 : Math.PI / 2;
arrow.head.position.set(
direction * (length - headLength * 0.5),
0,
0
);
}
const dimensionGeometry = new THREE.BufferGeometry();
dimensionGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(18), 3)
);
const dimensionLine = new THREE.LineSegments(
dimensionGeometry,
new THREE.LineBasicMaterial({
color: 0x87aac2,
transparent: true,
opacity: 0.8
})
);
dimensionLine.frustumCulled = false;
scene.add(dimensionLine);
const qPlusInput = document.querySelector("#qPlus");
const qMinusInput = document.querySelector("#qMinus");
const separationInput = document.querySelector("#separation");
const qPlusOut = document.querySelector("#qPlusOut");
const qMinusOut = document.querySelector("#qMinusOut");
const separationOut = document.querySelector("#separationOut");
const forceValue = document.querySelector("#forceValue");
const forceUnit = document.querySelector("#forceUnit");
const calculation = document.querySelector("#calculation");
function formatForce(microNewtons) {
if (microNewtons >= 1000) {
return {
value: (microNewtons / 1000).toFixed(2),
unit: "mN"
};
}
return {
value: microNewtons < 1 ? (microNewtons * 1000).toFixed(1) : microNewtons.toFixed(1),
unit: microNewtons < 1 ? "nN" : "µN"
};
}
function updateDimension(left, right) {
const positions = dimensionGeometry.attributes.position;
const y = 0.285;
const z = 0.56;
positions.setXYZ(0, left, y, z);
positions.setXYZ(1, right, y, z);
positions.setXYZ(2, left, y - 0.07, z);
positions.setXYZ(3, left, y + 0.07, z);
positions.setXYZ(4, right, y - 0.07, z);
positions.setXYZ(5, right, y + 0.07, z);
positions.needsUpdate = true;
}
function updateModel() {
state.qPlus = Number(qPlusInput.value);
state.qMinus = Number(qMinusInput.value);
state.r = Number(separationInput.value);
const qPlusC = state.qPlus * 1e-9;
const qMinusC = state.qMinus * 1e-9;
// Coulomb magnitude in newtons. The sign of the interaction is
// represented by attraction; the two force vectors are equal/opposite.
const forceNewtons =
K * Math.abs(qPlusC * qMinusC) / (state.r * state.r);
const forceMicroNewtons = forceNewtons * 1e6;
const formatted = formatForce(forceMicroNewtons);
qPlusOut.textContent = `+${state.qPlus.toFixed(2)} nC`;
qMinusOut.textContent = `−${state.qMinus.toFixed(2)} nC`;
separationOut.textContent = `${state.r.toFixed(2)} m`;
forceValue.textContent = formatted.value;
forceUnit.textContent = formatted.unit;
calculation.textContent =
`8.9875517923×10⁹ · ` +
`(${state.qPlus.toFixed(2)}×10⁻⁹)` +
`(${state.qMinus.toFixed(2)}×10⁻⁹)` +
` / (${state.r.toFixed(2)})² = ` +
`${formatted.value} ${formatted.unit}`;
const halfSeparation = state.r * WORLD_UNITS_PER_METER / 2;
positive.group.position.x = -halfSeparation;
negative.group.position.x = halfSeparation;
positiveLabel.sprite.position.set(-halfSeparation, 1.13, 0);
negativeLabel.sprite.position.set(halfSeparation, 1.13, 0);
positiveLabel.draw("Positive proxy", `+${state.qPlus.toFixed(2)} nC`, "#49dcff");
negativeLabel.draw("Negative proxy", `−${state.qMinus.toFixed(2)} nC`, "#ff668f");
// Opposite charges attract:
// force on the left positive sphere points right;
// force on the right negative sphere points left.
const arrowLength = THREE.MathUtils.clamp(
0.70 + 0.47 * Math.log10(1 + forceMicroNewtons / 18),
0.70,
2.18
);
leftArrow.group.position.set(-halfSeparation, 0.49, 0.28);
rightArrow.group.position.set(halfSeparation, 0.49, -0.28);
setArrow(leftArrow, +1, arrowLength);
setArrow(rightArrow, -1, arrowLength);
positiveForceLabel.sprite.position.set(-halfSeparation, 0.79, 0.30);
negativeForceLabel.sprite.position.set(halfSeparation, 0.79, -0.30);
positiveForceLabel.draw(
"Force on +q",
`${formatted.value} ${formatted.unit}`,
"#49dcff"
);
negativeForceLabel.draw(
"Force on −q",
`${formatted.value} ${formatted.unit}`,
"#ff668f"
);
distanceLabel.sprite.position.set(0, 0.42, 0.58);
distanceLabel.draw("Center separation", `r = ${state.r.toFixed(2)} m`, "#c8deef");
updateDimension(-halfSeparation, halfSeparation);
}
qPlusInput.addEventListener("input", updateModel);
qMinusInput.addEventListener("input", updateModel);
separationInput.addEventListener("input", updateModel);
updateModel();
// Camera controls are implemented directly without Three.js addons.
const target = new THREE.Vector3(0, 0.38, 0);
let theta = 0.29;
let phi = 1.14;
let radius = 9;
let userZoomed = false;
function fittedRadius() {
const verticalFov = THREE.MathUtils.degToRad(camera.fov);
const fitHeight = 6.1 / (2 * Math.tan(verticalFov / 2));
const fitWidth =
8.1 / (2 * Math.tan(verticalFov / 2) * camera.aspect);
return THREE.MathUtils.clamp(Math.max(fitHeight, fitWidth), 8, 27);
}
function updateCamera() {
camera.position.setFromSphericalCoords(radius, phi, theta);
camera.lookAt(target);
}
function resetCamera() {
theta = 0.29;
phi = 1.14;
radius = fittedRadius();
userZoomed = false;
updateCamera();
}
const pointers = new Map();
let pinchDistance = 0;
let lastPointer = null;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
lastPointer = {
x: event.clientX,
y: event.clientY
};
} else if (pointers.size === 2) {
const points = [...pointers.values()];
pinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && lastPointer) {
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
theta -= dx * 0.007;
phi = THREE.MathUtils.clamp(phi - dy * 0.006, 0.30, 1.48);
lastPointer = {
x: event.clientX,
y: event.clientY
};
} else if (pointers.size === 2) {
const points = [...pointers.values()];
const newDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (pinchDistance > 0 && newDistance > 0) {
radius = THREE.MathUtils.clamp(
radius * (pinchDistance / newDistance),
4.5,
28
);
userZoomed = true;
}
pinchDistance = newDistance;
}
updateCamera();
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
lastPointer = { x: remaining.x, y: remaining.y };
} else {
lastPointer = null;
pinchDistance = 0;
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("lostpointercapture", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radius = THREE.MathUtils.clamp(
radius * Math.exp(event.deltaY * 0.001),
4.5,
28
);
userZoomed = true;
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.querySelector("#resetView").addEventListener("click", resetCamera);
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
if (!userZoomed) {
radius = fittedRadius();
}
updateCamera();
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
positive.material.emissiveIntensity = 0.23 + Math.sin(elapsed * 1.8) * 0.045;
negative.material.emissiveIntensity = 0.23 + Math.sin(elapsed * 1.8 + 1.2) * 0.045;
positive.sphere.rotation.y = elapsed * 0.11;
negative.sphere.rotation.y = -elapsed * 0.13;
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => loading.classList.add("hidden"));
</script>
</body>
</html>
```