1

I am making a 2d game and i want to make 2d visibility. So my idea is to make the map and then mask it with another black (for example) polygon so that the player can see through the black polygon the map. So my idea is to draw some kind of a hole(polygon) trough the black polygon in order to see the map which is below the black polygon. I have already made the visibility but i have no idea how to make the mask.

This is what the program looks so far http://prntscr.com/7y36ev

but the problem is that if there are any objects in the dark they would still be rendered and the player could see them. I can shoot a ray and see if they are in line of sight but it would be a bit buggy because the rays will be shot in the center of the objects and if for example only their shoulder is showing up the object will not be rendered. Can somebody help ?

Edit - for anyone who wants to do the same I made it with Path2D. First I make the outside polygon with Path2D, then make the inside polygon(the hole) and append the hole to the outside polygon. This is sample code:

public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.red);

        Path2D.Double poly = new Path2D.Double();
        Path2D.Double hole = new Path2D.Double();

        hole.moveTo(40,40);
        hole.lineTo(40,60);
        hole.lineTo(60,60);
        hole.lineTo(60,40);
        hole.lineTo(40,40);

        poly.moveTo(80,2);
        poly.lineTo(80,80);
        poly.lineTo(2,80);
        poly.lineTo(2,2);
        poly.lineTo(80,2);

        poly.append(hole, true);
        g2d.fill(poly);  
    }
Nikolai Nikolov
  • 428
  • 3
  • 13

1 Answers1

0

Hi so far I understood your problem, you are intend to let each pixel of the map to be visible when the mask has a black pixel on the same position? If I'm right then:

Get the BufferedImage of the Map and the BufferedImage of the Mask and using two for loops, check if Mask has black-Pixel at Position (X,Y) if so then add pixel of Map to a new BufferedImage to position (X,Y) otherwise add a black Pixel to the resulting BufferedImage at position (X,Y).

Edit: Little bit Pseudo Code

BufferedImage mapBI = .... // the map as BufferedImage
BufferedImage maskBI = .... // the mask as BufferedImage
BufferedImage resultBI = ... // new BufferedImage with same size as mask 

for (i=0; i< maskBI.width; i++){
    for(j=0; j< maskBI.height; j++){
        if (maskBI.pixelAt(i,j) == blackPixel){
            resultBI.setPixel(i,j,mapBI.pixelAt(i,j));
        }else{
            resultBI.setPixel(i,j,blackPixel);
        }
    }
}

I hope you understand what I mean.

Kami
  • 459
  • 1
  • 6
  • 27
  • Well its a good idea but i am drawing with Graphics2D and i don't know how to edit pixels with Graphics only. I am using the same algorithm as this one http://ncase.me/sight-and-light/. Do you have an idea how to make it without editin each pixel ? – Nikolai Nikolov Jul 28 '15 at 20:19
  • You can convert Graphics2D to BuffredImage to access each pixel : Please read [here](http://stackoverflow.com/questions/6575578/convert-a-graphics2d-to-an-image-or-bufferedimage) – Kami Jul 28 '15 at 20:32
  • Yes but wont that be slow because if i have a big resolution it would slow down a lot. If i make a whole in the mask then draw the map first and then the mask i wont have to go trough each pixel, if that is possible though – Nikolai Nikolov Jul 28 '15 at 20:38
  • That is possible but, you have to know where to draw the hole and that information is only stored in the mask. Another option is to define a mask Object which has the information about the Circle or Rectangel which has **not** to be drawn and then draw a Polygon to the edges of that mask object. Circle would be little bit difficult, but a Rectangle should be easy to solve. – Kami Jul 28 '15 at 20:53
  • But I think, my answer with the pseudo code is the better approach. I have used BufferedImage a lot for drawing or manipulating Images. That should solve your problem. You should give a try and test the performance. – Kami Jul 28 '15 at 20:58
  • 1
    Yes i might try both approaches. Thanks for the help and the fast answer ! – Nikolai Nikolov Jul 28 '15 at 21:02
  • You are welcome :) if that answer helps you could you please accept my answer ? :) thanks :) – Kami Jul 28 '15 at 21:57