简体   繁体   English

libgdx精灵无法正确旋转

[英]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] [编辑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. 我认为问题与以下事实有关:我总是将y设置为rocket.getPosition().y-Constants.ROCKET_HEIGHT (从火箭射出的火的高度是火箭高度的一半,并且具有相同的值宽度),仅当火箭垂直站立时才起作用,但是例如当其在其侧面时,需要从位置的x值中减去它。 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] [编辑2]

Pictures 图片

When vertical: 垂直时: 垂直时

When turned to it's side: 转到侧面时: 水平时

[EDIT 3] All the code [编辑3]所有代码

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 [编辑4]新图片

在此处输入图片说明

在此处输入图片说明

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? 如何在libgdx中的x轴上旋转图像?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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