简体   繁体   中英

Orbiting object around orbiting object

在此处输入图片说明

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)

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.

You should be able to nest coordinate spaces using opengl using glPushMatrix() , glPopMatrix() calls. Here 'sa 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM