The code is a simulation of the solar system where it is possible to visualize bodies through a spacecraft piloted by the user
Question
I have a problem that I am somewhat aware of, the further I get away from the center of the map, the more the ship starts to have violent tremors and even teleports. I need tips on what I can do to avoid this. I’ll leave the video below of how it goes. The objects that use the actual size scale, and I need to maintain that.
2023-11-30 21-57-10.mkv (7.5 MB)
That looks like numeric instability caused by your coordinates getting too large. You may have to scale down the visual size of your solar system by 1000 or so. This is a common problem in 3d and the only ways around it really, are to either scale down your universe size, or restructure your app so that the ship stays at 0,0,0 and the rest of the scene moves around it. (which can be a conceptual pain). I’m assuming your sun is at 0,0,0? What units are you using … does 1 = 1 kilometer?
1 Like
I’m using data taken directly from NASA, so yes, these are huge numbers. I thought about making the world move instead of the ship, is there somewhere that explains how to do this? I can even manage, but an explanation would help.
There are a lot of different approaches, and they all kinda suck for one reason or another. What happens if you scale the scene root node to .0001 scene.scale.multiplyScalar(.0001) ?
Well, following your suggestion, I put the ship to stay still and just move the scene around, but my camera is moving instead of staying still next to the ship, could you look at the code and tell me what that would be?
CODE
function animateSpaceship() {
const acceleration = 5000;
const deceleration = 0.01;
const maxSpeed = velocidadeDaLuz ? 29979245868 : 1000000;
const maxCameraDistance = 0.0005;
const minCameraDistance = 0.0002;
if (spaceshipAccelerating) {
if (velocidadeDaLuz) {
spaceshipSpeed = maxSpeed;
} else {
spaceshipSpeed += acceleration;
}
} else {
spaceshipSpeed -= deceleration * spaceshipSpeed;
}
spaceshipSpeed = Math.max(spaceshipSpeed, 0);
spaceshipSpeed = Math.min(spaceshipSpeed, maxSpeed);
const direction = new THREE.Vector3(0, 0, 1);
direction.applyQuaternion(spaceship.quaternion);
const moveDistance = direction.multiplyScalar(spaceshipSpeed * clock.getDelta());
// Mova a cena na direção oposta para dar a impressão de que a nave está parada
scene.position.sub(moveDistance);
targetCameraDistance = minCameraDistance + (maxCameraDistance - minCameraDistance) * (spaceshipSpeed / maxSpeed);
cameraDistance += (targetCameraDistance - cameraDistance);
const cameraOffset = new THREE.Vector3(0, 0.00001, -cameraDistance);
const relativeCameraOffset = cameraOffset.applyQuaternion(spaceship.quaternion);
const cameraPosition = spaceship.position.clone().add(relativeCameraOffset);
camera.position.copy(cameraPosition);
const forwardDirection = new THREE.Vector3(0, 0, 1);
forwardDirection.applyQuaternion(spaceship.quaternion);
const lookAtPosition = new THREE.Vector3().addVectors(spaceship.position, forwardDirection);
const upDirection = new THREE.Vector3(0, 1, 0);
upDirection.applyQuaternion(spaceship.quaternion);
camera.up.copy(upDirection);
camera.lookAt(lookAtPosition);
spaceship.rotation.z = 0;
spaceship.children[1].setVolume(spaceshipSpeed / maxSpeed);
if (spaceshipSpeed > 0 && !spaceship.children[1].isPlaying) {
spaceship.children[1].play();
} else if (spaceshipSpeed === 0 && spaceship.children[1].isPlaying) {
spaceship.children[1].pause();
}
const spaceshipSpeedKM = spaceshipSpeed * escalaModelo;
document.getElementById('velocidadeNave').textContent = spaceshipSpeedKM.toFixed(0) + ' KM/S';
}
you probably don’t want the camera added to the scene… not sure if thats happening in your code?
I’ll explain better, I set the scene to move and not the ship, because this gives the impression that the ship is moving more and the scene, thus avoiding problems of agitation due to distances. The code I sent is doing this, the problem is that the camera is moving and is not staying there next to the ship.
Ohh, ok… can you ship.add( camera ) instead?
I’m going to try to separate the camera from the rest of the scene like I did with the ship, because when the scene moves it ends up moving together.
Have a look at the way three-mesh-bvh - Character Movement manages the camera position, it may be useful, the camera is always independent of the scene but copies the player position and then subtracts the camera distance vector to always be behind the character (unless 1st person is checked)
2 Likes