[SOLVED] Having a hard time Translating in 3D

Hi,

I am very new at Processing, this is my second week.

I am altering some code from one of the examples to create a bunch of Pshapes. It’s looking good, but I can’t figure out how to translate() in P3D.

In the main part of the code I am using fullScreen() so I am guessing that’s my size(). In the tab where I am controlling my polygon I want to translate, or at least have the shapes move in the Z axis. I can’t seem to do that. In the tab called “polygons” I know I need to include size( x, y, P3D) to do this then translate() with x y and z axes, but if I add P3D in size() my code won’t run. If anyone has any ideas on how to do this please let me know.

Thank you.

Here’s the code.

main part:

import oscP5.*; 

OscP5 oscP5;

float ramp1=450;
float ramp2=100;
float ramp3=1;


ArrayList<Polygon> polygons;

void setup() {

  fullScreen(P3D);
 // size(2700, 1500, P3D); 

  //background(0);
  oscP5 = new OscP5(this, 12000);

  PShape boxes = createShape();   //boxes is the name of my shape  
  boxes.beginShape();
  boxes.strokeWeight(1);
   boxes.stroke(0);
  
  boxes.noFill();


  //dimentions of base are 30X by 20 deep
  // sides are 50 Y by 30 X

  boxes.vertex(50, 50, 0);    // this is the square top
  boxes.vertex(20, 50, 0); 
  boxes.vertex(20, 50, -20); 
  boxes.vertex(50, 50, -20); 
  boxes.vertex(50, 50, 0); 

  boxes.vertex(70, 100, 0);   //front rectangle
  boxes.vertex(40, 100, 0); 
  boxes.vertex(20, 50, 0); 

  boxes.vertex(20, 50, -20); //back rectangle
  boxes.vertex(40, 100, -20); 
  boxes.vertex(70, 100, -20); 
  boxes.vertex(50, 50, -20); 

  boxes.vertex(50, 50, -20);  //close up bottom or middle square
  boxes.vertex(70, 100, -20); 
  boxes.vertex(70, 100, 0); 
  boxes.vertex(70, 100, 0);   
  boxes.vertex(40, 100, 0);
  boxes.vertex(40, 100, -20); 

  boxes.vertex(40, 100, -20);    //bottom back rectangle
  boxes.vertex(20, 150, -20); 
  boxes.vertex(50, 150, -20); 
  boxes.vertex(70, 100, -20); 

  boxes.vertex(70, 100, 0);   //front bottom rectangle
  boxes.vertex(50, 150, 0);
  boxes.vertex(20, 150, 0); 
  boxes.vertex(40, 100, 0);   

  boxes.vertex(20, 150, 0);   //bottom square
  boxes.vertex(20, 150, -20); 
  boxes.vertex(50, 150, -20); 
  boxes.vertex(50, 150, 0); 

  boxes.endShape();


  // Make an ArrayList
  polygons = new ArrayList<Polygon>();
    for (int i = 0; i < 200; i++) {
    polygons.add(new Polygon(boxes));
    }
  
}

void oscEvent(OscMessage theOscMessage) {    

  float value = theOscMessage.get(0).floatValue();

  if (theOscMessage.checkAddrPattern("/ramp1")) {
    if (value > 0.1) {
      ramp1 = value;
    } else {
      ramp1 = 0.0;
    }
  }
  float value2 = theOscMessage.get(0).floatValue();

  if (theOscMessage.checkAddrPattern("/ramp2")) {
    if (value2 > 0.1) {
      ramp2 = value2;
    } else {
      ramp2 = 0.0;
    }
  }
  
   float value3 = theOscMessage.get(0).floatValue();

  if (theOscMessage.checkAddrPattern("/ramp3")) {
    if (value3 > 0.1) {
      ramp3 = value3;
    } else {
      ramp3 = 0.0;
    }
  }
  
  
}

void draw() {

  background(255);

 // translate(width/2, height/2, 0);

  
    stroke(0);               //  for the sphere
  strokeWeight(ramp3);
  translate(width/2, height/2, 0);
  //rotateX(mouseY * 0.05);
  rotateY(radians(ramp2));
  noFill();
  sphereDetail(30);
  sphere(ramp1);
  
  
  //rotateX(PI/2);
  //rotateY(PI);
 // rotateY(radians(ramp2));
 //  rotateZ(radians(ramp2));
  // rotateX(radians(ramp2));

  
  // Display and move them all
  for (Polygon poly : polygons) {
    poly.display();
    poly.move();
    
    println(ramp1);
  }

  
  
  
}

here’s the tab called Polygons:

// A class to describe a Polygon (with a PShape)

class Polygon {
  // The PShape object
  PShape s;
  // The location where we will draw the shape
  float x, y, z;
  // Variable for simple motion

   float speed;
 

  Polygon(PShape s_) {
    x = random(-1000, 1000);
    y = random(1000, -1000); 
    z = random(-1000, -0); 

    s = s_;
    speed = random(-1, 1);
  }

  // Simple motion
  void move() {
    y += speed;
    x += speed*1.2;
    z += speed*4;


    if (y > height) {
      y = -100;
    }


    if (y < -1000) {
      y = height;
    }

    if (x > width) {
      x = 0;
    }


    if (x > width) {
      x = -200;
    }

    if (z > width) {
      z = -500;
    }


    if (z < 1000) {
      z = 0;
    }
  }

