We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I was wondering how to rotate something around a certain radius. Ultimately to make a solar system with the sun in the middle and the planets rotating around it.
Answers
rotation around itself - NOT what you want
and another sketch with one blue planet and a red sun
**explanation: **
What is box and rect?
First, you need to know that
box()
other thanrect()
is drawn always on the origin (0,0,0 in 3D space), meaning that half of its width is in the negative area, same for its height and for its depth. So the number in the brackets afterbox
(its parameters) don't signify the position (as forrect()
) but the size of it (one number / parameter means same size for width, height and depth, 3 numbers would be different width, height and depth).What is 3D space all about?
Both sketches above are in 3D space (Just PM me if you need 2D space). The idea of 3D space is that you draw not on a canvas but inside a box / room where you can place stuff further away (away from you, inside the screen). The idea is that the first parameter is x (you move something left right on the table surface e.g.), the 2nd y is the height (you move something up and down above the table) and the 3rd z is that you move something back and forth on the table surface.
Please note that with translate we nearly never use Z in the examples above (Z is just 0). We only use Z with the big gray box in the center in translate.
Rotation
Now the important bit is that each rotation takes place around the origin too. That means that in 2D space we need to place the
rect()
on the origin to make it rotate around itself (its own center) as inrect(-20,-20,40,40);
the translation to the right position must take place with the commandtranslate()
then before rotate.The examples use rotateY, which is a rotation around the y-axis, what makes the rotated box just stay on the table surface with y staying the same and x and z being changed by the rotation. There is also rotateX and rotateZ of course - try it out!
Planet
As you see above the only box that rotates like a planet is the blue one. Here you see a 2nd (3rd)
translate
afterrotate
:translate(123, 0);
which makes it turn around another point than itself like a planet does.Best, Chrisir ;-)
If I might add an important point, and to clarify, the order you call translate and rotate functions matter. For example:
is not the same as
For the latter, all objects rotate around their own Y axis.
Kf
Thanks!!!
Also, 2 translate()s in a row (without rotateY() inbetween) just add up
Thanks for the in depth answers. What I had in mind was couple of ellipses going around one ellipse in 2D. Would I have to use PVector?
PVector is not necessary (but ok)
Just use that code above
Replace rotateY with simple rotate
Use translate with 2 parameters (leaving out the last one which is Z)
Show your approach Chrisir
Here is an example:
Kf
Full Code solution?!?
WHY?
my thoughts exactly.
when you get two questions that are very similar within a day of each other then you've got to think 'homework assignment'.
@koogs Maybe I answer both questions at the same time. What its called two birds with a single stone. This is the big challenge on internet at university/school level, to avoid plagiarism. I will encourage students not to ask these questions then, or to have a private forum btw tutor and students.
I will say the question was answer before my posting. I just presented a solution using vectors instead of doing translations and rotations of the whole scene.
I would like to model the Earth and the moon in 2D. Going through some videos it shows how to make a 'class' was wondering if there was a way not do it and model by incorporating mass and the gravitational force?
and I do not copy your work, I go through them trying to understand and try to come up with my own code :)