We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › store previous coordinates of objects in array
Page Index Toggle Pages: 1
store previous coordinates of objects in array (Read 432 times)
store previous coordinates of objects in array
Aug 1st, 2009, 3:02am
 
Hello,
i would like to draw the five last position of each rectangles under them. It  looks like a sandwich of five stratum. More precisely when a rectangle move at t+6 i would like to draw the five previous position
with different alpha. I know that i have to store it in an array but how can i update the cycle ? how can i refresh my array with new position ? After being drawn, the oldest position of the five previous position disappear from the rendering and from the array
For example :
t+4
t+3
t+2
t+1
t+0 // when the t+5 will be drawn t+0 will dispappear and so on.

i try to implement this code :

Obj[] o = new Obj[20];
int Height = 50;
int Width =  50;

void setup() {
//size(screen.width,screen.height, P3D);
 size(500, 500, P3D);
 colorMode(HSB);      
 noStroke();
 smooth();            
 frameRate(30);
 for(int i=0; i < o.length; i++) { // initialize x, y for each object
   float objectSize = 2;
   float x = random(Width);
   float y = random(Height);
   o[i] = new Obj(x, y, objectSize);
 }}

void draw() {
  background(0);
 translate(width/2.1, height/2, 350);
 //translate(width/2.1, height/2, 750);
 rotateX(1);
 for(int i=0; i < o.length; i++) {
   o[i].draw();
 }}

class Obj {

 color colors;
 float x, y, objectSize;

 Obj(float x, float y, float s) {
   this.x = x;
   this.y = y;
   objectSize = s;
   colors = color(random(255), 255, 255); // hue, saturation, brightness
 }
 void draw() {
   move();
   render();
 }
 void move() {
     // move randomly
     x += random(-objectSize/2,objectSize/2);
     y += random(-objectSize/2,objectSize/2);
     // limits
     if (x > Width + objectSize) x = -objectSize;
     if (x < -objectSize) x = Width + objectSize;
     if (y > Height + objectSize) y = -objectSize;
     if (y < -objectSize) y = Height + objectSize;
 }
void render() {
   pushMatrix();
   fill(colors);
   translate(x,y);
   rect(objectSize/2, 0, objectSize, objectSize);
   popMatrix();
 }}
Re: store previous coordinates of objects in array
Reply #1 - Aug 1st, 2009, 3:43am
 
I did something similar in Re: Can someone explain my own code to me. I used an ArrayDeque but it can be done with a simple ArrayList too.
You can do that with a simple array, but then you have either to shift the array positions each time (not a problem as the array is quite small) or do a complex management of the indexes.
Re: store previous coordinates of objects in array
Reply #2 - Aug 1st, 2009, 5:28am
 
Isn't what you're trying to do similar to the storing input example?

http://processing.org/learning/basics/storinginput.html
Page Index Toggle Pages: 1