time fix
This commit is contained in:
parent
069a494d39
commit
79dd7077af
@ -62,7 +62,7 @@ scene.add(sun);
|
|||||||
const segments = 128;
|
const segments = 128;
|
||||||
const orbitMaterial = new THREE.LineBasicMaterial({ color: 0xffffff, opacity: 0.5, transparent: true });
|
const orbitMaterial = new THREE.LineBasicMaterial({ color: 0xffffff, opacity: 0.5, transparent: true });
|
||||||
|
|
||||||
function createPlanet(radius, textureFile, distance, orbitSpeed) {
|
function createPlanet(radius, textureFile, distance, orbitalPeriodDays) {
|
||||||
const texture = textureLoader.load(`assets/textures/${textureFile}?v=${Date.now()}`);
|
const texture = textureLoader.load(`assets/textures/${textureFile}?v=${Date.now()}`);
|
||||||
const geometry = new THREE.SphereGeometry(radius, 32, 32);
|
const geometry = new THREE.SphereGeometry(radius, 32, 32);
|
||||||
const material = new THREE.MeshBasicMaterial({ map: texture }); // Reverted
|
const material = new THREE.MeshBasicMaterial({ map: texture }); // Reverted
|
||||||
@ -80,17 +80,17 @@ function createPlanet(radius, textureFile, distance, orbitSpeed) {
|
|||||||
const trail = new THREE.Line(trailGeometry, trailMaterial);
|
const trail = new THREE.Line(trailGeometry, trailMaterial);
|
||||||
scene.add(trail);
|
scene.add(trail);
|
||||||
|
|
||||||
return { planet, pivot, orbitSpeed, trail, trailPoints };
|
return { planet, pivot, orbitalPeriodDays, trail, trailPoints };
|
||||||
}
|
}
|
||||||
|
|
||||||
const mercuryData = createPlanet(0.4, 'mercury.jpg', 7, 0.0297);
|
const mercuryData = createPlanet(0.4, 'mercury.jpg', 7, 88);
|
||||||
const venusData = createPlanet(0.9, 'venus.jpg', 11, 0.0116);
|
const venusData = createPlanet(0.9, 'venus.jpg', 11, 225);
|
||||||
const earthData = createPlanet(1, 'earth.jpg', 15, 0.00717); // Adjusted for 365-day year
|
const earthData = createPlanet(1, 'earth.jpg', 15, 365);
|
||||||
const marsData = createPlanet(0.7, 'mars.jpg', 20, 0.0038);
|
const marsData = createPlanet(0.7, 'mars.jpg', 20, 687);
|
||||||
const jupiterData = createPlanet(3, 'jupiter.jpg', 30, 0.0006);
|
const jupiterData = createPlanet(3, 'jupiter.jpg', 30, 4333);
|
||||||
const saturnData = createPlanet(2.5, 'saturn.jpg', 40, 0.00024);
|
const saturnData = createPlanet(2.5, 'saturn.jpg', 40, 10759);
|
||||||
const uranusData = createPlanet(2, 'uranus.jpg', 50, 0.000085);
|
const uranusData = createPlanet(2, 'uranus.jpg', 50, 30687);
|
||||||
const neptuneData = createPlanet(1.9, 'neptune.jpg', 60, 0.000043);
|
const neptuneData = createPlanet(1.9, 'neptune.jpg', 60, 60190);
|
||||||
|
|
||||||
// --- Saturn's Rings ---
|
// --- Saturn's Rings ---
|
||||||
const ringGeometry = new THREE.RingGeometry(3.5, 5, 64);
|
const ringGeometry = new THREE.RingGeometry(3.5, 5, 64);
|
||||||
@ -293,7 +293,7 @@ pauseButton.addEventListener('click', () => {
|
|||||||
pauseButton.textContent = isPaused ? 'Play' : 'Pause';
|
pauseButton.textContent = isPaused ? 'Play' : 'Pause';
|
||||||
});
|
});
|
||||||
|
|
||||||
const speeds = [1, 100, 500];
|
const speeds = [1, 100, 500, 1000, 10000];
|
||||||
let currentSpeedIndex = 0;
|
let currentSpeedIndex = 0;
|
||||||
let timeScale = speeds[currentSpeedIndex]; // Start with 1x speed
|
let timeScale = speeds[currentSpeedIndex]; // Start with 1x speed
|
||||||
|
|
||||||
@ -308,8 +308,9 @@ speedToggleButton.addEventListener('click', () => {
|
|||||||
const clock = new THREE.Clock();
|
const clock = new THREE.Clock();
|
||||||
const timeContainer = document.getElementById('time-container');
|
const timeContainer = document.getElementById('time-container');
|
||||||
let simulationTime = new Date();
|
let simulationTime = new Date();
|
||||||
const ORBIT_SPEED_MULTIPLIER = 0.1;
|
const TIME_SPEED_FACTOR = 60; // 1 real second = 1 simulation minute at 1x
|
||||||
const TIME_SPEED_FACTOR = 3600; // 1 real second = 1 simulation hour at 1x
|
const MS_IN_A_DAY = 24 * 60 * 60 * 1000;
|
||||||
|
const MOON_ORBIT_DAYS = 27.3;
|
||||||
|
|
||||||
|
|
||||||
function updateTimeDisplay() {
|
function updateTimeDisplay() {
|
||||||
@ -327,7 +328,9 @@ function animate() {
|
|||||||
requestAnimationFrame(animate);
|
requestAnimationFrame(animate);
|
||||||
|
|
||||||
// Get time delta for frame-rate independent animation
|
// Get time delta for frame-rate independent animation
|
||||||
const delta = clock.getDelta();
|
let delta = clock.getDelta();
|
||||||
|
// Clamp the delta to a maximum value to prevent large jumps, e.g., on the first frame or after tab refocus.
|
||||||
|
delta = Math.min(delta, 0.1);
|
||||||
|
|
||||||
// Update controls
|
// Update controls
|
||||||
controls.update();
|
controls.update();
|
||||||
@ -344,8 +347,9 @@ function animate() {
|
|||||||
const worldPosition = new THREE.Vector3();
|
const worldPosition = new THREE.Vector3();
|
||||||
|
|
||||||
planets.forEach(p => {
|
planets.forEach(p => {
|
||||||
// Orbit speed is now correctly scaled by timeScale
|
const orbitalPeriodMs = p.orbitalPeriodDays * MS_IN_A_DAY;
|
||||||
p.pivot.rotation.y += p.orbitSpeed * ORBIT_SPEED_MULTIPLIER * delta * timeScale;
|
const angleIncrement = (incrementMs / orbitalPeriodMs) * (2 * Math.PI);
|
||||||
|
p.pivot.rotation.y += angleIncrement;
|
||||||
|
|
||||||
// Planet's self-rotation (can also be constant)
|
// Planet's self-rotation (can also be constant)
|
||||||
p.planet.rotation.y += 0.05 * delta;
|
p.planet.rotation.y += 0.05 * delta;
|
||||||
@ -362,7 +366,9 @@ function animate() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Moon's orbit (also scaled by timeScale)
|
// Moon's orbit (also scaled by timeScale)
|
||||||
moonPivot.rotation.y += 0.1 * ORBIT_SPEED_MULTIPLIER * delta * timeScale;
|
const moonOrbitalPeriodMs = MOON_ORBIT_DAYS * MS_IN_A_DAY;
|
||||||
|
const moonAngleIncrement = (incrementMs / moonOrbitalPeriodMs) * (2 * Math.PI);
|
||||||
|
moonPivot.rotation.y += moonAngleIncrement;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update time display every frame
|
// Update time display every frame
|
||||||
@ -371,6 +377,7 @@ function animate() {
|
|||||||
renderer.render(scene, camera);
|
renderer.render(scene, camera);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
animate();
|
animate();
|
||||||
|
|
||||||
// Handle window resize
|
// Handle window resize
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user