How to make a 3D ring (or donut, visually)?

Hello,

I am learning the 3d mode and was wondering how I could make a 3d ring to go around my "earth".

Basically, I would like a thin ring to go around my sphere (ala Saturn's rings or such). Here's what I have:

void setup()
{
  size(800, 600, OPENGL);
  smooth();
  textSize(20);
}

void draw()
{
  background(255);
  lights();
  // noStroke();

  translate(width/2, height/2);

  fill(255, 0, 0);
  pushMatrix();
  strokeWeight(.5);

//  rotateX(radians(frameCount));
  rotateY(-radians(frameCount));
//  rotateZ(radians(15));
  sphereDetail(30);
  sphere(100);
//  text("Hi", 0, height/4);
  popMatrix(); 

}

Thanks for any ideas or tips! Also, any tips on my current code are welcome.

Answers

  • Answer ✓

    How's this?

    void setup() {
      size(800, 600, P3D);
      sphereDetail(30);
      smooth();
    }
    
    void draw(){
      background(255);
      translate(width/2, height/2);
      lights();
      fill(255, 0, 0);
      stroke(0);
      strokeWeight(1);
      rotateY(-radians(frameCount));
      sphere(100);
      stroke(128,0,128);
      strokeWeight(10);
      noFill();
      rotateX(HALF_PI-.45);
      ellipse(0,0,240,240);
    }
    
  • edited November 2014

    ...well that'll do it!

    I don't know why I didn't think a simple ellipse would work, thanks a lot (and next time I'll be sure to try such a 'simple' thing out before asking here :\"> ). But thanks a lot!

    Edit: Why did you change to P3D instead of OPENGL? Is it better for shapes?

    Also, when you ("you" being anyone) run the below, is the ring "clipping" on the edges every so often?

    int i; 
    
    void setup()
    {
      size(800, 600, OPENGL);
      smooth();
      textSize(20);
    }
    
    void draw()
    {
      background(255);
      lights();
      // noStroke();
    
      translate(width/2, height/2);
    
      fill(255, 0, 0);
    //  pushMatrix();
      strokeWeight(.5);
    
    //  rotateX(radians(frameCount));
      rotateY(-radians(frameCount));
    //  rotateZ(radians(15));
      sphereDetail(30);
      sphere(100);
      fill(20,255,150);
      text("Hi", 0, height/4);
    //  popMatrix();
    
    
      pushMatrix();
      fill(0,0,255);
      strokeWeight(.2);
    //  rotateX(radians(frameCount*2));
      rotateY(radians(frameCount*2));
      translate(width/4,-70);
    
      sphereDetail(30);
      sphere(25);
      popMatrix();
    
      pushMatrix();
      noFill();
      strokeWeight(25);
      rotateX(HALF_PI-.45);
      translate(0,0);
      ellipse(0,0,350,350);
      popMatrix();
    }
    
    
    void multSpheres(){
      pushMatrix();
      fill(255,255,50);
      strokeWeight(.2);
      rotateY(radians(frameCount*1));
      rotateZ(radians(frameCount*2));
      translate(175,0);
      for (i = 0; i < 15; i++){
        sphere(50-(i*4));
        translate((i*3),5);
      }
    
    
      popMatrix();
    }
    
  • edited November 2014

    In Processing 2+, P3D & OPENGL is the same renderer: println(P3D == OPENGL); ;)
    In Processing 1, that same statement above would result false! :-B
    Another interesting test is: println(isGL());

  • Thanks GoToLoop - so is it just a personal preference, that could even come down to "p3d" takes less keystrokes?

    Also, as I play around with the ellipse, it's animation gets sort of laggy, or clips parts of the ellipse on each rotation...is that just me, or is it happening to anyone else? The above reply has the clipping, and this one produces a "glitchy" animation:

    int i; 
    
    void setup()
    {
      size(800, 600, OPENGL);
      smooth();
      textSize(20);
    }
    
    void draw(){
      background(255);
      lights();
      // noStroke();
    
      translate(width/2, height/2);
    
      fill(255, 0, 0);
    //  pushMatrix();
      strokeWeight(.5);
    
    //  rotateX(radians(frameCount));
      rotateY(-radians(frameCount));
    //  rotateZ(radians(15));
      sphereDetail(30);
      sphere(100); // set the EARTH
      fill(20,255,150);
      text("Hi", 0, height/4);
    //  popMatrix();
    
    
      pushMatrix();
      fill(0,0,255);
      strokeWeight(.2);
      rotateY(radians(frameCount*2)); 
      translate(width/4,-70);
    
      sphereDetail(30);
      sphere(25); // set the Moon
      popMatrix();
    
      rings();
    
    }
    
    void rings(){
      pushMatrix();
      noFill();
    //  stroke(100,200,200);
      strokeWeight(10);
      rotateX(radians(frameRate*2));
      translate(0,0);
      ellipse(0,0,250,550);
      popMatrix();
    }
    
    
    void multSpheres(){
      pushMatrix();
      fill(255,255,50);
      strokeWeight(.2);
      rotateY(radians(frameCount*1));
      rotateZ(radians(frameCount*2));
      translate(175,0);
      for (i = 0; i < 15; i++){
        sphere(50-(i*4));
        translate((i*3),5);
      }
      popMatrix();
    }
    
  • The 'glitchy' animation is due to using frameRate (which can vary wildly between frames)

    Try the code below. I wouldn't recommend using frameRate or frameCount to control animation because the elapsed time between frames can vary greatly as the sketch runs.

    You might find this webpage useful when creating frame-rate independent animations.

    int i;
    
    void setup()
    {
      size(800, 600, OPENGL);
      smooth();
      textSize(20);
    }
    
    void draw() {
      background(255);
      lights();
      // noStroke();
    
      translate(width/2, height/2);
    
      fill(255, 0, 0);
      pushMatrix();
      strokeWeight(.5);
    
      //  rotateX(radians(frameCount));
      rotateY(-radians(frameCount));
      //  rotateZ(radians(15));
      sphereDetail(30);
      sphere(100); // set the EARTH
      fill(20, 255, 150);
      text("Hi", 0, height/4);
      popMatrix();
    
    
      pushMatrix();
      fill(0, 0, 255);
      strokeWeight(.2);
      rotateY(radians(frameCount*2));
      translate(width/4, -70);
    
      sphereDetail(30);
      sphere(25); // set the Moon
      popMatrix();
    
      rings();
    }
    
    void rings() {
      pushMatrix();
      noFill();
      //  stroke(100,200,200);
      strokeWeight(10);
      rotateZ(radians(45));
      rotateY(radians(-frameCount*0.5));
      //rotateZ(radians(frameCount*2));
      translate(0, 0);
      ellipse(0, 0, 250, 550);
      popMatrix();
    }
    
    
    void multSpheres() {
      pushMatrix();
      fill(255, 255, 50);
      strokeWeight(.2);
      rotateY(radians(frameCount*1));
      rotateZ(radians(frameCount*2));
      translate(175, 0);
      for (i = 0; i < 15; i++) {
        sphere(50-(i*4));
        translate((i*3), 5);
      }
      popMatrix();
    }
    
  • Thanks for that, I'll check it out!

Sign In or Register to comment.