Drawing a trail of 3D ellipses with a specific angle

Hello I am trying to draw a rectangle that is positioned inside a mouseY responsive ellipse. The code I have right now has a radian of PI/4.0f trail of ellipses drawn. However, I would like to have the angle of the ellipses trail upstraight, same as the rectangle so that it looks like they are wrapping the rectangle.

I tried to change the Z-axis but it doesn't work. Can anyone give me some suggestions please? Please also let me know if you need further elaboration.

Cheers, Karen

float rectX = 30;
float rectY = 100;

int num = 30;
float [] ypos = new float [num];
float radius = 200.0;

void setup() {
  size(400, 400, P3D);
  for (int i = 0; i <num; i ++) {
    ypos[i] = 0;
  }
  noFill();
  strokeWeight(1);
}

void draw() {
  background(0);
  stroke(255);
  translate(width/2, height/2);
  rotateY(-PI/4.0f);

  drawRect();
  drawResponsiveEllipse();
}

void drawRect() {

  pushMatrix();
  beginShape();

  fill(255);
  vertex(-rectX, rectY);
  vertex(rectX, rectY);
  vertex(rectX, -rectY);
  vertex(-rectX, -rectY);

  endShape(CLOSE);
  noFill();
  popMatrix();
}

void drawResponsiveEllipse() {

  translate (0,0);
  rotateX(PI/6.0f);
  pushMatrix();
  beginShape();

  ypos[num - 1] = mouseY;
  for ( int i = 0; i < num-1; i++ ) {
    ypos[i] = ypos[i+1];
  }
  noCursor();
  noStroke();
  for (int i = 0; i < num - 1; i+=2.5f ) {
    float b = map(i, 0, num, 0, 255);
    stroke(b); 
    ellipse (-rectX, ypos[i]-rectY, radius, radius);
    noFill();
  } 

  endShape(CLOSE);
  popMatrix();
}

Answers

  • It’s probably not cool to try ellipse in 3D

    Try using translate together with

          sphere (radius);
    

    Please edit your post and format the code better

  • Or try translate with ellipse but use 0,0 as ellipse position and put the real position into the translate command

    In setup () say ellipseMode(CENTER); iirc

  • float rectX = 30;
    float rectY = 100;
    
    int num = 30;
    float [] ypos = new float [num];
    float radius = 200.0;
    
    void setup() {
      size(400, 400, P3D);
      for (int i = 0; i <num; i ++) {
        ypos[i] = 0;
      }
      noFill();
      strokeWeight(1);
      noCursor();
    }
    
    void draw() {
      background(0);
    
      translate(width/2, height/2);
      rotateY(-PI/4.0f);
    
      drawRect();
      drawResponsiveEllipse();
    }
    
    // --------------------------------------------------
    // Minor functions 
    
    void drawRect() {
    
      pushMatrix();
      beginShape();
    
      fill(255);
      stroke(255);
      vertex(-rectX, rectY);
      vertex(rectX, rectY);
      vertex(rectX, -rectY);
      vertex(-rectX, -rectY);
    
      endShape(CLOSE);
      popMatrix();
    }//func 
    
    void drawResponsiveEllipse() {
      if (mouseY<=0)
        return; // quit function 
    
      ypos[num - 1] = mouseY;
      for ( int i = 0; i < num-1; i++ ) {
        ypos[i] = ypos[i+1];
      }
    
      noStroke();
      noFill();
    
      for (int i = 0; i < num - 1; i+=2.5f ) {
        // color 
        float b = map(i, 0, num, 255, 0);
        float r = map(i, 0, num, 0, 255);
        stroke(r, 0, b);
        // ellipse 
        pushMatrix();  
        translate(-rectX, ypos[i]-rectY, 0); 
        rotateX(PI/6.0f);
        ellipse (0, 0, radius, radius);
        popMatrix();
      } // for  
      //
    }//func 
    //
    
  • new version:

    float rectX = 30;
    float rectY = 100;
    float angleBox;
    
    int num = 30;
    float [] ypos = new float [num];
    float radius = 200.0;
    
    void setup() {
      size(400, 400, P3D);
      for (int i = 0; i <num; i ++) {
        ypos[i] = 0;
      }
      noFill();
      strokeWeight(1);
      noCursor();
    }
    
    void draw() {
      background(0);
      lights();
    
      translate(width/2, height/2);
      rotateY(-PI/4.0f);
    
      drawRect();
      drawResponsiveEllipse();
    }
    
    // --------------------------------------------------
    // Minor functions 
    
    void drawRect() {
    
      strokeWeight(1);
    
      pushMatrix();
      beginShape();
    
      fill(255);
      stroke(210);
    
      translate(-rectX, rectY-110, 0);
      rotateY(angleBox); 
      box(rectX, rectY*2, rectY/2); 
    
      endShape(CLOSE);
      popMatrix();
    
      angleBox+=0.04;
    }//func 
    
    void drawResponsiveEllipse() {
      if (mouseY<=0)
        return; // quit function 
    
      ypos[num - 1] = mouseY;
      for ( int i = 0; i < num-1; i++ ) {
        ypos[i] = ypos[i+1];
      }
    
      noStroke();
      noFill();
      strokeWeight(3);
    
      for (int i = 0; i < num - 1; i+=2.5f ) {
        // color 
        float b = map(i, 0, num, 255, 0);
        float r = map(i, 0, num, 0, 255);
        stroke(r, 0, b);
        // ellipse 
        pushMatrix();  
        translate(-rectX, ypos[i]-rectY, 0); 
        rotateX(PI/6.0f);
        ellipse (0, 0, radius, radius);
        popMatrix();
      } // for  
      //
    }//func 
    //
    
Sign In or Register to comment.