2

I am looking to create a fog-of-war style effect around on sprite units and was told scissorstack would do the job but I can't figure out what i am doing wrong...

I have the typical libgdx set up. I have a class for every Game Character. I would like to control 'how far out' each unit sees by configuring each class. So say I want MainPlayer with fog of war 5, but a Pawn unit to have a fog of war of 1 space around the tile the unit is located in.

I have the sprite loaded, tmx map and collision detection working fine. Now I just can't figure out where I should put the scissorstack code in the Player class / how to get it working. I'd also like to know the coordinates of the visible tiles around the fog...

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;
    }





}




}

I tried following: Making a Group hide Actors outside of its bounds

This is what i tried in the Player class' draw method:

    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);
    }

I am beyond confused. An example of usin scissorstack within the player entity class would be helpful. thanks

Community
  • 1
  • 1
user2556304
  • 159
  • 2
  • 15
  • What have you tried? And please get rid of all unnecessary stuff in your code there. Nobody needs to know about the InputProcessing. Where are the clipping boundaries for your `Player`? What do you mean by one "space" of fog of war around a Pawn? You want the Pawn to be covered in fog and everything else to be visible? – noone Nov 10 '13 at 07:22
  • My goal is to be able to adjust the "visibility" by entity type. So I have a Player/hero class and a Pawn class. Updated question – user2556304 Nov 10 '13 at 07:41
  • Have you considered culling rather than scissoring? In other words, choose to NOT draw as opposed to drawing and having OpenGL take care of the bounds. – R Hyde Nov 10 '13 at 15:08
  • This is an interesting point. Where do I learn the differences? So you're saying scissor stack still draws everything and then trims whereas culling you can decide what not to draw period? I haven'tfound enough good resources online for scissorstack.. I'll look into culling. TY – user2556304 Nov 10 '13 at 17:46

2 Answers2

2

NathanSweet was kind enough to help me out figuring out how to make it work (for LibGdx 0.9.9) a while back: CalculateScissors

So for anyone looking for an example, this code has worked for me before:

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();

All you need is to play around with the clipBounds' values so the desired effect takes place.

cavpollo
  • 4,071
  • 2
  • 40
  • 64
0

I haven't used it myself, but I'd try it this way:

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();   
}
noone
  • 19,520
  • 5
  • 61
  • 76
  • @user2556304 Any results? Anything changed? Any error? Furthermore I think Scissors aren't a solution anyway, because you will only see those Entities, which are seen by ALL of your units... Have u tried to manually filter your Actors by radius and disable them by hand? – noone Nov 10 '13 at 20:13