confused by type, PVector but .x or other methods won't work

edited February 2017 in Questions about Code

I have a 2d Arraylist, quite new with this so I might have an error in the creation of it. Can't seem to figure out what's wrong. Here's creating the arrays in settings():

for (int x = 0; x < width+gridSize; x=x+gridSize) {
        ArrayList vList = new ArrayList();

        for (int z = 0; z < height+gridSize; z=z+gridSize) {
            vList.add(new PVector(x,0,z));
        }

        vectorList.add(vList);

    }

then in draw()

for (int i = 0; i < vectorList.size(); ++i) {
        ArrayList vList = vectorList.get(i);
        for (int j = 0; j < vList.size(); ++j) {
            Object v = vList.get(i);
            println("v: "+v.getClass());

the getClass gives me: class processing.core.PVector but v.x: x cannot be resolved or is not a field and v.array: The function array() does not exist.

I'm lost here.. It's a PVector right? Why can't I access it methods?

Answers

  • Line 2 belongs outside/ before loops in the first code segment

    You are producing an ArrayList of ArrayList of PVector. Completely complex. Why?

    2nd code segment:

    line 4: PVector v = .....

    println (v.x);

  • Line 2 belongs outside/ before loops in the first code segment

    Not if he's creating a list of lists - line 2 would be creating the inner list.

    I think we need to see more code because there are important things going on here that we aren't being shown. Like the definition of vectorlist.

  • Thanks for the quick answer @chrisir !

    Ok let me elaborate, I want to draw A shapes (depends on the gridSize) and each of those shapes have B coordinates. Basically the shapes are lines within a grid and I want to be able to manipulate each of the coordinates per shape independently.

    When I write line 4 Pvector v = I get the error: cannot convert from Object to PVector Although getClass says it's a PVector.. So I'm very confused on this one. Sorry I forgot to add this :)

  • Answer ✓

    PVector v = (PVector) vList.....

    Maybe look at beginShape and vertex to store a shape

  • In theory you can tell the ArrayList its type

    Not sure in your case with a nested ArrayList though

        ArrayList <PVector> list = new ArrayList ();
    
  • @chrisir That way of converting works! @koogs Here is the rest of the code (removed all libraries):

    I just don't get to see any points and lines yet..

    int gridSize = 50;
    
    ArrayList<ArrayList<PVector>> vectorList = new ArrayList();
    
    void settings() {
        size(1200, 1200, P3D);
    }
    
    void setup() {
        frameRate(90);
    
        for (int x = 0; x < width+gridSize; x=x+gridSize) {
            ArrayList vList = new ArrayList();
    
            for (int z = 0; z < height+gridSize; z=z+gridSize) {
                vList.add(new PVector(x,0,z));
            }
    
            vectorList.add(vList);
    
        }
    }
    
    
    void draw() {
        surface.setTitle(int(frameRate) + " fps");
    
        translate(-width/2+gridSize, 0);
        background(0);
        stroke(255);
        fill(255);
    
        for (int i = 0; i < vectorList.size(); ++i) {
            ArrayList vList = vectorList.get(i);
            beginShape(LINES);
            for (int j = 0; j < vList.size(); ++j) {
                PVector v = (PVector) vList.get(i);
                // println("v.x: "+v.x);
                // println("v.y: "+v.y);
                // println("v.z: "+v.z);
                vertex(v.x, v.y, v.z);
            }
            endShape(CLOSE);
        }
    
    }
    
  • Answer ✓

    Line 37

    j !!!!!!!!

  • Answer ✓

    Not i

  • Not sure but might look better with lights();

  • thanks @chrisir ! Always the obvious little things :)

  • I got eyes like an eagle.

    ;-)

  • Try lights();

  • @chrisir no difference with the lights(). Perhaps with shaders I could do something but I just started learning how to use them (the pain in my head trying to understand it). So right now they're just vertices and lines which I want to animate with OSC (from audio analysing songs) and with controlP5 controls.

    but after replacing i and j I started seeing the vertices :)

  • in my version you can see the effect of lights() :

    ArrayList<ArrayList<PVector>> vectorList = new ArrayList();
    float a; 
    
    void settings() {
      size(1200, 1200, P3D);
    }
    
    void setup() {
      // frameRate(90);
    
      int gridSize = 50;
    
      for (int x = -(width+gridSize)/2; x < (width+gridSize)/2; x+=gridSize) {
        ArrayList vList = new ArrayList();
    
        for (int z = -(height+gridSize)/2; z < (height+gridSize)/2; z+=gridSize) {
          vList.add(new PVector(x, z, z));
          vList.add(new PVector(x, z+66, z));
        }
    
        vectorList.add(vList);
      }//for
    }//func 
    
    
    void draw() {
      surface.setTitle(int(frameRate) + " fps");
      background(0);
      lights(); 
    
      translate(width/2, height/2, -800);
      rotateX(.16);
      rotateY(a);
    
      stroke(255);
      fill(255, 0, 0); // RED 
    
      for (int i = 0; i < vectorList.size(); i++) {
        shapeFromArrayList(vectorList.get(i));
      }
      a+=.031;
    }
    
    void shapeFromArrayList(ArrayList<PVector> localList) {
      //
      rotate(.2); 
      // beginShape(LINES);
      beginShape(QUAD_STRIP);
      // beginShape(TRIANGLES);
    
      for (int j = 0; j < localList.size(); j++) {
        PVector v = localList.get(j);
        // println("v.x: "+v.x);
        // println("v.y: "+v.y);
        // println("v.z: "+v.z);
        vertex(v.x, v.y, v.z);
      }
      endShape(CLOSE);
    }
    //
    
  • If the object isn't changing after being defined in setup () then you should look into using PShape, which you define once and then draw using a single call.

  • Thanks!

    In flowens code I see only lines, maybe you can explain what your goal is

  • he @chrisir that's some cool stuff! I'll inspect the code later to see how you made this.

    I created this myself:

    https://www.instagram.com/p/BQx3b1dA6c5/ https://www.instagram.com/p/BQzLsYwAiAZ/

    I'm using Coge (imimot.com) on top to layer some effects like mirrors and audio-analyzing. I send OSC as well and some controls in the sketch to manipulate things around. And use Syphon to stream the pixeldata from one application to the other (so from processing to Coge) and record it as well

  • Answer ✓

    @flowen Really cool work.

    Kf

  • @kfrajer thanks man! appreciate it :)

  • Answer ✓

    Very impressive!

  • @chrisir thanks to you and the forum I was able to finish it!

Sign In or Register to comment.