Public Group

# rotating objects in a double orbit

This topic is 2135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a object at the center. Other set of objects rotating around center in first orbit. Now i want other objects to rotate around the objects in 1st orbit.

n the above fig, set of triangles are rotating around the square and circles are rotating around the triangle. I have the code which works for 1st orbit, but i am not able to render that second orbit.

void display()
{
for (int k = 0; k < 10; k++)
{

distance_x = 0.4 * cos(angle1*3.14 / 180);
distance_y = 0.4 * sin(angle1*3.14 / 180);
angle1 += 60;
glPushMatrix();
glRotatef(i, 0.0f, 0.0f, 1.0f);
glTranslatef(distance_x, distance_y, 0.0f);
glRotatef(-i, 0.0f, 0.0f, 1.0f);

DrawTriangle();
glPopMatrix();
}
}



I am calling display in loop. i is static global variable. which transformations will do the second orbit?

##### Share on other sites

Well, you should know the concepts of relative coordinate system and world coordinate system. Your triangles should save your relative point list and the center point. Then update the relative point list when rotate for some radius and redraw it. Give some simple code:

#define PI 3.1415926535

typedef struct
{
double x, y;
} POINT, *LPPOINT;
typedef class Triangle
{
private:
POINT pointList[3];
POINT centerPoint;
public:
Triangle() { };  // Constructor
~Triangle() { };  // Destructor
BOOL Draw();    // Realize your Draw method
BOOL Rotate(double theta);    // Rotate method
} TRIANGLE, *LPTRIANGLE;

BOOL TRIANGLE::Rotate(double theta)
{
double cosVal, sinVal;
for (int i = 0; i < 3; i++)
{
cosVal = cos(theta * PI / 180);
sinVal = sin(theta * PI / 180);
// Compute the relative coordinate after rotate
this->pointList[i].x = this->pointList[i].x * cosVal - this->pointList[i].y * sinVal;
this->pointList[i].y = this->pointList[i].x * sinVal + this->pointList[i].y * cosVal;
}
return this->Draw();

BOOL TRIANGLE::Draw()
{
// In your draw method, you should change your relative coordinate to world coordinate.
}


• 21
• 11
• 9
• 17
• 13