【问题标题】:LIBGDX ScissorStack Example?LIBGDX ScissorStack 示例?
【发布时间】:2013-11-10 07:05:16
【问题描述】:

我希望在精灵单元上创建一种战争迷雾风格的效果,并被告知 scissorstack 可以完成这项工作,但我不知道我做错了什么......

我设置了典型的 libgdx。我为每个游戏角色开设了一门课。我想通过配置每个班级来控制每个单元看到的“多远”。所以说我想要拥有战争迷雾 5 的 MainPlayer,但一个 Pawn 单位在该单位所在的图块周围有 1 个空间的战争迷雾。

我已经加载了精灵,tmx 地图和碰撞检测工作正常。现在我只是不知道应该将 scissorstack 代码放在 Player 类中的哪个位置/如何让它工作。我也想知道雾周围可见瓷砖的坐标...

public class Player extends Sprite implements InputProcessor{
private Vector2 velocity = new Vector2();

public Player(Sprite sprite, TiledMapTileLayer collision layer){
super(sprite);
this.collisionLayer=collisionLayer;
}

public void draw(SpriteBatch spriteBatch){
update(Gdx.graphics.getDeltaTime());
super.draw(spriteBatch);
}

public Vector2 getVelocity() {
        return velocity;
    }

    public void setVelocity(Vector2 velocity) {
        this.velocity = velocity;
    }

    public float getSpeed() {
        return speed;
    }

    public void setSpeed(float speed) {
        this.speed = speed;
    }

    public float getGravity() {
        return gravity;
    }

    public void setGravity(float gravity) {
        this.gravity = gravity;
    }

    public TiledMapTileLayer getCollisionLayer() {
        return collisionLayer;
    }

    public void setCollisionLayer(TiledMapTileLayer collisionLayer) {
        this.collisionLayer = collisionLayer;
    }


    public boolean keyDown(int keycode){
        switch(keycode){
        case Keys.W:

            setY(getY()+1*50);//velocity.x=speed;
            break;
        case Keys.A:
            setX(getX()-1*50);//velocity.x=-speed;
            break;
        case Keys.D:
            setX(getX()+1*50);//velocity.x=speed;
            break;
        case Keys.S:
            setY(getY()-1*50);//velocity.y=-speed;
            break;
        }
        System.out.println(getX()/50+", "+getY()/50);   
            return true;
    }

    public boolean keyUp(int keycode){
        switch(keycode){
        case Keys.W:
            velocity.y=0;
            break;
        case Keys.A:
            velocity.x=0;
            break;
        case Keys.D:
            velocity.x=0;
            break;
        case Keys.S:
            velocity.y=0;
            break;

        }return true;

    }

    @Override
    public boolean keyTyped(char character) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        // TODO Auto-generated method stub
        return false;
    }





}




}

我尝试了以下操作:Making a Group hide Actors outside of its bounds

这是我在 Player 类的 draw 方法中尝试的:

    public void draw(SpriteBatch spriteBatch){
        update(Gdx.graphics.getDeltaTime());
Rectangle scissors = new Rectangle();
Rectangle clipBounds = new Rectangle(getX(),getY(),4*width,4*height);
ScissorStack.calculateScissors(camera, spriteBatch.getTransformMatrix(), clipBounds, scissors);
ScissorStack.pushScissors(scissors);
spriteBatch.draw(...);
spriteBatch.flush();
ScissorStack.popScissors();

        super.draw(spriteBatch);
    }

我很困惑。在播放器实体类中使用 scissorstack 的示例会有所帮助。谢谢

【问题讨论】:

  • 你试过什么?请在你的代码中删除所有不必要的东西。没有人需要知道 InputProcessing。 Player 的剪辑边界在哪里? Pawn周围的战争迷雾“空间”是什么意思?您希望 Pawn 被雾覆盖而其他一切都可见吗?
  • 我的目标是能够按实体类型调整“可见性”。所以我有一个玩家/英雄类和一个典当类。更新问题
  • 您是否考虑过剔除而不是剪裁?换句话说,选择不绘制而不是绘制并让 OpenGL 处理边界。
  • 这是一个有趣的观点。我在哪里学习差异?所以你是说剪刀堆栈仍然绘制所有内容然后修剪,而剔除你可以决定不绘制什么?我没有在网上找到足够好的 scissorstack 资源。我会考虑剔除。 TY

标签: java libgdx


【解决方案1】:

不久前,NathanSweet 很友好地帮助我弄清楚如何让它工作(对于 LibGdx 0.9.9): CalculateScissors

所以对于任何寻找示例的人来说,这段代码以前对我有用:

Rectangle scissor = new Rectangle();
Rectangle clipBounds = new Rectangle(0, 0, worldW, worldH);

...

batch.begin();

...

ScissorStack.calculateScissors(camera, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), batch.getTransformMatrix(), clipBounds, scissor);
ScissorStack.pushScissors(scissor);

...

ScissorStack.popScissors();
batch.end();

您所需要的只是使用 clipBounds 的值,以便产生所需的效果。

【讨论】:

    【解决方案2】:

    我自己没有用过,但我会这样尝试:

    public void draw(SpriteBatch spriteBatch) {
        update(Gdx.graphics.getDeltaTime());
        Rectangle scissors = new Rectangle();
        Rectangle clipBounds = new Rectangle(getX(),getY(),4*width,4*height);
        ScissorStack.calculateScissors(camera, spriteBatch.getTransformMatrix(), clipBounds, scissors);
        ScissorStack.pushScissors(scissors);
        super.draw(spriteBatch);
        ScissorStack.popScissors();   
    }
    

    【讨论】:

    • @user2556304 有任何结果吗?有什么改变吗?有什么错误吗?此外,我认为 Scissors 无论如何都不是解决方案,因为您只会看到所有单位都可以看到的那些实体……您是否尝试过按半径手动过滤 Actor 并手动禁用它们?
    猜你喜欢
    • 2014-05-18
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多