Reputation: 1091
In my game I have a rocket sprite and at the bottom of the rocket there is an animated flame sprite , I want the flame sprite to align with the bottom of the rocket when the rocket rotates.
This is the rocket when the game starts, everything is OK:
But when i rotate the rocket the flame does not align with the rocket's bottom like this:
How can i make it align?
here is my code:
// draw rocket
rocketSprite.setRegion(AssetLoader.rocketTexture);
rocketSprite.setPosition(rocket.getX(), rocket.getY());
rocketSprite.setOrigin(rocket.getWidth() / 2, rocket.getHeight() / 2);
rocketSprite.setSize(rocket.getWidth(), rocket.getHeight());
rocketprite.setRotation(rocket.getRotation());
rocketSprite.setScale(1, 1);
rocketPodSprite.draw(game.batch);
//flame width and height
int w = 70;
int h = 20;
// randomly change scale of flame for effects
float sw = MathUtils.random(2f / 7f, 1);
float sh = MathUtils.random(0.5f, 1);
// draw flame
flameSprite.setRegion(AssetLoader.flameTexture);
flameSprite.setPosition(rocket.getX() + rocket.getWidth() / 2 - w, rocket.getY() - 15);
flameSprite.setOrigin(w, h / 2);
flameSprite.setSize(w, h);
flameSprite.setRotation(rocket.getRotation());
flameSprite.setScale(sw, sh);
flameSprite.draw(game.batch);
Upvotes: 0
Views: 365
Reputation: 13571
You need to change origin
- point arround which your sprite will be rotated.
Your problem is that both rocket and flame are rotated arround their own centres however you would rather like to rotate them both arround rocket's one. Then you need to set the flame sprite origin with position of rocket center.
flameSprite.setOrigin(w, h / 2 + rocket.getHeight() / 2 + SPACE_BETWEEN_ROCKET_AND_FLAME);
As I see in your case the SPACE_BETWEEN_ROCKET_AND_FLAME
has value of 15 - flame.height
due to this position set:
flameSprite.setPosition(rocket.getX() + rocket.getWidth() / 2 - w, rocket.getY() - 15);
Upvotes: 2