Loading...
Logo
Processing Forum

moving objects

in Programming Questions  •  2 years ago  
Hi All,

I am trying to move an object with translate but at the same time I want to show the previous position of such object (kind of tracking history). I usually do this by deleting the background but in this case if I do so then the 3D navigation o crazy for obvious reasons. Do you know another way I can still show the prev position of an object using a background at the same time?

Copy code
  1. import peasy.*;

  2. PeasyCam cam;
  3. int t =0;
  4. void setup() {
  5.   size(600,600,P3D);
  6.   cam = new PeasyCam(this, 0);
  7.   cam.setMinimumDistance(200);
  8.   cam.setMaximumDistance(1000);
  9.   frameRate(5);
  10. }
  11. void draw() {
  12.   lights();
  13. //  rotateX(-.5);
  14. //  rotateY(-.5);
  15.  background(0);
  16.   fill(255,0,0);
  17.  // box(30);
  18.    box(10);
  19.   pushMatrix();
  20.   t+=10;
  21.   translate(0,t,0);
  22.   fill(0,0,255);
  23.   box(10);
  24.  
  25.   popMatrix();
  26.   
  27.   
  28. }

Replies(11)

Re: moving objects

2 years ago
You could use an array or object-oriented programming (a simple xyz-class) to store the coordinates.

Also it's not a good idea to use a low frameRate to slow down your moving objects. You'll notice moving the camera doesn't work well. Instead have a normal frameRate and either decrease the speed or, since you want a jump each time, only add to the position once every nth frame.

Code Example
Copy code
  1. import peasy.*;
  2. PeasyCam cam;
  3. int t;

  4. void setup() {
  5.   size(600,600,P3D);
  6.   cam = new PeasyCam(this, 0);
  7.   cam.setMinimumDistance(200);
  8.   cam.setMaximumDistance(1000);
  9. }

  10. void draw() {
  11.   lights();
  12.   background(0);
  13.   fill(255,0,0);
  14.   box(10);

  15.   if (frameCount % 30 == 0) { // every 30th frame
  16.     t+=10;
  17.   }

  18.   pushMatrix();
  19.   translate(0,t,0);
  20.   fill(0,0,255);
  21.   box(10);
  22.   popMatrix();
  23. }

Re: moving objects

2 years ago
Hi,

thank you very much for your advice. It not clear to me what the xyz class should do though. In general I don't understand how to update the object position withouth losing the previous one due to background clearing..
I was looking for an exaple but I couldnt find any..

Re: moving objects

2 years ago
Ok, here is a commented example of an array, actually two arrays, that each hold 50 numbers in memory. One for the X and one for the Y positions. See the code for an explanation. Remember arrays start at zero.

Copy code
  1. // hold 50 numbers in memory
  2. int[] xpos = new int[50];
  3. int[] ypos = new int[50];

  4. void setup() {
  5.   size(800,800);
  6.   smooth();
  7. }

  8. void draw() {
  9.   background(255);

  10.   // move all the coordinates one place down
  11.   for (int i=0; i<xpos.length-1; i++) {
  12.     xpos[i] = xpos[i+1];
  13.     ypos[i] = ypos[i+1];
  14.   }

  15.   // set the last coordinates at mouseX,mouseY
  16.   xpos[xpos.length-1] = mouseX;
  17.   ypos[ypos.length-1] = mouseY;

  18.   // for all the coordinates show a circle => xpos[i]. ypos[i]
  19.   // the fill and size depend on the place in the array
  20.   // newer (aka higher i) are bigger and darker
  21.   // older (aka lower i) are smaller and lighter
  22.   for (int i=0; i<xpos.length; i++) {
  23.     noStroke();
  24.     fill(255-i*5);
  25.     ellipse(xpos[i],ypos[i],i*5,i*5);
  26.   }
  27. }

Re: moving objects

2 years ago
And here is a commented example of a class to store coordinates.
Copy code
  1. // arrayList to hold all the coordinates (or technically the coordinate objects)
  2. ArrayList coordinates = new ArrayList();

  3. void setup() {
  4.   size(800,800);
  5.   smooth();
  6. }

  7. void draw() {
  8.   background(255,0,100);

  9.   // display circles at all the coordinates
  10.   for (int i=0; i<coordinates.size(); i++) {
  11.     Coordinate c = (Coordinate) coordinates.get(i);
  12.     float radius = noise(i + frameCount * 0.05) * 60;
  13.     ellipse(c.x,c.y,radius,radius);
  14.   }

  15.   // removes coordinates if there are more than 150, comment this out to keep all coordinates
  16.   if (coordinates.size() > 150) { coordinates.remove(0); }
  17. }

  18. void mouseDragged() {
  19.   // while dragging, add coordinates at the mouse location
  20.   coordinates.add(new Coordinate(mouseX,mouseY));
  21. }

  22. // the extremely basic coordinate class
  23. class Coordinate {
  24.   int x,y;
  25.   Coordinate(int x, int y) {
  26.     this.x = x;
  27.     this.y = y;
  28.   }
  29. }

Re: moving objects

2 years ago
Hi,

I think the second example is what I want..thank you very much. I will study it.

Re: moving objects

2 years ago
All right. Both are valid ways to store coordinates. Here is your original code with the array implemented. All the previous positions have a small box. I'll leave the other one for you to figure out then.
Copy code
  1. import peasy.*;
  2. PeasyCam cam;
  3. int[] position = new int[50];

  4. void setup() {
  5.   size(600,600,P3D);
  6.   cam = new PeasyCam(this, 0);
  7.   cam.setMinimumDistance(200);
  8.   cam.setMaximumDistance(1000);
  9. }

  10. void draw() {
  11.   lights();
  12.   background(0);
  13.   fill(255,0,0);
  14.   box(10);

  15.   if (frameCount % 30 == 0) {
  16.     for (int i=0; i<position.length-1; i++) {
  17.       position[i] = position[i+1];
  18.     }
  19.     position[position.length-1] = position[position.length-2] + 10;
  20.   }

  21.   for (int i=0; i<position.length-1; i++) {
  22.     pushMatrix();
  23.     translate(0,position[i],0);
  24.     fill(0,0,255);
  25.     box(5);
  26.     popMatrix();
  27.   }
  28.  
  29.     pushMatrix();
  30.     translate(0,position[position.length-1],0);
  31.     fill(0,0,255);
  32.     box(10);
  33.     popMatrix();
  34. }

Re: moving objects

2 years ago
Many thanks again.Here it is. 
I knew shiffman example for shifting position within an array but I thought it wasn't helpful at all..but you actually showed the opposite. It seems I need to understand it better but I find much harder compared to the use of arraylist and class.

Have you idea how can I understand it better?
Copy code
  1. // arrayList to hold all the coordinates (or technically the coordinate objects)
  2. import peasy.*;
  3. PeasyCam cam;


  4. ArrayList coordinates = new ArrayList();
  5. int t = 0;
  6. void setup() {
  7.   size(400,400,P3D);
  8.     cam = new PeasyCam(this, 50);
  9.   cam.setMinimumDistance(200);
  10.   cam.setMaximumDistance(1000);
  11.   
  12.  // smooth();
  13.   

  14. }

  15. void draw() {
  16.   background(255);
  17.   fill(255,0,0);
  18.  // translate(width/2,height/2,0);
  19.   if (frameCount 0 ==0) {
  20.     t+=10;
  21.     coordinates.add(new Coordinate(0,t,0));
  22.   }


  23.   for (int i=0; i<coordinates.size(); i++) {
  24.     Coordinate c = (Coordinate) coordinates.get(i);


  25.     pushMatrix();
  26.     translate(c.x,c.y,c.z);
  27.     box(10);
  28.     popMatrix();
  29.   }
  30. }


  31. class Coordinate {
  32.   int x,y,z;
  33.   Coordinate(int x, int y, int z) {
  34.     this.x = x;
  35.     this.y = y;
  36.     this.z = z;
  37.   }
  38. }

Re: moving objects

2 years ago
Great. Objects are a fantastic way to store information. Not only xyz, but basically everything you need. So there is a lot of potential in using them. Sticking to those, especially in an example like yours, is a good option if you feel more comfortable with a class and an arraylist. With regard to an array. It's basically a list of that type of data instead of just one. The list can hold 1, 100 or 1000 things on it. That doesn't really matter. Some examples:

