We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am wondering if it is possible to select a shape that is moving and create another shape that is binded to it and orbits it. I am attempting to make a simple flat solar system and having trouble getting moons to follow and track a sphere. Here is the code I am using at the moment. I would very much like to see how to add another small polygon to orbit the last made polygon. Or if there are any tutorials or videos dealing with parenting or something along those lines would also be helpful!
Thanks!
void setup() {
background(0);
size(1600, 800);
}
void draw() {
if(mousePressed)
background(0);
pushMatrix();
translate(width*0.5, height*0.5);
rotate(frameCount / 50.0);
polygon(0, 0, 80, 20); // Icosahedron
popMatrix();
pushMatrix();
translate(width*0.5, height*0.5);
rotate(frameCount / 80.0);
polygon(100, 100, 10, 10); // Icosahedron
popMatrix();
pushMatrix();
translate(width*.5, height*.5);
rotate(frameCount / 30.0);
polygon(70, 70, 5, 5); // Icosahedron
popMatrix();
}
void polygon(float x, float y, float radius, int npoints) {
float angle = TWO_PI / npoints;
beginShape();
for (float a = 0; a < TWO_PI; a += angle) {
float sx = x + cos(a) * radius;
float sy = y + sin(a) * radius;
vertex(sx, sy);
}
endShape(CLOSE);
}
Answers
Format you code. Edit your post, select code and hit ctrl+o.
Kf
And please use a descriptive title for your query.
There probably are but they would probably use classes and recursion which you aren't ready for yet.
Generally each parent is responsible for drawing its children. This sketch shows a 5 object system.
The code is deliberately verbose to help you see what is happening but also so you might add Venus yourself :)
BTW I have modified the topic title for you. I leave formatting the code to you :)
Possible - instead of using rotate you could use a for loop with cos and sin and calc the positions of the planet
Store those
And use them when calc the pos of the moon
Maybe make a class skyObject or so to hold the data in an ArrayList
Thank you so much quark! I am going to be working to modify this a bit and see what I can do my next challenge is to attempt to map an image for each object rather than a color.