Charge in an elastic solid (wobble)
by Chantal Roth
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Spin 1/2 Simulation with Scene Rotation</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
#gui-container {
position: absolute;
top: 10px;
left: 10px;
}
</style>
</head>
<body>
<!-- Include Three.js library -->
<script src="https://cdn.jsdelivr.net/npm/three@0.124.0/build/three.min.js"></script>
<!-- Include OrbitControls -->
<script src="https://cdn.jsdelivr.net/npm/three@0.124.0/examples/js/controls/OrbitControls.js"></script>
<!-- Include dat.GUI library for controls -->
<!-- Include dat.GUI library for controls -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.min.js"></script>
<div id="gui-container"></div>
<script>
</script>
</body>
</html>
JavaScript
// Import necessary THREE.js components (assuming you have them included in your HTML)
// import * as THREE from 'three';
// import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
// import * as dat from 'dat.gui';
// Set up the Three.js scene, camera, and renderer
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeeeeee); // Light grey background
let sphereRadius = 1; // Radius of the central sphere
// Create a perspective camera
const camera = new THREE.PerspectiveCamera(
45, // Field of view
window.innerWidth / window.innerHeight, // Aspect ratio
0.1, // Near clipping plane
1000 // Far clipping plane
);
camera.position.set(0, 10, 20); // Position the camera
camera.lookAt(0, 0, 0); // Make the camera look at the origin
// Create the WebGL renderer
const renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight); // Set the render size
document.body.appendChild(renderer.domElement); // Add the renderer to the HTML document
// Add OrbitControls for interactive rotation, panning, and zooming
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableRotate = true;
controls.rotateSpeed = 1.0;
controls.enablePan = true;
controls.enableZoom = true;
// Set controls to rotate with right mouse button
controls.mouseButtons = {
LEFT: THREE.MOUSE.PAN,
MIDDLE: THREE.MOUSE.DOLLY,
RIGHT: THREE.MOUSE.ROTATE
};
// Parameters for the simulation
const params = {
sphereRadius: 1, // Peak radius where wobble is maximal
showArrows: true,
showGrid: true,
showSphere: true, // Toggle to show/hide the central sphere
mode: 'Plus Charge', // Simulation mode: Plus charge or minus charge
gridSize: 40, // Number of divisions along one axis of the...