Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  [Help]2D tile map lighting  (Read 7567 times)
0 Members and 1 Guest are viewing this topic.
Offline Tom-Todd

Senior Newbie





« Posted 2012-04-23 19:39:47 »

Hello, I'm currently making a game. I'm using a tile map and am trying to get lighting and shadows working for it.
It currently looks like this -


And the bit which controls the lighting is the following class -

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
package kingdom.blue;
 
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import java.awt.Color;

 
public final class map{
   
        public static final int WIDTH = 100;
        public static final int HEIGHT = 12;
        public static final int MAPLEVEL = 2;
        public static final int TILE_SIZE_X = 50;
        public static final int TILE_SIZE_Y = 50;
        public BufferedImage[][] data = new BufferedImage[WIDTH][HEIGHT];
        public Color [] [] lightMap = new Color [WIDTH][HEIGHT];
        public LoadMapImages lmi;
        public int mapPosX;
        public int mapPosY;
        public double round = 0.5;
        boolean mapLoaded;
       
        public void renderMap(){
          for(int x = 0;x < WIDTH;x++){
            for(int y = 0;y < HEIGHT;y++){
                data[x][y] = lmi.ground5;
               data[7][HEIGHT-8] = lmi.ground3;
               data[8][HEIGHT-8] = lmi.ground1;
               data[9][HEIGHT-8] = lmi.ground2;
               data[x][HEIGHT-1] = lmi.ground1;
               data[6][HEIGHT-4] = lmi.ground2;
               data[5][HEIGHT-4] = lmi.ground3;
               data[12][HEIGHT-8] = lmi.ground2;
               data[11][HEIGHT-8] = lmi.ground3;
               //data[15][HEIGHT-4] = lmi.ground2;
              //data[14][HEIGHT-4] = lmi.ground3;
             
               data[21][HEIGHT-4] = lmi.ground2;
               
               data[20][HEIGHT-4] = lmi.ground3;
               data[24][HEIGHT-6] = lmi.ground2;
               data[23][HEIGHT-6] = lmi.ground3;
               
               data[30][HEIGHT-3] = lmi.ground2;
               data[29][HEIGHT-3] = lmi.ground3;
               
               //data[21][HEIGHT-11] = lmi.ground1;
              data[21][HEIGHT-9] = lmi.ground1;
               //data[22][HEIGHT-11] = lmi.ground1;
              data[22][HEIGHT-9] = lmi.ground1;
               //data[20][HEIGHT-11] = lmi.ground1;
              data[20][HEIGHT-9] = lmi.ground1;
               //data[20][HEIGHT-10] = lmi.ground1;
              //data[22][HEIGHT-10] = lmi.ground1;
              data[21][HEIGHT-10] = lmi.torch;
               
               
               //data[24][HEIGHT-7] = lmi.torch;
              lightMap [x][y] = new Color(0,0,0,200);
            }
          }
        }
       
        public map(Player player){
           lmi = new LoadMapImages();
        }  
        public void paint(Graphics2D g)
        {          
                //g.setColor(Color.GRAY);
               for (int x = 0; x < WIDTH; x++){
                        for (int y = 0; y < HEIGHT; y++)
                        {
                            g.setColor(lightMap[x][y]);
                            g.drawImage(data[x][y],mapPosX + x*TILE_SIZE_X, mapPosY + y*TILE_SIZE_Y, TILE_SIZE_X, TILE_SIZE_Y, null);
                            g.fillRect(mapPosX + x*TILE_SIZE_X,mapPosY + y*TILE_SIZE_Y, TILE_SIZE_X, TILE_SIZE_Y);
                            //System.out.println("map drawing" + lightMap[(int)((x+70)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y]);
                       }
                }
       }
       public void scrollMapY (int scrAmount, int y, int x, int cy){
               mapPosY += scrAmount;
               mapPosY += scrAmount;
       }
       
