#Mesh positions not updating with provided coordinates.

4 messages · Page 1 of 1 (latest)

dusk zephyr
#

LOOP ADDING ALL THE PLANETS TO THE SCENE

for (let i = 0; i < planets.length; i++) {
          const planet = new Planet(planets[i]);
          this.scene.add(planet.mesh);
          //add the body to the scene
          this.world.addBody(planet.body);
          console.log(planet.body);
        }

ANIMATE FUNCTION

animate() {

      const timeElapsed = 1/60;
      const dt = timeElapsed;

        //update the world
        this.world.step(1/60);

        // Update the positions of the planets
        for (let i = 0; i < planets.length; i++) {
          planets[i].calculateOrbit(dt);
          //set planets mesh to the planets body position
          planets[i].mesh.position.copy(planets[i].body.position);
          //animate the mesh to follow the updated position
          planets[i].mesh.quaternion.copy(planets[i].body.quaternion);
          planets[i].mesh.rotateY(0.01);
        }

        updateMeshes();

        //call the distanceBetweenEarthAndSun function
        this.distanceBetweenEarthAndSun();
        this.controls.update(dt);


        requestAnimationFrame(this.animate.bind(this));
        
        this.renderer.render(this.scene, this.camera);
    }```
#
calculateOrbit() {
    //set mesh equal to the planets mesh not createMesh
    const mesh = this.mesh

    let currentTime = 0
    // Calculate the elapsed time since the previous update
    const elapsedTime = (Date.now()/10000) - currentTime;
    currentTime = Date.now(); // Update current time to the current time

    // Calculate the new mean anomaly based on the elapsed time
    const M = this.meanAnomaly + this.orbitalVelocity * elapsedTime / 86400;

    // The rest of the function can stay the same as before
    const a = (this.aphelion + this.perihelion) / 2;
    const e = (this.aphelion - this.perihelion) / (this.aphelion + this.perihelion);
    const E = this.solveKepler(M, e);
    const x = a * (Math.cos(E) - e);
    const y = a * Math.sqrt(1 - e**2) * Math.sin(E);
    mesh.position.set(x, y, 0);
    this.body.position.set(x, y, 0);
    this.body.velocity.set(this.orbitalVelocity * Math.cos(E), this.orbitalVelocity * Math.sin(E), 0);
    mesh.rotation.z += this.rotationPeriod * elapsedTime / 86400;
    
    

    
  }```
#

MY FUNCTIONS CREATING THE CANNON BODY AND THREE MESH

createBody(meanAnomaly) {
    // Calculate the velocity using the mean anomaly
    const velocity = this.solveKepler(meanAnomaly, this.eccentricity);
  
    // Create a Cannon.js body for the planet
    const shape = new CANNON.Sphere(this.diameter);
    const body = new CANNON.Body({
      mass: this.mass,
      position: new CANNON.Vec3(this.position.x, this.position.y, this.position.z),
      velocity: new CANNON.Vec3(velocity.x, velocity.y, velocity.z),
      shape: shape,
    });
  
    return body;
  }


  createMesh() {
    // Create a Three.js mesh for the planet using an image texture
    const geometry = new THREE.SphereGeometry(this.diameter, 64, 64);
    const texture = new THREE.TextureLoader().load(`images/${this.name}.jpeg`);
    const material = new THREE.MeshBasicMaterial({ map: texture });
    const mesh = new THREE.Mesh(geometry, material);

    // Set the position of the mesh
    mesh.position.set(this.position.x, this.position.y, this.position.z) // Set the position of the mesh
    mesh.rotation.z = this.obliquityToOrbit * Math.PI / 180; // Set the rotation of the mesh
    //set the mesh postion to copy the body position
    if(this.body){
    mesh.position.copy(this.body.position)
    }
    return mesh
  }```
dusk zephyr
#

Mesh positions not updating with provided coordinates.