简体   繁体   中英

libgdx sprite not rotating correctly

I'm making a game with a spaceship and when the forward button is pressed an image of fire coming out of the back has to appear. This works but only when the spaceship is standing vertical. When it turns upside down it's not perfectly aligned with the back of the spaceship. This is the update function of the class I made for it.

public void update(Rocket rocket) {
    sprite.setOrigin(rocket.getOriginX(), rocket.getOriginY()+Constants.ROCKET_HEIGHT);
    sprite.setSize(Constants.ROCKET_BOOST_WIDTH, Constants.ROCKET_BOOST_HEIGHT);
    sprite.setPosition(rocket.getPosition().x - sprite.getWidth()/2, rocket.getPosition().y-Constants.ROCKET_HEIGHT);
    sprite.setRotation(rocket.getBody().getAngle()* MathUtils.radDeg);
}

I've tried setting the origin to the origin of the rocket which seems logical to me, but I think I don't fully understand what the origin is.

[EDIT 1]

I don't think the problem is with the origin, I set it now to

sprite.setOriginCenter();

I think the problem has to do with the fact that i always set the y to rocket.getPosition().y-Constants.ROCKET_HEIGHT (the height of the fire coming out of the rocket is half the height of the rocket and has the same width), which only works when the rocket is standing vertical, but when it's on it's side for example, it needs to be subtracted from the x value of the position. I still don't know how to fix this and I have a very similar problem in a different part of the game. If someone can fix this it would be extremely helpful as I don't have a clue.

[EDIT 2]

Pictures

When vertical: 垂直时

When turned to it's side: 水平时

[EDIT 3] All the code

public class RocketBoostDraw {
        private Sprite sprite;
        private Texture texture;
        private float offset = 0;

        public RocketBoostDraw(Rocket rocket) {
            texture = new Texture(Gdx.files.internal("boost.png"));
            sprite = new Sprite(texture);
            sprite.setSize(Constants.ROCKET_BOOST_WIDTH, Constants.ROCKET_BOOST_HEIGHT);
            sprite.setPosition(rocket.getPosition().x, rocket.getPosition().y - Constants.ROCKET_HEIGHT);
            offset = Constants.ROCKET_HEIGHT/2 + sprite.getHeight()/2;
        }

        public void update(Rocket rocket) {
            sprite.setPosition(rocket.getPosition().x + offset * MathUtils.cosDeg(rocket.getRotation()), rocket.getPosition().y+(offset)*MathUtils.sinDeg(rocket.getRotation()));
            sprite.setRotation(rocket.getBody().getAngle()* MathUtils.radDeg);
        }

        public void draw(SpriteBatch batch, Rocket rocket) {
            sprite.draw(batch);
        }
    }

[EDIT 4] New pictures

在此处输入图片说明

在此处输入图片说明

Instead of rotating the sprite you can directly rotate your texture right when you draw it to the batch. I have given the same answer elsewhere and it works for everyone. Here is the link to my answer:

How can i rotate an image over its x axes in libgdx?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM