Home > Software engineering >  How can I use my "render" function in a different class in three.js?
How can I use my "render" function in a different class in three.js?

Time:06-22

I'm new to three.js and I am making a bowling game. But I have an issue here, I made the physics in a class and now I need to access a function from my "Application" class. I really don't get the issue here and I'm pretty lost.

Application Class:

export class Application {
    constructor() {  
        this.objects = [];
        this.createScene();
    }

    createScene() {
        this.scene = new THREE.Scene();
        this.camera = new THREE.PerspectiveCamera(60,
        window.innerWidth / window.innerHeight, 1, 10000);

        this.renderer = new THREE.WebGLRenderer();
        this.renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(this.renderer.domElement);
        this.render();

    }

    getMesh(){
        return this.curveObject;
    }
    update(){
        
    }

    render() {
        requestAnimationFrame(() => {    
        this.render();
    });
        this.objects.forEach((object) => {
        object.update();
    });
        this.renderer.render(this.scene, this.camera);
    }
}

Animate Function inside "Physics" class:

  animate(){
       if (this.pinTest){
      //  console.log(this.pinTest);
        this.pin1Mesh.position.copy(this.pin1Body.position);
        this.pin1Mesh.quaternion.copy(this.pin1Body.quaternion);
        this.pinTest.position.copy(this.pin1Body.position);
        this.pinTest.quaternion.copy(this.pin1Body.quaternion);
    }
    // I need to call render here
    //this.renderer.render(this.scene, this.camera);
}

CodePudding user response:

Base class example:

class baseAnimation {
  animate() {
    if (this.pinTest) {
      //  console.log(this.pinTest);
      this.pin1Mesh.position.copy(this.pin1Body.position);
      this.pin1Mesh.quaternion.copy(this.pin1Body.quaternion);
      this.pinTest.position.copy(this.pin1Body.position);
      this.pinTest.quaternion.copy(this.pin1Body.quaternion);
    } else
      console.log("pinTest === false");
  }
}

class Physics extends baseAnimation {

}



let test = new Physics();

test.pinTest = false;

test.animate();

Passing an object to an Animation class (using static method) example:

class Animation {
  static animate(obj) {
    if (obj.pinTest) {
      //  console.log(this.pinTest);
      obj.pin1Mesh.position.copy(obj.pin1Body.position);
      obj.pin1Mesh.quaternion.copy(obj.pin1Body.quaternion);
      obj.pinTest.position.copy(obj.pin1Body.position);
      obj.pinTest.quaternion.copy(obj.pin1Body.quaternion);
    } else
      console.log("pinTest === false");
  }
}

class Physics {

}

let test = new Physics();

test.pinTest = false;

Animation.animate(test);

  • Related