5
\$\begingroup\$

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?

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Have you looked at Sprites and their setOrigin and setRotation methods? \$\endgroup\$ – StrongJoshua May 11 '15 at 23:30
  • \$\begingroup\$ 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) \$\endgroup\$ – Springrbua May 12 '15 at 7:15
  • \$\begingroup\$ @StrongJoshua Sprites methods allow me to rotate an object but not to rotate it from the same distance from a point \$\endgroup\$ – user65825 May 12 '15 at 10:22
  • \$\begingroup\$ 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. \$\endgroup\$ – StrongJoshua May 12 '15 at 21:32
  • \$\begingroup\$ Use atan2(y, x); -and yes, thats (y,x) not (x,y) \$\endgroup\$ – Eric Feb 26 '17 at 20:55
4
\$\begingroup\$

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);
    }
}
\$\endgroup\$
0
\$\begingroup\$

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);
\$\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.