0
\$\begingroup\$

So this is the problem: Picture of the problem

I have an object pulled towards the center of a circle and I am unable to figure out how to move the object left and right depending on its rotation around the circle.

I basically need to know in which "angle" to push the object if the left or right key was pressed.

Thank you

\$\endgroup\$
  • \$\begingroup\$ Hmm, ok so a lot of questions here. Do you have friction? Is the moveable object round? Does it roll like a wheel? If it is rotating, how is the mass distributed (like a bowling ball or like a bike wheel?). Does the larger object move as well? You can make physically realistic model for this but need to define it much better. \$\endgroup\$ – Greg Petersen Jan 9 '18 at 16:18
1
\$\begingroup\$

This should be solveable by simple trigonometry.

Have a look at the following unit circle:

Basic trigonometry

So basically, depending on detected key presses, you'd increase or decrease θ. Then, when with the center of your circle, just calculate any position by doing something like the following:

posX = centerX + cos(θ)*radius;
posY = centerY + sin(θ)*radius;

You also mentioned gravity. It can be simulated by slowly decreasing the radius of the circle.

Hope that helps.

Cheers

\$\endgroup\$
  • 1
    \$\begingroup\$ By the way, this is just a very simple model without any friction, center of mass or a moving gravitational center. \$\endgroup\$ – Ignatiamus Jan 9 '18 at 17:20
1
\$\begingroup\$

The solution depends on your implementation. If the object that is pulled towards the centre is not rotated, but only translated towards the circle, then the best solution would be to find the tan of that object, based on the circle. Ignatiamus's answer can probably help with that.

If however, your object is rotated based on the circle (if for example it represents a character that is walking on a planet), there is a more lazy way to do it. Simply Translate the character based on its local coordinates. If the character's face if facing the +X axis for example, if you do a translation towards the X axis with a positive value, the object will move towards it, based on it's local rotation. Once you do that, you should probably re-calculate the objects rotation, so that its placed properly towards the circle.

Some pseudocode:

void Start() { // Runs once in the beginning

    object.LookAt(circle); // Makes the object face the circle

}

void Update() { // Runs once a frame

    object.translate(1.0f, 0); // Move 1 unit towards the +X axis in 2D
    object.LookAt(circle); // Makes the object look at the circle again

}

This code will cause the object to constantly rotate around the circle and translate properly. Please note that based on your implementation you may need to change some parameters, like which axis your object you want it to point to etc.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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