what's wrong with my if statement?

edited June 2014 in Questions about Code

Hi there, I'm having a problem with this code. I will just explain its function here, rather than the whole project

In the Node class spawnNewLocation() drops a new rectangle every 30 frames. It is supposed to pick an angle, and move outward until it isn't over any other rectangles, then stay there. The issue I'm having is that after a few rectangles are dropped, they simply fly off the screen.

I have tried 'faking' the scenario to verify checkRectsAreOver() boolean and can't reproduce the error in isolation. (Does that make sense?)

Any help will be sincerely appreciated. I haven't posted here before, so any feedback or further questions will be helpful too.

-Lotu

// ** Node Class ** \\
Node[] nodes;

void setup() {
  size(1000, 800, P2D);
  colorMode(HSB, 360, 100, 100, 100);
  smooth();
  nodes();
  stroke(0);
  fill(255);
  rectMode(CENTER);
  ellipseMode(CENTER);
}

void draw() {   
  background(360);
  for (int i = 0; i < nodes.length; i++) {
    nodes[i].drawNodes();
  }
}



void nodes() {
  // Nodes \\
  nodes = new Node[0];
  rectMode(CENTER);
  Node tempNode = new Node(nodes.length + 1, 300, 200);
  nodes = (Node[]) append(nodes, tempNode);
}


class Node {
  float x, y; // position
  float drainX, drainY; // where the node joins the river

  int index;

  PVector[] locations;
  PVector[] sizes;
  boolean[] movable;

  boolean active = true;
  int startTime = millis();

  int nodeDiam = 20;

  PVector nodeVector;

  Node(int tempIndex, float xVal, float yVal) {
    index = tempIndex;
    x = xVal;
    y = yVal;
    locations = new PVector[0];
    movable = new boolean[0];

    nodeVector = new PVector(x, y);
  }


  void drawNodes() {
    if (frameCount % 30 == 0) {
      spawnNewLocation();
    }
    strokeWeight(1);
    stroke(360);
    fill(0);
    for (int i = locations.length-1; i > 0; i--) {
      if (movable[i]) {
        moveRectsIntoPlace(locations[i].x, locations[i].y, sizes[i].x, sizes[i].y, i);
      }
      float tx = locations[i].x;
      float ty = locations[i].y;
      int w = 10; // x-Size
      int h = 10; // y-Size
      pushMatrix();
      translate(x, y);
      rect(tx, ty, w, h);
      popMatrix();
    }

    fill(200);
    ellipse(x, y, nodeDiam, nodeDiam);
  }

  void spawnNewLocation() {
    float newLocX = 0;
    float newLocY = 0;
    float newSizeX = 20;
    float newSizeY = 20;
    float angle = randomGaussian() * (TWO_PI);
    newLocX = sin(angle) * nodeDiam / 2;
    newLocY = cos(angle) * nodeDiam / 2;

    // println("Location number: " + locations.length + " newLocX: "+newLocX + " newLocY: "+newLocY);
    PVector newLoc = new PVector(newLocX, newLocY);
    locations = (PVector[]) append(locations, newLoc);

    PVector newSize = new PVector(newSizeX, newSizeY);
    sizes = (PVector[]) append(locations, newSize);

    boolean isMovable = true;
    movable = (boolean[]) append(movable, isMovable);
  }


  void moveRectsIntoPlace(float newLocX, float newLocY, float newSizeX, float newSizeY, int index) {
    // float j = 1;
    if (checkRectsAreOver(index, locations, sizes, newLocX, newLocY, newSizeX, newSizeY)) {
      PVector temp = new PVector(newLocX, newLocY);
      // // println ("- over " + i + " at " + " temp with magnitude " + temp.mag());
      float j = temp.mag();
      temp.setMag(j+0.5);
      locations[index].set(temp);
    } else {
      println(index + " shouldn't move any more");
      movable[index] = false;
    }
  }


  boolean checkRectsAreOver (int index, PVector[] l, PVector[] d, float x1, float y1, float w1, float h1) {
    boolean isOver = false;
    for (int i = 0; i < l.length; i++) {
      if (i != index) {
        float x2 = l[i].x;
        float y2 = l[i].y;
        float w2 = d[i].x;
        float h2 = d[i].y;
        if  ( 
        (abs(x1 - x2) < abs(w2 - abs(w2-w1)/2))
          && 
          (abs(y1 - y2) < abs(h2 - abs(h2-h1)/2))
          ) {
          isOver = true;
          println(index + " is over " + i);
          break;
        }
      }
    }
    return isOver;
  }
}
Tagged:

Answers

  • Still didn't dive into your code. Just noticed you're using append() all the time.
    If you need append(), it means a regular array isn't enough anymore!
    You should use ArrayList in those cases instead:

    http://processing.org/reference/ArrayList.html

  • Thanks, though I don't believe that is the source of my problem, but perhaps I should be just using expand(list, list.length+1); instead in this case?

Sign In or Register to comment.