LibGDX Rotate sprite around another one

I am trying to rotate one sprite around another one. It is a space rocket and it’s exhaust flame. The rocket can rotate 360 degrees and I would like the flame to be always at the bottom of the rocket. What I’m doing right now is setting flame’s origin to the center of the rocket sprite but I also have to scale the flame down and when I do so it dissapears, only rocket is visible. Could someone explain what am I doing wrong? Is it even good approach? I’ve just read about sprite origin and I’m not sure I understand it comepletly.
Here is my code:

Sprite sprite1 = new Sprite(new Texture("1.png"));
sprite1.setScale(1/PPM);
sprite1.setRotation(angle);
Sprite sprite2 = new Sprite(new TextureRegion(...));
sprite2.setOrigin(sprite.getWidth()+sprite1.getWidth()/2,sprite.getHeight()+sprite1.getHeight()/2)
sprite2.setScale(1/PPM);
sprite1.setPosition(x,y);
sprite2.setPosition(x,y-sprite2.height());

It might be helpful to draw it out…

It sounds like you want the image to line up with the bottom middle of the rocket, the trouble you will find has to do with lining up the origins when the x axis is rotated at 30 degrees, that bottom middle you’ll need to work out the trig relationship between the two sprites…

it almost might be easier to have them as a single sprite with engine on vs engine off…

I don’t know whether libGDX provides you with a way to specify arbitrary affine coordinate system transformations for your sprites.
If so, then your are looking for the following transformations:


// We start with the world coordinate system as reference
// and we would like to position our rocket at its position
// in the world and rotate it around its own origin:
translate(rocketPosition)
rotate(rocketRotation)
// Now we can draw the rocket
// ...draw it...
// Next, being in the local frame of the rocket, we want
// to position the exhaust flame at the bottom of the rocket.
// Given that the size of the rocket in its own local coordinate
// system is 'rocketHeight', and the origin is at the center
// of the rocket, we do the following now:
translate(0, -rocketHeight/2)
// ...draw exhaust flame...

Generally, you want a kind of “parent/child” relationship for transformations, where the transformation of the exhaust flame is relative to the rocket. This can be done using a scene graph. I don’t know whether libGDX provides such as scene graph for 2D.

Basically, you could use the formula for calculating every point on a circle: r² = (y-yd)² + (x-xd)²
r is the radius, x and y the standard coordinates, xd and yd the displacement of the circles center.
You could fill in the formula like this for a circle with radius 16 pixels and a displacement of x:200 and y:80 :
256 = (y-80)² + (x-200)²
Now you just have to fill in one of the two variables and rearrange the equation to output the coordinate you need.

I don’t know if that can solve your particular problem, but it answers the question :smiley: