1

I'm working on a sketch where I'll need to draw a lot. of spheres, but I think I'm missing something about how their coordinates work. In this example I'm trying to draw a line of spheres, equally distant apart on the x-axis. I can draw the spheres starting at 0, between I try to shift them tone centered around the origin, I get strange results.

Main Program

    import peasy.*;

ArrayList<Sphere> spheres = new ArrayList<Sphere>();

int r = int(random(0, 256));
int g = int(random(0, 256));
int b = int(random(0, 256));

PeasyCam cam;

void setup ()
{
  size(430, 760, P3D);
  frameRate(60);
  orientation(LANDSCAPE);
    
  for(int i = 0; i < 6; i++){
    spheres.add(new Sphere(color(r, g, b), 25, ((i*5) + 75), 0, 0));
  }
  
  
  for(int i = 0; i < 6; i++){
    spheres.get(i).shiftx();
  }

  
  cam = new PeasyCam(this, 0, 0, 0, 1500);

}

void reset(){

  
}

void draw ()
{ 
  background(0);
  ambientLight(255, 255, 255);
  directionalLight(0, 255, 255, 1, -1, 0);
  
  for(int i = 0; i < spheres.size(); i++){
    spheres.get(i).update();
  }
  
  
}

Sphere class

class Sphere {

  int size;
  color col;
  float x;
  float y;
  float z;
  
  Sphere(color  col, int size, float x, float y, float z){
    this.size = size;
    this.x = x;
    this.y = y;
    this.z = z;
    this.col = col;
    
    println(this.x,this.y, this.z);
    
  }
  
  public void shiftx(){
    this.x -= 85;
    println(this.x,this.y, this.z);
  }
  
  public void update() {
    //colorMode(RGB);
    fill(col);
    sphereDetail(180);
    //stroke(255);
    noStroke();
    translate(this.x,this.y,this.z);
    sphere(this.size);
  }   
}

enter image description here

This is the result when I draw the spheres starting at the origin and increase their x value accordingly. enter image description here

But this is what happens when I shift every sphere to the left by the same amount. If the are equidistant, and then shifted left (negative on the x axis)by the same amount, why are they suddenly drawn on top of each other?

Konrad
  • 21,590
  • 4
  • 28
  • 64
Ben Delany
  • 75
  • 10

1 Answers1

2

translate is a global operation, you have to use pushMatrix and popMatrix to not mess up every other spheres

public void update() {
  pushMatrix();
  colorMode(RGB);
  fill(col);
  sphereDetail(180);
  stroke(255);
  noStroke();
  translate(this.x,this.y,this.z);
  sphere(this.size);
  popMatrix();
}
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • That's whatI was missing, thanks! So the Matrix operations keep the translations local? – Ben Delany Mar 26 '23 at 16:25
  • https://processing.org/reference/pushMatrix_.html `pushMatrix` is like - "hey processing, save current transformations for me" and `popMatrix` is like - "hey processing, load the transformations that I asked you to save before please" – Konrad Mar 26 '23 at 16:27
  • @BenDelany Check out the [2D Transformations tutorial](https://processing.org/tutorials/transform2d): the same applies in 3D. Konrad: nice consice answer (+1) – George Profenza Mar 26 '23 at 21:24