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?
2 Answers
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 radians = Math.toRadians(currentOrbitDegrees);
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);
setOrigin
andsetRotation
methods? \$\endgroup\$Something
arround a point is nothing but movingSomething
to that point, rotating it and move it foreward by the same amount. So if you haveSomething
at P(1,0) and a point P(0,0) you have to moveSomething
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) \$\endgroup\$