int oneInt versus int[] multipleInts = new int[50]
String oneWord versus String[] multipleWords = new String[50]
color oneColor versus color[] multipleColors = new color[50]
PImage oneImage versus PImage[] multipleImages = new PImage[50]

To get the number, image, word at that point in the list you use the index. For a list of 50. The data is in position 0 to 49.

Just like a very neat and structured grocery list.

String[] groceries = new String[3]
groceries[0] = bread;
groceries[1] = butter;
groceries[2] = cheeeeeese;

Re: moving objects

2 years ago
Ok I tried to implement the method to store a box coordinates in kind of 3D conway game of life but I didnt succeed. The idea is to have a 2D game of life which at each framrate draw the next state on a new Z level instead of the same plane.

I managed to make it but I am using the no background method and fixed camera; I would instead want to use peasycame to navigate the model.

I post here the code so my goal  it is clear for everyone 
Copy code
  1. import processing.opengl.*;

  2. //import peasy.*;
  3. // Id like to use PeasyCam indeed
  4. //PeasyCam cam;  


  5. int xNum = 20;
  6. int yNum =20;

  7. int cellSize = 30;

  8. int t =-400;
  9. int xSize = xNum*cellSize;
  10. int ySize = yNum*cellSize;

  11. boolean[][] state = new boolean[xNum][yNum];
  12. boolean[][] nextState = new boolean[xNum][yNum];

  13. void setup() {
  14.   size(800,800,OPENGL);
  15.   noStroke();
  16. frameRate(5);
  17. background(255);

  18.   
  19. //  cam = new PeasyCam(this, 0);
  20. //  cam.setMinimumDistance(200);
  21. //  cam.setMaximumDistance(1000);
  22.   
  23.   for(int i=0; i<xNum; ++i)
  24.   {
  25.     for (int j=0; j<yNum; ++j)
  26.     {
  27.       int a = int(random(0,2));
  28.       if (a==0){
  29.       state[i][j] = false;
  30.       } else {
  31.        state[i][j] = true; }
  32.     }
  33.   }


  34. }

  35. void draw()
  36. {
  37. // background(255); I am not using the background in the draw in order to keep previous states of my CAs
  38. ambientLight(95,95,95);
  39. directionalLight(51, 102, 126, 0, 0, -1);
  40. directionalLight(60, 60, 60, 1, 60, 0);
  41. directionalLight(20, 20, 20, 0, -5, 0);
  42. lightSpecular(0, 50, 0);
  43. lightFalloff(1,0,0);
  44.  camera(1200,1200,1200,0,0,0,0,0,-1);


  45.   for(int i=0; i<xNum; ++i)
  46.   {
  47.     for (int j=0; j<yNum; ++j)
  48.     {
  49.       if(state[i][j] ==true)
  50.       {
  51.        
  52. //        strokeWeight(0.5);
  53. //        stroke(15);
  54.         fill(160);
  55.    
  56.         pushMatrix();
  57.         translate(i*cellSize,j*cellSize,t);
  58.      
  59.         box(cellSize);
  60.        
  61.       //  rect(i*cellSize,j*cellSize, cellSize,cellSize);
  62.    
  63.        popMatrix();
  64.        
  65.      
  66.       }
  67.       int liveCells =0;
  68.       
  69.       for(int m=-1; m<=1; ++m)
  70.       {
  71.         for(int k=-1; k<=1; ++k)
  72.         {
  73.           if(! (m==0 && k==0))
  74.           {
  75.             if(state[(i+m+xNum)%xNum][(j+k+yNum)%yNum] == true)
  76.             {
  77.               liveCells++;
  78.             }
  79.           }
  80.         }
  81.       }

  82.       if(liveCells<2 || liveCells>3)
  83.       {
  84.         nextState[i][j] = false;
  85.       }
  86.       else if(liveCells==3)
  87.       {
  88.         nextState[i][j] = true;
  89.       }
  90.       else
  91.       {
  92.         nextState[i][j] = state [i][j];
  93.       }
  94.     }
  95.     

  96.   }
  97.   for(int i=0; i<xNum; ++i)
  98.   {
  99.     for (int j=0; j<yNum; ++j)
  100.     {
  101.       state[i][j] = nextState [i][j];
  102.       
  103.   
  104.     }
  105.   }
  106.   

  107. t+=30;
  108. if (frameCountP==0){
  109.  
  110.   t =-400;
  111.   setup();
  112. }


  113. }
