# Pushing an object left and right (gravitation towards center of a circle)

So this is 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

• 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. – Greg Petersen Jan 9 '18 at 16:18

## 2 Answers

This should be solveable by simple trigonometry.

Have a look at the following unit circle: 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

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

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.