Javascript setting this.value is altering the temp variable
-
12-12-2019 - |
Question
I have a temp variable a THREE.Veector3 which I pass into the creation of an object
object[i] = new Object(new THREE.Vector3(0,0,0));
In the class Object I have a variable texture which I set to the new THREE.Vector3 called pos
function Object(pos){
this.texture = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map:
THREE.ImageUtils.loadTexture( '../img/img.png' ) } ) );
this.texture.position = pos;
this.texture.rotation.x = 0;
this.texture.rotation.y = 1.57;
this.texture.rotation.z = 1.57;
this.texture.scale.x = 100;
this.texture.scale.y = 50;
this.texture.scale.z = 100;
this.texture.position.y = 1000;
}
The problem I'm having is that I want to pass pos into another object created inside this Object.
object2 = new Object2(pos);
However pos has been modified to have the y at 1000. I'm not quite sure why this is happening I read some guides on passing variables but I'm still a bit confused on how the pos variable is modified. Any help would be much appreciated.
Solution
Just change your object to always take a clone of the position
function Object(pos){
this.texture = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map:
THREE.ImageUtils.loadTexture( '../img/img.png' ) } ) );
this.texture.position = pos.clone();
this.texture.rotation.x = 0;
this.texture.rotation.y = 1.57;
this.texture.rotation.z = 1.57;
this.texture.scale.x = 100;
this.texture.scale.y = 50;
this.texture.scale.z = 100;
this.texture.position.y = 1000;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow