【问题标题】:how to rotate a vector along with a sprite?如何旋转矢量和精灵?
【发布时间】:2015-01-13 15:24:37
【问题描述】:

我的需要是让一艘宇宙飞船和一艘航母一起旋转,我会解释的,我从这个开始:

矢量上的点 A 是原点,与航天器的精灵重合。 现在,我的问题是,当飞船旋转时,飞船头部的红点(这是另一个精灵)必须保持在头部。

如果我用触摸屏旋转飞船,我可以用红点定期停留在B点。

这是因为我要给飞船喂食,所以我可以将飞船的原点与红点的点进行比较,从而得到它必须继续前进的方向。

我是 libgdx 的新手。 我希望我是清楚的。

谢谢

我的代码摘录:

    sprite.setPosition(Gdx.graphics.getWidth() / 2 - sprite.getWidth()/2, Gdx.graphics.getHeight() / 2 - sprite.getHeight()/2);
    point.setPosition(sprite.getX() + (sprite.getWidth()/2 - point.getWidth()/2), sprite.getY() + (sprite.getHeight() / 2 + point.getHeight()/2));

    if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){

        if(Gdx.input.getX() < Gdx.graphics.getWidth() / 2)
        {
            //System.out.println("x: " + Gdx.input.getX() + " - y: " + Gdx.input.getY());
            sprite.setRotation(rotation++);
            point.setPosition(sprite.getX() + (sprite.getWidth()/2 - point.getWidth()/2), sprite.getY() + (sprite.getHeight() / 2 + point.getHeight()/2));
        }
        else
        {
            //System.out.println("x: " + Gdx.input.getX() + " - y: " + Gdx.input.getY());
            sprite.setRotation(rotation--);
            point.setPosition(sprite.getX() + (sprite.getWidth()/2 - point.getWidth()/2), sprite.getY() + (sprite.getHeight() / 2 + point.getHeight()/2));
        }
    }

回答后编辑 我已经添加了 Actor 的代码

public class MyActor extends Actor {
    Sprite sprite;

    public MyActor() {
        sprite = new Sprite();
        sprite.setTexture(new Texture("rocket.png"));

        setWidth(sprite.getWidth());
        setHeight(sprite.getHeight());
        setBounds(0, 0, getWidth(), getHeight());
        setTouchable(Touchable.enabled);
        setX(200);
        setY(100);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        Color color = getColor();
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(sprite, getX(), getY());
    }
}

但我不确定实施...您能给我一些建议吗?

【问题讨论】:

    标签: java android vector libgdx coordinates


    【解决方案1】:

    只需像旋转宇宙飞船一样旋转红点。只需偏移红点的旋转中心,使其与飞船旋转点所在的位置相同。所以不要围绕它的中心旋转红点,而是围绕飞船中心旋转。

    【讨论】:

      【解决方案2】:

      我建议你使用Actor 而不是Sprite

      您可以访问许多精灵不可用的高级功能。

      您甚至可以根据需要对演员进行分组和轮换。

      参考:Scene2D

      希望这会有所帮助。
      祝你好运。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-23
        • 1970-01-01
        相关资源
        最近更新 更多