face objects outwards in a uniform angle

edited October 2015 in Questions about Code

hello there, i'm sure there is an easy answer to this...

i'm rotating ellipses around a circle, but each ellipse seems to face the same direction...

but i want them all to face uniformly around the sphere...

i've tried a bunch of stuff... nothing seems to work without throwing things into chaos.

i imagine its something like... divide the amount of objects by their angle around the sphere, rotating each by that much... but i just can't get it...

i've commented out the rotation line where I think i should, but uncomment and you'll see what i mean... the ellipse kind've rotate in a non-uniform fashion...

what i'm trying to do is have every ellipse angle at the right angle to the centre of the sphere... if that makes sense... like rays of sunshine or something... or if i had a piece of string going out to each ellipse, the ellipse rotates on the angle of that piece of string...

hope someone can help...

int radius = 150;
float d = 0.0;
void setup() {
  size(1000, 1000, P3D);
  noStroke();
}

void draw() { 
  background(0);
  translate(width/2, height/2);
  lights();
  sphereDetail(50);
  stroke(100, 100, 255);
  rotate(radians(d*5));
  sphere(100);
 d = d + radians(1);
  for (int deg = 0; deg < 360; deg += 10) {
    float angle = radians(deg); 
    float x = (cos(angle) * radius);
    float z = (sin(angle) * radius);
    pushMatrix();
    translate(x, z, 0);
    fill(50, 200, 255);
    pushMatrix();
    //rotateY(90);
    ellipse(0, 0, 20, 20);
    popMatrix();
    popMatrix();
  }
}

Answers

  • edited October 2015

    I can get what I want the normal old way, just rotating within a for loop...

    But the particular sketch I'm working on needs the sin/cos/translate code as used above...

    int radius = 150;
    float d = 0.0;
    float theta = 0;
    void setup() {
      size(400, 400, P3D);
      noStroke();
    }
    
    void draw() { 
      background(0);
      translate(width/2, height/2);
      lights();
    
      rotateY(radians(mouseX));
      rotateX(radians(mouseY));
    
      for (int i = 0; i < 360; i+=45) {
    
        rotate(radians(i));
    
        stroke(255);
        pushMatrix();
        rotateY(radians(90));
        ellipse(0, -100, 100, 100);
        popMatrix();
      }
    }
    
  • Answer ✓

    You're close. Really close. The second example sketch really helped. Have a solution.

    int radius = 150;
    float d = 0.0;
    void setup() {
      size(1000, 1000, P3D);
      noStroke();
    }
    
    void draw() { 
      background(0);
      translate(width/2, height/2);
      lights();
      sphereDetail(50);
      stroke(100, 100, 255);
      rotate(radians(d*5));
      sphere(100);
     d = d + radians(1);
      for (int deg = 0; deg < 360; deg += 10) {
        float angle = radians(deg); 
        float x = (cos(angle) * radius);
        float z = (sin(angle) * radius);
        pushMatrix();
        translate(x, z, 0);
        fill(50, 200, 255);
        pushMatrix();
        rotateY(HALF_PI);
        rotateX(HALF_PI-angle);
        ellipse(0, 0, 20, 20);
        popMatrix();
        popMatrix();
      }
    }
    

    This was a good forum post. You posted formatted sample code and everything. Please stay and be an example for others!

  • hmmmm..... i appear to have fixed it... for now... mind you i have no idea what i'm doing... trial and error for 3 hours is the only way I know how to fix things :(

    int radius = 150;
    float d = 0.0;
    float theta = 0;
    void setup() {
      size(400, 400, P3D);
      noStroke();
          fill(50, 200, 255);
    }
    
    void draw() { 
      background(0);
      translate(width/2, height/2);
      lights();
      sphereDetail(50);
      sphere(20);
    
    rotateY(radians(mouseX));
    rotateX(radians(mouseY));
    
      for (int deg = 0; deg < 360; deg += 45) {
    
        pushMatrix();
    
        float angle = radians(deg); 
    
        float x = (cos(angle) * radius);
        float z = (sin(angle) * radius);
    
        translate(x, 0, z);
        rotateY(radians(-deg));
        pushMatrix();
        ellipse(0, 0, 100, 100);
        popMatrix();
        popMatrix();
      }
    }
    
  • the -deg sorted it out... with out the minus... i noticed it kind've had the right angles but all back to front... add a minus and voila...

  • ah i just saw your post @TfGuy44... will check it out!

Sign In or Register to comment.