Armend Veseli
Armend Veseli

Reputation: 143

Three.js 3D Scene, Uncaught Type Error: "Cannot read property 'x' of undefined" Vertex/Particle Position

I'm attempting to create a randomly distributed particle field that exhibits Brownian (random) motion.

Scroll to the bottom of the code to see where the error happens. I'm trying to set the position of a single vertex with position.x.

I'll omit the rest of the code not directly related to rendering the particles in effort to save your time.

//Using WebGL renderer...

var particle, particles = [], particle_system, material, p_x, p_y, p_z;

particles = new THREE.Geometry(); 

material = new THREE.ParticleBasicMaterial({color: 0xffffff, size: 1});

for(var count = 0; count < 1000; count++){

    p_x = Math.random() * 1000 - 500;

    p_y = Math.random() * 1000 - 500;

    p_z = Math.random() * 1000 - 500;

    particle = new THREE.Vector3(p_x, p_y, p_z);

    particles.vertices.push(particle);

}

particle_system = new THREE.ParticleSystem(particles, material);

scene.add(particle_system);

particle_system.geometry.dynamic = true;

//All of the code bellow will go into the render loop.    

var index = 0;

while(index < 1000){

    index++;

    particle_system.geometry.verticiesNeedUpdate = true;

    //THESE 3 LINES BELLOW CAUSE THE ERROR

    particles.vertices[index].position.x += Math.random() * 1000 - 500;

    particles.vertices[index].position.y += Math.random() * 1000 - 500;

    particles.vertices[index].position.z += Math.random() * 1000 - 500;

}

Upvotes: 0

Views: 4537

Answers (1)

vincent
vincent

Reputation: 1533

  • verticiesNeedUpdate should be spelled verticesNeedUpdate
  • your while loop is wrongly incrementing the index variable before it is used. So in the last iteration (when index == 999) you try to access particles.vertices[1000] which is not defined

Upvotes: 2

Related Questions