       public void lightMap (int plrY, int plrX){
           int baseDarkness;
           
           //TORCH LIGHT STUFF <to be placed in own file?>
          for (int x = 0; x < WIDTH; x++){
               for (int y = 0; y < HEIGHT; y++){
                   if(data[x][y] == lmi.torch){
                       baseDarkness = lightMap [x+10][y].getAlpha();
                       int dispersalRate = baseDarkness/10;
                       for(int xl = 0; xl < 7; xl++){
                           for(int yl = 0; yl < 8; yl++){
                               if(y+yl < HEIGHT){
                                   if(y+yl > -1){
                                       
                                        lightMap [x][y] = new Color (0,0,0,10);
                                        lightMap [x+1][y] = new Color (0,0,0,15);
                                        lightMap [x-1][y] = new Color (0,0,0,15);
                                        lightMap [x][y+1] = new Color (0,0,0,15);
                                        lightMap [x][y-1] = new Color (0,0,0,15);
                                        if(((dispersalRate*xl)+(dispersalRate*yl)) < 200){
                                            lightMap [x+xl][y+yl] = new Color (0,0,0,((dispersalRate*xl)+(dispersalRate*yl)));
                                            lightMap [x-xl][y+yl] = new Color (0,0,0,((dispersalRate*xl)+(dispersalRate*yl)));
                                        }
                                        else{
                                            lightMap [x+xl][y+yl] = new Color (0,0,0,200);
                                            lightMap [x-xl][y+yl] = new Color (0,0,0,200);  
                                        }
                                    }
                               }
                                    if(y-yl < HEIGHT){
                                        if(y-yl > -1){
                                            if(((dispersalRate*xl)+(dispersalRate*yl)) <= 200){
                                                lightMap [x-xl][y-yl] = new Color (0,0,0,((dispersalRate*xl)+(dispersalRate*yl)));
                                                lightMap [x+xl][y-yl] = new Color (0,0,0,((dispersalRate*xl)+(dispersalRate*yl)));
                                            }
                                            else{
                                               lightMap [x-xl][y-yl] = new Color (0,0,0,200);
                                               lightMap [x+xl][y-yl] = new Color (0,0,0,200);
                                            }
                                        }
                                    }
                           }
                        }
                    }
                }        
            }
       }
       
       
       public boolean blocked(int x,int y,int dir){
        try{
            //if(y <= cy){
           //------------------------------------------------------------------------------------->
           if(dir == 0){
           
            if(data[(int)((x+70)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground1){
                return true;  
            }
            if(data[(int)((x+70)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground2){
                return true;  
            }
            if(data[(int)((x+70)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground3){
                return true;  
            }
           
           
           if(data[(int)((x+150)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground1){
             return true;  
           }
           if(data[(int)((x+150)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground2){
             return true;  
           }
           if(data[(int)((x+150)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground3){
             return true;  
           }
           return false;
          }
         
           
          else if(dir == 1){
             
            if(data[(int)((x+20)-mapPosX)/TILE_SIZE_X][(int)((y+150) -mapPosY)/TILE_SIZE_Y] == lmi.ground1){
                return true;  
            }
            if(data[(int)((x+20)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground2){
                return true;  
            }
            if(data[(int)((x+20)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground3){
                return true;  
            }
           
           
           if(data[(int)((x+100)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground1){
             return true;  
           }
           if(data[(int)((x+100)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground2){
             return true;  
           }
           if(data[(int)((x+100)-mapPosX)/TILE_SIZE_X][(int)((y+150)-mapPosY)/TILE_SIZE_Y] == lmi.ground3){
             return true;  
           }
           return false;
          }
           
        }
       catch(Exception es){
       }
        return false;
      }
     
}


If anyone can help me get shadows working so that blocks can produce shadows, or help me implement a better lighting system it would be much appreciated, It's my first real game so I'm not quite sure how to go about it...
Offline davidllanos22

Junior Member


Projects: 2


I'm just a learner!


« Reply #1 - Posted 2012-04-23 20:33:03 »

I made something like that a few weeks ago. I'll try to explain what I made:

-First af all, the Tile class should have a variable lightLevel or something like that, set it to 0. That way that tile won't be visible.(You can also make that if the light level is 0 don't draw that tile for better performance Smiley );

-On the draw method of the tile I used some black images with different alpha values . So first draw the tile and the alpha images just above. Make a switch or if's and select the image according to the lightLevel.

-Draw a map using an array of Tiles.

-On the torch class I made that:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public Torch(int x, int y){

//change the light livel of all the adjacent tiles(make the shape that you want)
Map.tiles[x][y].lightLevel=0;
Map.tiles[x+1][y].lightLevel=1;
Map.tiles[x+2][y].lightLevel=1;
Map.tiles[x+3][y].lightLevel=2;
Map.tiles[x][y+1].lightLevel=1;
Map.tiles[x][y+2].lightLevel=1;

//...and many more
}


So, when you place a torch on the map it will assign the lightLevel.

I hope it's understable. Man, I suggest you to use more classes, it will simplify your work so much.

Offline davidllanos22

Junior Member


Projects: 2


I'm just a learner!


« Reply #2 - Posted 2012-04-23 20:36:04 »

I've just realized that I write a semicolon after the parentheses xDD

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Tom-Todd

Senior Newbie





« Reply #3 - Posted 2012-04-23 20:39:08 »

Ah, fair enough, it's slightly similar in that you're using alpha-ed black images and I'm using alpha-ed black squares, I was planning on moving the torches stuff in to it's own class, but you're right I should give the tiles their own class, that'll simplify things later. Also, did you have any shadows in yours or did you not bother?
Offline davidllanos22

Junior Member


Projects: 2


I'm just a learner!


« Reply #4 - Posted 2012-04-23 20:59:31 »

Java is OOP so..make your game OOP as well  Grin No I didn't make shadows, but now I'm thinking how can I make them haha Post your progress and let us know how it's going! Oh and wellcome to JGO Smiley

Offline Tom-Todd

Senior Newbie





« Reply #5 - Posted 2012-04-23 21:00:41 »

Haha, fair enough, thanks! I'll be sure to post up progress and let you know if I find out any ways of doing shadows...
Offline 65K
« Reply #6 - Posted 2012-04-23 21:08:10 »

I pondered on this for a while but eventually switched to OpenGL and use Pitbuller's lighting today. Follow my signature for some info.
Or check out http://code.google.com/p/straightedge/ for Java2D.

Offline Tom-Todd

Senior Newbie





« Reply #7 - Posted 2012-04-23 21:12:28 »

That could be quite useful, I'll take a look! -Thanks  Smiley
Offline Tom-Todd

Senior Newbie





« Reply #8 - Posted 2012-04-23 21:17:44 »

Wouldn't using that library mean changing how I'm doing the mapping?
Offline Ciaran54

Junior Member


Medals: 4



« Reply #9 - Posted 2012-04-23 21:25:17 »

Few things before I get to your main question:

1. As far as I know, it is not common to store game maps as 2 dimensional arrays of bufferedImages. This makes any sort of logic look quite messy and usually people tend towards having 2d arrays of ints if the tiles aren't dynamic (or enumerations, if you like to keep it easy to read) (if they are dynamic, give them their own classes), which represent tiles, and are later converted by the renderer to render a static bufferedImage which represents that tile number. Using ints allows you to say:

if(tileNumber>=0 && tilenumber<=20){   //where only floor tiles have IDs between 0 and 20
//do stuff specific to all floor tiles
}

rather than

if(tile.equalTo(lmi.floor) || tile.equalto(lmi.floor2) || tile.equalto(lmi.floor3) etc...


Maybe it's just me, but i think that although an array of buffered images may seem easier for rendering, it is not as neat when you use it in logic.

/////////////////////////////////

2.Rather than searching through every tile to find a torch, which can get quite intensive for larger maps, you may find it would be better to keep an arrayList of points where torches reside.

/////////////////////////////////

Now onto your main question. Depending on how you want your lighting to look, and how many torches you are going to be using, there are many different lighting systems you could use.

Probably one of the simplest to implement would be to check whether each tile is visible from the light source.

so you would need to make a method for checking the visibility from the torch tile to the destination tile, and use the result from that to figure out whether the tile should be lit or not.


For example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
public static boolean isTileAVisibleFromB(Point a, Point b){   //couldn't come up with a decent method name :P

   if(a.getX() == b.getX() && a.getY() == b.getY()) return true;

   if(a.getX() != b.getX()){         //To prevent dividing by zero
     double gradient = ((double)a.getY() - (double)b.getY()))/((double)a.getX() - (double)b.getX()));
     
      if(a.getX() > b.getX()){
         int length = a.getX() - b.getX();

         int x;
         int y;

         while(a.getX() + x != b.getX() && a.getY() + y != b.getY()){
            y = (int)(x*gradient);
           
            if( the tile[x + a.getX()][y + a.getY()] cannot let light through){   //Pseudocode, you will need to check whether this tile can let light through
              return false;
            }
            x++;
         }


      else{
         int length = b.getX() - a.getX();

         int x;
         int y;

         while(a.getX() + x != b.getX() && a.getY() + y != b.getY()){
            y = (int)(x*gradient);
           
            if(the tile[x + b.getX()][y + b.getY()] cannot let light through){   //Pseudocode, you will need to check whether this tile can let light through
              return false;
            }
            x++;
         }
   }
   else{
      int length = a.getY() - b.getY();

      int y;

      if(length>0){
         while(a.getY() + y != b.getY()){
            if( the tile[x + a.getX()][y + a.getY()] cannot let light through){   //Pseudocode, you will need to check whether this tile can let light through
              return false;
            }
            y++
         }
      }
      else{
                  while(a.getY() + y != b.getY()){
            if( the tile[x + a.getX()][y + a.getY()] cannot let light through){   //Pseudocode, you will need to check whether this tile can let light through
              return false;
            }
            y++
         }
      }
   }

   return true;
}



That might not work completely, i just quickly typed it up in notepad, and there are some sections you need to fill in for yourself where I have just used pseudocode. You just need to check this before you light a tile, each time you were thinking of lighting a tile, and it will create sharp shadows, which are not completely realistic, but some people like 'em. If you wanted them with blurry edges there are a few different paths you could take, but just experiment! that code there should get the hard bit out of the way, but once you understand what I've written, you could customise it to include translucency, mirrors, coloured lighting, all sorts of stuff!.

One more thing, i'm not sure what you're doing with the lighting at the moment, but there is no need to redo the lighting every frame, only whenever you change something in the area of the torch.

Good luck, post us the results Wink

-Ciaran54

      
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Tom-Todd

Senior Newbie





« Reply #10 - Posted 2012-04-23 21:29:32 »

Wow, thats really helpful thanks!, with the everything posted here I should be able to take a good swing at getting this working. But I'm off to bed as I'm tired...
I'll make sure to post up progress later this week.  Smiley
Offline Ciaran54

Junior Member


Medals: 4



« Reply #11 - Posted 2012-04-24 10:53:09 »

Sorry, I slept on that code and realised that it would not work for line gradients of greater than 1, because it would miss out bits of the line checking. A method I have used in the past is to, if the gradient is greater than 1, do 1/gradient and step by y++ rather than x++.

But this makes a huge mess of code, surely someone can think of a better way?

Offline theagentd
« Reply #12 - Posted 2012-04-24 10:59:37 »

Determing visibility between a light and a tile is pretty easy, just raycast between them and check if there´s something in between. The problem here is performance. Doubling the radius of a light quadrouples the area the light covers, and therefore the number of raycasts. However, the raycasts also get a LOT more expensive since the extra raycasts needed are twice as long. In the end this scales VERY badly with larger lights, but if you only have a handful of lights with a radius of around 10 tiles or so you should be able to get realtime frame rates pretty easily.

I encountered the exact same problem when I was doing fog of war with "shadows" from walls. I ended up uploading a "wall map" to a texture and then doing the raycasts in a shader on the GPU for each tile. I got antialiased shadows thanks to free texture interpolation of the wall map, very simple code and almost no CPU cost at all since it´s handled by the GPU. Performance of lights with a radius of 10 tiles: around 50 000 lights at 60 FPS on relatively low end computers. Increasing the light size still has a very high cost though...

Myomyomyo.
Offline Tom-Todd

Senior Newbie





« Reply #13 - Posted 2012-04-24 14:38:33 »

Are there any good raycasting tutorials?
Offline Ciaran54

Junior Member


Medals: 4



« Reply #14 - Posted 2012-04-24 17:18:11 »

Raycasting is the fancy name for what I described, all you need is an algorithm to draw a line. You could finish the one I started, make your own, or google the 'Bresenham algorithm' for more information on how to calculate lines efficiently

Take into mind what theagentd said, about the lights scaling badly. But be sure you only update the lights every frame if you absolutely need to. If the lighting isn't going to change, You only need to calculate lighting when the level starts.If you can place your own blocks or lights, you only need to update that affects that area when you place a block/torch.
Offline Tom-Todd

Senior Newbie





« Reply #15 - Posted 2012-04-24 17:26:11 »

Fair enough, would the bit earlier about " if the gradient is greater than 1, do 1/gradient and step by y++ rather than x++." - would that work?
or are there better ways to do it?

EDIT: Also, why is it that your code doesn't work above gradients of 1?
Offline Ciaran54

Junior Member


Medals: 4



« Reply #16 - Posted 2012-04-24 18:48:43 »

My code won't work correctly for gradients above 1 because if you have a gradient below one, you need to increment the x value several times to see a change in the integer value of y. For a gradient greater than one, for every x increment, there have been several changes in the y value. So you will skip some values using my code, as it only ever implements x stepping.

I accidentally just forgot to write the second half of the code... Cry

By doing 1/gradient, and incrementing by y, you are essentially flipping the standard straight line equation from y=mx to x=y/m (or x=[1/m]y)by dividing both sides by m. you would need to change a few of the calculations, for it to work properly for y stepping, but it would definitely work.

The Bresenham algorithm is supposedly one of the most efficient ways of doing it, because it keeps clear of using double values and uses ints instead. I haven't looked into how the Bresenham algorithm works, but if I were a beginner, I would use whatever made sense to me the most, because copying and pasting code when you don't understand it and couldn't rewrite it yourself is usually a bad habit to get into.
Offline Tom-Todd

Senior Newbie





« Reply #17 - Posted 2012-04-24 18:59:23 »

Yeah, as much as I want things to work... I also want to learn HOW they work... so I know it's worth something Smiley
Offline Ciaran54

Junior Member


Medals: 4



« Reply #18 - Posted 2012-04-24 19:26:38 »

Well, if you want the basic premise for my code, so you can modify it to work for you/rewrite it, this is the basic idea i used:

1. Calculate the gradient and set one of the ends of the ray to be the graph origin.
2a. Stepping through the x coordinates, use the equation y=mx (where m is the gradient) to find the corresponding y value.
2b. take the x value and the calculated y value and check to see whether that tile is blocked
2c. If the tile is blocked, it is not possible to light the final tile
2c. If you have reached the final tile, it is possible for the light to travel this path
3. Repeat steps 2 until all tiles in the possible area have been checked.

I hope this has helped you, good luck Wink
Offline Tom-Todd

Senior Newbie





« Reply #19 - Posted 2012-04-24 19:42:01 »

That sounds simple, but it looks a bit more of a pain in the arse in practice...  Tongue Anyway, thanks for all the help, after I've changed my map system from an array of buffered images... I'll have a go at it, then post my results
Offline Tom-Todd

Senior Newbie





« Reply #20 - Posted 2012-04-25 18:42:12 »

First attempt didn't work -
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public static boolean tileVisible(int x1, int x2, int y1, int y2, map m){
       
        int deltax = x2 - x1;
        int deltay = y2 - y1;
        int y = y1;
        int ynum = deltax/2;
        for(int x = x1; x <= x2; x++){
            if(m.Map[(int)(x-m.mapPosX)/m.TILE_SIZE_X][(y-m.mapPosY)/m.TILE_SIZE_Y].lightBlock){
                return false;
            }
            ynum+= deltay;
            if(ynum >= deltax){
                ynum -= deltax;
                y++;        
            }
        }
        return true;
    }
Offline Ciaran54

Junior Member


Medals: 4



« Reply #21 - Posted 2012-04-25 19:19:47 »

I assume you were reading from here:
http://www.gamedev.net/page/resources/_/technical/game-programming/line-drawing-algorithm-explained-r1275

Basically, like the one I showed, what you have coded there only works for 1/8th of the possible lines, you need to scroll down a bit on the page to learn what to change for the others. It is very similar to the first one, but just has a few signs changed and the like.

Also, would you mind explaining why you are accessing map tiles in that array, by minusing and dividing and casting?
Offline Tom-Todd

Senior Newbie





« Reply #22 - Posted 2012-04-25 19:21:38 »

Well, because the tile map moves, you need to make it relative to the tile map rather than the canvas surely?
EDIT: Also, shouldn't that code at the bottom work then?
Offline Ciaran54

Junior Member


Medals: 4



« Reply #23 - Posted 2012-04-25 19:59:54 »

wait... are your x1, x2, y1, y2 coordinates referring to 2 individual pixels or 2 individual tiles?

If you use the bresenham on tile coordinates rather than pixels, you will need to do far fewer calculations, so it will be much faster. Sorry if I was unclear Wink

For logic in tile based games, most calculations, such as pathfinding, lighting, etc. will use the tile as the smallest unit for calculation.


You've adapted the short bit of code from the middle of the page, the stuff at the very bottom should work, yes.
Offline Tom-Todd

Senior Newbie





« Reply #24 - Posted 2012-04-25 20:01:41 »

Right well currently it's working on pixels... so yeah... I guess I should change that
Offline Tom-Todd

Senior Newbie





« Reply #25 - Posted 2012-04-25 20:35:32 »

To use it for tiles would it be exactly the same, but I just have to supply it with tile numbers instead of pixels then at the end check for tile numbers instead of the mess of code I currently have?
Offline Ciaran54

Junior Member


Medals: 4



« Reply #26 - Posted 2012-04-25 20:44:43 »

yep. You would access the array just by going
1  
m.Map[x][y]


I hope it works out Wink
Offline Tom-Todd

Senior Newbie





« Reply #27 - Posted 2012-04-25 20:49:08 »

hmm... I've changed it to that and it's now not working... the joys of bug fixing, here I come!
EDIT: Derp, just didn't change what I was printing to system xD may be why...
Offline Tom-Todd

Senior Newbie





« Reply #28 - Posted 2012-04-25 21:02:24 »

Well, I've got no errors, however my program just sits there on load... I think that it may be a little slow...
Offline Ciaran54

Junior Member


Medals: 4



« Reply #29 - Posted 2012-04-25 21:17:42 »

It should definitely not be so slow that your code doesn't run... You should not even notice a low frame rate with only one light...

What actually happens? Does any lighting show up at all?
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!