Mohammed
YaBB Newbies
Offline
Posts: 12
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(); }}