while here is the same code with an attempt to use the class to store coordinates. At the moment I get an array out of bounds error; but can't figure out how to fix it and make to look like the sketch above.

Copy code


  1. import peasy.*;
  2. PeasyCam cam;

  3. //Create Arraylist to store coordinates
  4. ArrayList coordinates = new ArrayList();



  5. int xNum =20;
  6. int yNum =20;

  7. int cellSize = 10;
  8. int t =0;

  9. int xSize = xNum*cellSize;
  10. int ySize = yNum*cellSize;

  11. boolean[][] state = new boolean[xNum][yNum];
  12. boolean[][] nextState = new boolean[xNum][yNum];

  13. void setup() {
  14.   size(500,500,P3D);


  15.   cam = new PeasyCam(this, 0);
  16.   cam.setMinimumDistance(200);
  17.   cam.setMaximumDistance(1000);

  18.   // Start with a random position of cells
  19.   for(int i=0; i<xNum; ++i)
  20.   {
  21.     for (int j=0; j<yNum; ++j)
  22.     {
  23.       
  24.       int a = int(random(0,2));
  25.       if (a==0) {
  26.         state[i][j] = false;
  27.       } 
  28.       else {
  29.         state[i][j] = true;
  30.       }
  31.     }
  32.   }
  33. }

  34. void draw()
  35. {

  36.   background(255);

  37.   for(int i=0; i<xNum; ++i)
  38.   {
  39.     for (int j=0; j<yNum; ++j)
  40.     {

  41.       if(state[i][j] ==true)
  42.       {
  43. //Draw a cube if the cell value is true

  44.         stroke(255);
  45.         fill(0);
  46.         
  47.         // Iterate through all the coordinates and draw them
  48.         for (int k=0; k<coordinates.size(); k++) {
  49.           Coordinate c = (Coordinate) coordinates.get(i);
  50.           pushMatrix();

  51.           translate(c.x,c.y,c.z);
  52.           box(cellSize);

  53.           //  rect(i*cellSize,j*cellSize, cellSize,cellSize);
  54.           // t+=10;

  55.           popMatrix();
  56.         }
  57.       }


  58. // check neighbour for alive cells according to GOL rules
  59.       int liveCells =0;

  60.       for(int m=-1; m<=1; ++m)
  61.       {
  62.         for(int k=-1; k<=1; ++k)
  63.         {
  64.           if(! (m==0 && k==0))
  65.           {
  66.             if(state[(i+m+xNum)%xNum][(j+k+yNum)%yNum] == true)
  67.             {
  68.               liveCells++;
  69.             }
  70.           }
  71.         }
  72.       }

  73.       if(liveCells<2 || liveCells>3)
  74.       {
  75.         nextState[i][j] = false;
  76.       }
  77.       else if(liveCells==3)
  78.       {
  79.         nextState[i][j] = true;
  80.       }
  81.       else
  82.       {
  83.         nextState[i][j] = state [i][j];
  84.       }
  85.     }
  86.   }
  87.   
  88.   //Update state of cell every 30 frames
  89.   //store new coordinates and increase z

  90.   if (frameCount % 30 ==0) {
  91.     for(int i=0; i<xNum; ++i)
  92.     {
  93.       for (int j=0; j<yNum; ++j)
  94.       {
  95.         state[i][j] = nextState [i][j];
  96.         if(state[i][j] ==true) {
  97.           t+=10;
  98.           coordinates.add(new Coordinate(i*cellSize,j*cellSize,t));
  99.         }
  100.       }
  101.     }
  102.   }
  103. }


  104. //class for storing xyz values
  105. class Coordinate {
  106.   int x,y,z;
  107.   Coordinate (int x, int y, int z) {
  108.     this.x = x;
  109.     this.y = y;
  110.     this.z = z;
  111.   }
  112. }

Re: moving objects

