-1
\$\begingroup\$

I have following piece of code, now i want to move the "moon" object in the orbit around "earth". please help!!

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
int main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(WHITE);

settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
outtextxy(10, 10, "Welcome to Graphics Programming");
outtextxy(10, 30, "My VU-ID is BC123456789");

//rectangle(x1,y1,x2,y2);
rectangle(10,60,400,400);

int Earth_x,Earth_y;
Earth_x=10+390/2;        // This expression calculates the center of the rectangle
Earth_y=60+340/2;        // as we want to draw our Earth in the center.
outtextxy(Earth_x, Earth_y, "Earth");
circle(Earth_x,Earth_y,30);

//Moon orbit
circle(Earth_x,Earth_y,100);

int Moon_x,Moon_y;
Moon_x=Earth_x+100;         //Initial coordinates of Moon
Moon_y=Earth_y;
while(1){                   // continuous loop as condition will always be true
setcolor(WHITE);
outtextxy(Moon_x, Moon_y, "Moon");
circle(Moon_x,Moon_y,10);
// redraw Moon orbit
circle(Earth_x,Earth_y,100);
delay(100);
setcolor(BLACK);
outtextxy(Moon_x, Moon_y, "Moon");
circle(Moon_x,Moon_y,10);
delay(100);
\$\endgroup\$

closed as off-topic by Kromster, MichaelHouse Nov 24 '14 at 15:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Kromster, MichaelHouse
If this question can be reworded to fit the rules in the help center, please edit the question.

3
\$\begingroup\$

Use polar coordinate system. Cast Moon's position to polar coordinates by formulas:

r = sqrt( sqr(Moon_x - Earth_x) + sqr(Moon_y - Earth_y) );
phi = atan2( Moon_y - Earth_y, Moon_x - Earth_x );

Then, you can change angle (phi) to move Moon around the center:

phi += angular_speed;

And after that cast changed polar coordinates back to cartesian coordinates:

Moon_x = r * cos( phi )
Moon_y = r * sin( phi )
\$\endgroup\$
  • \$\begingroup\$ Good answer. do note that this will only work if the object in question is round \$\endgroup\$ – Timothy Groote Nov 24 '14 at 11:02
  • \$\begingroup\$ What do you mean in "object is round"? You mean orientation (sprite rotation) of object will not be changed? Yes, it's true, but in case Moon orbiting around Earth, orientation should be calculated separately even if Moon is round. \$\endgroup\$ – Kostya Regent Nov 24 '14 at 11:39
  • \$\begingroup\$ What i mean is that OP's wording of the question was wider. (how to move an object around the circumference of another object). Your answer works porfectly for planets, because x=r*cos(phi); y=r*sin(phi) makes you move in (2d) circles, and can even be scaled up to 3d. but imagine trying to move along the circumference of a plank, or a house... \$\endgroup\$ – Timothy Groote Nov 25 '14 at 10:02
  • 2
    \$\begingroup\$ Ah, now I understand what you mean. Question has inconsistent topic. Topic should be "How to move object along circumference of another object's orbit". Because Moon moves along circumference of Earth means that Moon would have 0 height (roll on the surface of Earth). \$\endgroup\$ – Kostya Regent Nov 25 '14 at 11:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.