FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   Yikes!  Help needed wrestling a tornado...
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Yikes!  Help needed wrestling a tornado...  (Read 388 times)
richaman


Yikes!  Help needed wrestling a tornado...
« on: Dec 13th, 2004, 3:31am »

Yes, I'm a newbie, albeit an old one, struggling with what aparently are basic concepts like objects and arrays.  
 
I'm trying to create a little 3D environment that the user can interact with.  I wrote this little tornado thing and I learned a lot about rotation and matrices from it, but I want the tornado to change the color (at least!) of the rectangles as it passes over them - a veritable path of devestation.  I also wanted to lose the cursor, but that's minor.
 
Here it is:
 
///////////////////////////////////////////////////
 
void setup(){
  size(500, 530);
  ellipseMode(CENTER_DIAMETER);
  noStroke();
  //framerate(10);
  noCursor();
}
 
void loop(){
  int D = 11;
 
  background(230, 230, 0);
  translate(250, 100);
  rotateX(PI/3.1417);
  fill(163, 171, 20);
  for(float XX = -width; XX < 1.5*width; XX +=(1.15*D)){
    for(float YY = -height/2; YY < height; YY +=(2.15*D)){
 rectMode(CENTER_DIAMETER);
 rect(XX, YY, D, 2*D);
 
    }
  }
 
  push();  //save this position for future use...
  translate(50, 150, 15);
  rotateX(PI/2);
  fill(100);
  ellipse(10, 30, 50, 50);
  fill(160);
  translate(-100, 10, -245);
  ellipse(20, 10, 30, 30);
  pop();  //back to the original position
 
 
 
  println("mouseY is" + mouseY +  "mouseX is" + mouseX);
 
  translate(mouseX-250, 1.3*mouseY-250, 20);
  rotateZ(millis()*.01);
  rotateX(PI/3);
  //rotateY(PI*T*0.005);
  //stroke(0, 255, 0);
  fill(0);
  ellipse(0, 0 , 7, 7);
  fill(33);
  ellipse(9, 15 , 7, 13);
  ellipse(-9, 37 , 3, 9);
  rotateZ(millis()*.0015);
  fill(100, 33, 76);
  ellipse(8, 26 , 7, 13);
  ellipse(-3, 20 , 9, 5);
  ellipse(-11, 35 , 3, 4);
   
  fill(27, 73, 3);
  ellipse(1, 39 , 3, 2);
  ellipse(8, 26 , 11, 13);
 
}
 
///////////////////////////////////////////////////
 
 
I've been working all day at recreating the rectangle field as an array of objects by  adapting the code Reas wrote for the Learning section, but I've  completely lost myself.  I keep getting ArrayOutOfBoundsException errors when I try to initialize my class (I think that's what I'm doing, anyway).  
 
Here's that code:
 
///////////////////////////////////////////////////
 
 
// Array Objects
// by REAS <http://www.groupc.net>
 
int spacing = 40;
int MAX;
color autumnColor;
color destructionColor;
color currentColor;
Plot[] Plots;
float plotX;
float plotY;
boolean rectOver = false;
 
void setup()
{
  size(200, 200);
  background(176);
  noStroke();
  framerate(30);
 
  autumnColor = color(200+random(55), 200+random(55), 100);
  destructionColor = color(55+random(33), 55+random(33), 55+random(33)) ;
  currentColor = autumnColor;
  MAX = 25;
  Plots = new Plot[MAX];
  int W = 11;
  int H = 2*W;
  int xSpace = W + spacing;
  int ySpace = H + spacing;
 
  /* for (int i=0; i<2*height/MAX; i++) {
    for(int j=0; j<2*height/MAX; j++) {
 int index = i*height/MAX + j;
 Plots[index] = new Plot(currentColor, i*xSpace, j*ySpace, W, H);
 int[colors] = new int[i*j]
    }
  }*/
  for(int rowNo = -width/W; rowNo <2*width/xSpace; rowNo++){
    for(int collumnNo = 0; collumnNo <2*height/ySpace; collumnNo++){
 int numPlots = rowNo * collumnNo + collumnNo;
 Plots[numPlots] = new Plot(currentColor,  collumnNo * xSpace,  rowNo * ySpace, W, H);
     // int [][] colors = new int[collumnNo][rowNo];
    }
  }
 
}
 
void loop()
{
  plotX = mouseX;
  plotY = mouseY;
  for(int i=0; i<MAX; i++) {
    Plots[i].update(plotX, plotY);
    Plots[i].draw();
  }
  if (rectOver) {
    currentColor = destructionColor;
  }
}
 
class Plot {
  float mx, my;
  int space = spacing;
  float xxx, yyy = 0;
  int W = 11;
  int H = 2*W;
  int xSpace = W + spacing;
  int ySpace = H + spacing;
 
  // Contructor (required)
  Plot(color c, float imx, float imy, float ixxx, float iyyy) {
    c  = currentColor;
    mx = imy;
    my = imx;
    xxx = int(ixxx);
    yyy = int(iyyy);
 
  }
 
  void update(float x, float y)
  {
    if ( overRect(plotX, plotY, xSpace, ySpace) ) {
 rectOver = true;
 //currentColor = destructionColor;
    }
  }
 
  boolean overRect(float x, float y, int width, int height)
  {
    if (mouseX >= x && mouseX <= x+width &&
    mouseY >= y && mouseY <= y+height) {
 return true;
    } else {
 return false;
    }
  }
  // Custom method for drawing the object
  void draw() {
    stroke(0);
    fill(currentColor);
    rect(mx+xxx-1, my+yyy-1, W, H);
  }
}
 
///////////////////////////////////////////////////
 
 
Any Help is MUCH appreciated!
 
Rich
 
 
 

richaman
JohnG

WWW
Re: Yikes!  Help needed wrestling a tornado...
« Reply #1 on: Dec 13th, 2004, 11:34am »

I've found 2 problems with the second lot of code, first is the reason for the array out of bounds exception,a nd the other is one that'll bite once that's fixed.
 
first:
 
You're defining the Plots[] arrat to be MAX in size, that means there's elements 0 to MAX-1. The first loop round of your initialisation fo Plots sets element #0, the second time, it tries to set elemtnt -17. Now -17 isn't a valid offset to the array you created, hence the exception.
 
second: Your calculation for "numPlots" will create wacky results, I think you don't want to multiply the current row/column offsets and add the row, but you do want to multiply the row offset by the total number of columns, then add the current column number (or vice versa, depending if you're building horizontally or vertically)
 
It would probably be easier to create a 2D array instead of a 1D one that you index funkily (like the pixels[] array)
 
So you'd have:
Plot Plots[][];
...
setup()
{
...
Plots=newPlot[xsize][ysize];
 
Which will save you having to constantly multiply numbers to get the right offset for a particular row etc, at the cost of a bit of speed.
 
richaman


Re: Yikes!  Help needed wrestling a tornado...
« Reply #2 on: Dec 13th, 2004, 4:37pm »

Thanks JohnG,
 
clearly I'm not understanding how arrays are created and accessed... Is there a resource that might clarify this for me?  I've thoroughly looked over the processing Reference and Learning sections, but I think my misunderstandings are more fundamental than those are geared for.  
 
Another person had suggested that a two dimensional array might be easier for me, but had also commented about the efficiency issue so I have stubbornly tried to pursue the 1D array.  If I'm going to follow up on the 2D angle, how do I access it?  I assume I divide the units into the x and y coordinates of a particular point and plug those back into the array....  all good in theory, but in practice?
 
Thanks again.
 
Rich
 

richaman
Pages: 1 

« Previous topic | Next topic »