Home > Blockchain >  Why can't I declare a position in my multidimensional array?
Why can't I declare a position in my multidimensional array?

Time:05-07

I'm trying to do a 3d version of an L-Syste. But I cannot declare elements in my multidimensional array. When I debug, I see that the element stays the same. I call for example arraygrowth [0][1] = 100; But the Element at that position stays 0

let arraygrowth =[];
arraygrowth.push(new THREE.Vector3(0,0,0)); 
arraygrowth.push(new THREE.Vector3(0,0,0));
arraygrowth.push(new THREE.Vector3(0,0,0));

arraygrowth [0][1] = 100;
arraygrowth [1][1] = 100;
arraygrowth [2][1] = 100;

CodePudding user response:

Cleaning up your code, you're essentially doing this:

let arraygrowth =[];
arraygrowth.push(new THREE.Vector3(0,0,0)); 
arraygrowth.push(new THREE.Vector3(0,0,0));
arraygrowth.push(new THREE.Vector3(0,0,0));

arraygrowth [0][1] = 100;
arraygrowth [1][1] = 100;
arraygrowth [2][1] = 100;

This is the equivalent of new THREE.Vector3(0, 0, 0)[1] = 100; ... which doesn't make sense. That this is not how you change the x, y, z parameters of a Vector3. When you call arraygrowth[0], you get a Vector3, and now you have to use one of its properties or methods to assign the value you want. For example:

arraygrowth[0].y = 100; // Sets y value
arraygrowth[0].setY(100); // Also sets y value
arraygrowth[0].setComponent(1) = 100; // Also sets y value

https://threejs.org/docs/#api/en/math/Vector3.setComponent

  • Related