We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › rotating star
Page Index Toggle Pages: 1
rotating star (Read 1061 times)
rotating star
Dec 8th, 2009, 7:52am
 
Hello!

I've been crunching my head now on this for the past few days and I cannot seem to get it to work. It's ever so simple: I'd like a star rotating on it's own axes.

easy no?

I've been checking the forum and the language and the learning bit, came up many things that worked, but when I try to implement it in my own code... it keeps rotating on some unknown axes.

Don't have a clue what I do wrong... still a newbee.

If someone could have a look at it and explain to me what I do wrong, I'd be much obliged.

Here's the Code:

float xpos;
float ypos;
int sz;
float angle;

void setup() {
 frameRate(12);
 size(400,400);
 smooth();
 xpos = width/2;
 ypos = height/2;
 sz = width/8;
}

void draw() {
 background(0xFF006600);
 
 angle += PI/240;

 pushMatrix();
 rotate(-angle);
 Star();
 popMatrix();
}

void Star() {

 fill(0xFFFF0000);
 noStroke();

 beginShape();
 vertex(xpos,ypos-sz/2);
 vertex(xpos+sz/6,ypos-sz/6);
 vertex(xpos+sz/2,ypos-sz/11);
 vertex(xpos +sz/4,ypos+sz/8);
 vertex(xpos+sz/3,ypos+sz/2);
 vertex(xpos,ypos+sz/3);
 vertex(xpos-sz/3,ypos+sz/2);
 vertex(xpos-sz/4,ypos+sz/6);
 vertex(xpos-sz/2,ypos-sz/9);
 vertex(xpos-sz/6,ypos-sz/6);
 endShape(CLOSE);

}


Thanks a lot for whoever can help me!

kYra
Re: rotating star
Reply #1 - Dec 8th, 2009, 8:01am
 
The problem is that you always rotate arround the origin (0,0), so if you want to rotate an object arround its origin you have to move or draw it there, rotate it and then move it back to where you want to it be.
So in your example I entered 0 for the pos. You could also just remove all your pos inside your star drawing. And then rotated and translated it...


Code:
float xpos; 
float ypos;
int sz;
float angle;

void setup() {
 frameRate(12);
 size(400,400);
 smooth();
 xpos =0;
 ypos = 0;
 sz = width/8;
}

void draw() {
 background(0xFF006600);
 
 angle += PI/24;

 pushMatrix();
 translate(width/2,height/2);
 rotate(-angle);
 Star();
 popMatrix();
}

void Star() {

 fill(0xFFFF0000);
 noStroke();

 beginShape();
 vertex(xpos,ypos-sz/2);
 vertex(xpos+sz/6,ypos-sz/6);
 vertex(xpos+sz/2,ypos-sz/11);
 vertex(xpos +sz/4,ypos+sz/8);
 vertex(xpos+sz/3,ypos+sz/2);
 vertex(xpos,ypos+sz/3);
 vertex(xpos-sz/3,ypos+sz/2);
 vertex(xpos-sz/4,ypos+sz/6);
 vertex(xpos-sz/2,ypos-sz/9);
 vertex(xpos-sz/6,ypos-sz/6);
 endShape(CLOSE);

}


Re: rotating star
Reply #2 - Dec 8th, 2009, 8:03am
 
It is because you rotate your star, then translate it at (xpos, ypos) before drawing it. You should do that backwards : translate, then rotate it, then draw it.

That can't be done if you use xpos and ypos in your Star() function. You should have something like :

Code:
pushMatrix();
 translate(xpos, ypos);
 rotate(angle);
 drawStar();
popMatrix();

void drawStar() {
 // draw something around the origin (0, 0)
 beginShape();
 vertex(0, -sz/2);
 vertex(sz/6, -sz/6);
 ...
 endShape();
}



edit: sorry for double-explanation, Cedric was quicker!
Re: rotating star
Reply #3 - Dec 8th, 2009, 8:11am
 
OOOhhhhh !!!

Thank you both so much, I knew I just was overlooking something.

kYra
Re: rotating star
Reply #4 - Dec 8th, 2009, 8:14am
 
if you are planning to draw more stars you can put that rotating and translating in your function and simply draw your stars by writing Star(x,y);

Code:
float xpos; 
float ypos;
int sz;
float angle;

void setup() {
frameRate(12);
size(400,400);
smooth();
xpos =0;
ypos = 0;
sz = width/8;
}

void draw() {
background(0xFF006600);

angle += PI/24;



Star(20,150);
Star(120,70);
Star(230,150);
Star(150,20);
Star(width/2,height/2);
}

void Star(float x, float y) {


pushMatrix();
fill(0xFFFF0000);
noStroke();

translate(x,y);
rotate(-angle);
beginShape();
vertex(-12.0, -12.0);
bezierVertex(-12.0, -12.0, -12.0, -12.0, -12.0, -12.0);
bezierVertex(-12.0, -12.0, 0.0, -31.0, 0.0, -31.0);
bezierVertex(0.0, -31.0, 6.0, -14.0, 6.0, -14.0);
bezierVertex(6.0, -14.0, 22.0, -7.0, 23.0, -7.0);
bezierVertex(24.0, -7.0, 5.0, 8.0, 5.0, 8.0);
bezierVertex(5.0, 8.0, 0.0, 30.0, 0.0, 30.0);
bezierVertex(0.0, 30.0, -11.0, 7.0, -11.0, 7.0);
bezierVertex(-11.0, 7.0, -31.0, -8.0, -31.0, -8.0);
bezierVertex(-31.0, -8.0, -13.0, -11.0, -13.0, -11.0);
endShape();

popMatrix();

}





i know my star sucks Smiley
Re: rotating star
Reply #5 - Dec 8th, 2009, 8:24am
 
Why not OOP stars?  Cheesy


Code:
Star[] stars;

void setup() {
 frameRate(12);
 size(400,400);
 smooth();
 stars = new Star[]{
   new Star( 20, 150, 0),
   new Star(120,  70, 0),
   new Star(230, 150, 0),
   new Star(150,  20, 0),
   new Star(width/2, height/2, 0)
 };
}

void draw() {
 background(0xFF006600);
 for (int i = 0; i < stars.length; i++) {
   stars[i].model();
   stars[i].render();
 }
}

class Star {
 
 float x, y, angle;
 
 Star(float x, float y, float angle) {
   this.x = x; this.y = y; this.angle = angle;
 }
 
 void model() {
   this.angle += PI/24;
 }
 
 void render() {
   rectMode(CENTER);
   fill(#ffff00);
   noStroke();
   pushMatrix();
     translate(x,y);
     rotate(angle);
     rect(0, 0, 20, 20);
     rotate(PI/4);
     rect(0, 0, 20, 20);
   popMatrix();
 }
 
}

Re: rotating star
Reply #6 - Dec 8th, 2009, 8:28am
 
I like all your stars, haha...

but it needs to be just the one

...it will however come under attack later on by some ellipses Wink
Page Index Toggle Pages: 1