tvoloshyn
tvoloshyn

Reputation: 407

Orbiting object around orbiting object

enter image description here

How do I get to orbit green circle around orange and blue around green ?

I found many solutions which works fine with rotating around static point(int this case orange circle) but didn't find any good maths equation which would work for both static and moving points.

angle += sunRot;

if(angle > 360.0f)
{
    angle = 0.0f;
}

float radian = glm::radians(angle);

float radius = glm::distance(position, rotCenter);

float x = rotCenter.x + (radius * cosf(radian));
float z = rotCenter.z + (radius * sinf(radian));

glm::vec3 newPos = glm::vec3(x, 0, z);

setPosition(newPos);

Here is what I'm trying to achieve (Thanks to @George Profenza for sharing link)

Upvotes: 0

Views: 4307

Answers (2)

Stefan Haustein
Stefan Haustein

Reputation: 18793

Base all your calculations on the radius and angle of the current object where possible and store the radius and angle with the object.

In particular, do not calculate the radius based on the x/y coordinates in every iteration: If the base object has moved between steps, your calculated radius will be slightly off and the error will accumulate.

Upvotes: 1

George Profenza
George Profenza

Reputation: 51837

You should be able to nest coordinate spaces using opengl using glPushMatrix(), glPopMatrix() calls. Here's a basic example(press mouse to see coordinate spaces). The syntax isn't c++, but it's easy to see what I mean.

You can do this multiple ways:

  1. polar coordinate formula
  2. manually multiplying transformation matrices
  3. simply using push/pop matrix calls (along with translate/rotate where needed), which does the matrix multiplication for you behind the scenes.

Just in case you want to try the polar coordinate formula:

x = cos(angle) * radius
y = sin(angle) * radius

Where angle is the current rotation of a circle and the radius is it's distance from the centre of rotation.

Upvotes: 0

Related Questions