# In LibGDX, how might I make an object orbit around a position?

I need an object, which would be a circle, made with ShaderRender, to orbit around a specified point. In LibGDX is there a method or something that would allow me to accomplish this? If not, could you please provide a code example?

• Have you looked at Sprites and their setOrigin and setRotation methods? – StrongJoshua May 11 '15 at 23:30
• Rotating Something arround a point is nothing but moving Something to that point, rotating it and move it foreward by the same amount. So if you have Something at P(1,0) and a point P(0,0) you have to move Something to the P(0,0). Then rotate it how much you want (f.e. 90°) and move it foreward, relative to it's direction. The result would be P(0,1) – Springrbua May 12 '15 at 7:15
• @StrongJoshua Sprites methods allow me to rotate an object but not to rotate it from the same distance from a point – user65825 May 12 '15 at 10:22
• Try setting the Sprite's origin to where you want to rotate it around, and do the rotation. I haven't done this myself so I can't guarantee it will work, but try it. – StrongJoshua May 12 '15 at 21:32
• Use atan2(y, x); -and yes, thats (y,x) not (x,y) – Eric Feb 26 '17 at 20:55

If you wish for an object to orbit around a specified point in two dimensional space, you want to use some trigonometric functions. Here is an example method to calculate an objects position in an orbit:

class OrbitUtilities {
/* currentOrbitDegrees: the degrees around the orbit that the satellite is(can greater that 360(361 would be equivalent to 1))
* distanceFromCenterPoint: the distance in world units from the center point that the satellite is
* centerPoint: the vector of the center point of the orbit system
*/
public Vector2 calculateOrbit(float currentOrbitDegrees, float distanceFromCenterPoint, Vector2 centerPoint) {

float x = (Math.cos(radians) * distanceFromCenterPoint) + centerPoint.x;
float y = (Math.sin(radians) * distanceFromCenterPoint) + centerPoint.y;

return new Vector2(x, y);
}
}


you need to keep the angle of the object (alpha) , declare float alpha =0, then in render method do something like this

x = Math.cos(alpha*MathUtils.DegreesToRadians)* distance;
y = Math.sin(alpha* MathUtils.DegreesToRadians )* distance;
alpha++;
sprite.setPosition(x,y);