2 years ago
Well, it's hard because there are a few things running through each other. On the one hand there is a two-dimensional array of booleans. On the other hand there are the 3D coordinates. It's possible to go fully with coordinates. But since you already have the boolean array in place -and this is a pretty efficient way to store this kind of info- you can also just adapt the class to store the boolean array (instead of coordinates). In the sketch below you can move a round with peasycam, press c to clear everything and press space to add a new state.

Code Example:
Copy code
  1. ArrayList states = new ArrayList();

  2. import processing.opengl.*;
  3. import peasy.*;
  4. PeasyCam cam;

  5. int xNum = 20;
  6. int yNum = 20;
  7. int cellSize = 30;
  8. int z;

  9. void setup() {
  10.   size(800,800,OPENGL);
  11.   cam = new PeasyCam(this, 300,300,0, 1000);
  12.   cam.setMinimumDistance(1);
  13.   cam.setMaximumDistance(100000);
  14.   noStroke();
  15.   fill(160);
  16.   initialState();
  17. }

  18. void draw() {
  19.   background(255);
  20.   ambientLight(95,95,95);
  21.   directionalLight(51, 102, 126, 0, 0, -1);
  22.   directionalLight(60, 60, 60, 1, 60, 0);
  23.   directionalLight(20, 20, 20, 0, -5, 0);
  24.   lightSpecular(0, 50, 0);
  25.   lightFalloff(1,0,0);
  26.   displayStates();
  27.   if (frameCount % 30 == 0) { nextState(); } // autoplay
  28. }

  29. void keyPressed() {
  30.   if (key == 'c') { states.clear(); z=0; initialState(); } // clear
  31.   if (key == ' ') { nextState(); } // add a new state manually
  32. }

  33. void initialState() {
  34.   boolean[][] state = new boolean[xNum][yNum];
  35.   for(int i=0; i<xNum; i++) {
  36.     for (int j=0; j<yNum; j++) {
  37.       int a = int(random(2));
  38.       if (a==0) { state[i][j] = false; }
  39.       else      { state[i][j] = true;  }
  40.     }
  41.   }
  42.   states.add(new SavedState(state,z));
  43.   z += cellSize;
  44. }

  45. void displayStates() {
  46.   for (int i=0; i<states.size(); i++) {
  47.     SavedState s = (SavedState) states.get(i);
  48.     s.display();
  49.   }
  50. }

  51. void nextState() {
  52.   SavedState s = (SavedState) states.get(states.size()-1);
  53.   boolean[][] nextState = new boolean[xNum][yNum];
  54.   for(int i=0; i<xNum; ++i) {
  55.     for (int j=0; j<yNum; ++j) {
  56.       int liveCells =0;
  57.       for(int m=-1; m<=1; ++m) {
  58.         for(int k=-1; k<=1; ++k) {
  59.           if(! (m==0 && k==0)) {
  60.             if(s.savedState[(i+m+xNum)%xNum][(j+k+yNum)%yNum] == true) {
  61.               liveCells++;
  62.             }
  63.           }
  64.         }
  65.       }
  66.       if(liveCells<2 || liveCells>3) {
  67.         nextState[i][j] = false;
  68.       } else if(liveCells==3) {
  69.         nextState[i][j] = true;
  70.       } else {
  71.         nextState[i][j] = s.savedState[i][j];
  72.       }
  73.     }
  74.   }
  75.   states.add(new SavedState(nextState,z));
  76.   z += cellSize;
  77. }

  78. class SavedState {
  79.   boolean[][] savedState;
  80.   float z;
  81.   SavedState(boolean[][] savedState, float z) {
  82.     this.savedState = savedState;
  83.     this.z = z;
  84.   }
  85.  
  86.   void display() {
  87.     for(int i=0; i<xNum; ++i) {
  88.       for (int j=0; j<yNum; ++j) {
  89.         if(savedState[i][j]) {
  90.           pushMatrix();
  91.           translate(i*cellSize,j*cellSize,-z);
  92.           box(cellSize);
  93.           popMatrix();
  94.         }
  95.       }
  96.     }
  97.   }
  98. }

Re: moving objects

2 years ago
I found your answers not only enlightening but also very educational. Endless thanks!