  // Draw the object
 void display() {
    pushMatrix();
    size(P3D);
    translate(x, y, z);
    shape(s);
   popMatrix();
  }
}

thank you.

1 Like

OK. I solved my own problem. I made a little error in the if statement that controlled the Z dimension. I’m going to post the code.

main code:

import oscP5.*; 

OscP5 oscP5;

float ramp1=450;
float ramp2=100;
float ramp3=1;


ArrayList<Polygon> polygons;

void setup() {

  fullScreen(P3D);
//  size(2700, 1500, P3D); 

  //background(0);
  oscP5 = new OscP5(this, 12000);

  PShape boxes = createShape();   //boxes is the name of my shape  
  boxes.beginShape();
  boxes.strokeWeight(1);
   boxes.stroke(0);
  
  boxes.noFill();


  //dimentions of base are 30X by 20 deep
  // sides are 50 Y by 30 X

  boxes.vertex(50, 50, 0);    // this is the square top
  boxes.vertex(20, 50, 0); 
  boxes.vertex(20, 50, -20); 
  boxes.vertex(50, 50, -20); 
  boxes.vertex(50, 50, 0); 

  boxes.vertex(70, 100, 0);   //front rectangle
  boxes.vertex(40, 100, 0); 
  boxes.vertex(20, 50, 0); 

  boxes.vertex(20, 50, -20); //back rectangle
  boxes.vertex(40, 100, -20); 
  boxes.vertex(70, 100, -20); 
  boxes.vertex(50, 50, -20); 

  boxes.vertex(50, 50, -20);  //close up bottom or middle square
  boxes.vertex(70, 100, -20); 
  boxes.vertex(70, 100, 0); 
  boxes.vertex(70, 100, 0);   
  boxes.vertex(40, 100, 0);
  boxes.vertex(40, 100, -20); 

  boxes.vertex(40, 100, -20);    //bottom back rectangle
  boxes.vertex(20, 150, -20); 
  boxes.vertex(50, 150, -20); 
  boxes.vertex(70, 100, -20); 

  boxes.vertex(70, 100, 0);   //front bottom rectangle
  boxes.vertex(50, 150, 0);
  boxes.vertex(20, 150, 0); 
  boxes.vertex(40, 100, 0);   

  boxes.vertex(20, 150, 0);   //bottom square
  boxes.vertex(20, 150, -20); 
  boxes.vertex(50, 150, -20); 
  boxes.vertex(50, 150, 0); 

  boxes.endShape();


  // Make an ArrayList
  polygons = new ArrayList<Polygon>();
    for (int i = 0; i < 200; i++) {
    polygons.add(new Polygon(boxes));
    }
  
}

void oscEvent(OscMessage theOscMessage) {    

  float value = theOscMessage.get(0).floatValue();

  if (theOscMessage.checkAddrPattern("/ramp1")) {
    if (value > 0.1) {
      ramp1 = value;
    } else {
      ramp1 = 0.0;
    }
  }
  float value2 = theOscMessage.get(0).floatValue();

  if (theOscMessage.checkAddrPattern("/ramp2")) {
    if (value2 > 0.1) {
      ramp2 = value2;
    } else {
      ramp2 = 0.0;
    }
  }
  
   float value3 = theOscMessage.get(0).floatValue();

  if (theOscMessage.checkAddrPattern("/ramp3")) {
    if (value3 > 0.1) {
      ramp3 = value3;
    } else {
      ramp3 = 0.0;
    }
  }
  
  
}

void draw() {

  background(255);

 // translate(width/2, height/2, 0);

  
    stroke(0);               //  for the sphere
  strokeWeight(ramp3);
  translate(width/2, height/2, 0);
  //rotateX(mouseY * 0.05);
  rotateY(radians(ramp2));
  noFill();
  sphereDetail(30);
  sphere(ramp1);
  
  
  //rotateX(PI/2);
  //rotateY(PI);
 // rotateY(radians(ramp2));
 //  rotateZ(radians(ramp2));
  // rotateX(radians(ramp2));

  
  // Display and move them all
  for (Polygon poly : polygons) {
    poly.display();
    poly.move();
    
    println(ramp1);
  }

  
  
  
}

Polygons tab

// A class to describe a Polygon (with a PShape)

class Polygon {
  // The PShape object
  PShape s;
  // The location where we will draw the shape
  float x, y, z;
  // Variable for simple motion

   float speed;
 

  Polygon(PShape s_) {
    x = random(-1000, 1000);
   y = random(1000, -1000); 
   z = random(-1000, 1000); 

    s = s_;
    speed = random(-1, 1);
  }

  // Simple motion
  void move() {
    y += speed;
    x += speed*1.2;
    z += speed*4;


    if (y > height) {
      y = -100;
    }


    if (y < -1000) {
      y = height;
    }

    if (x > width) {
      x = 0;
    }


    if (x > width) {
      x = -200;
    }

    if (z > 0) {
      z = -500;
    }


    if (z < -1000) {
      z = 0;
    }
  }

  // Draw the object
 void display() {
    pushMatrix();
    //size(P3D);
    fullScreen(P3D);
    translate(x, y, z);
    shape(s);
   popMatrix();
  }
}
2 Likes