I'm having some problems with the routine I wrote to check visibility in my strategy game. Basically, I shoot a number of rays from the player to check where the player can see and or attack/travel to. This works fine. It actually works a little too well.
The ploy_ray call back just checks the pixel x and y value (divided by 16) and decides whether to stop that ray, or allow it to continue (and mark that map location as visible).
The problem is, sometimes the rays will squeeze through the gaps of certain structures and render the space as visible. Can anyone suggest a way of stopping this?
| 1 | void map_render_entity_visibility( BITMAP *bmp, int x_o, int y_o, int x, int y, int vis_radius ) { |
| 2 | |
| 3 | int s_x = x, s_y = y, t_x = 0, t_y = 0, a=0; |
| 4 | fixed angle = itofix(0); |
| 5 | bHaltRayCast = 0; r_x = x_o; r_y = y_o; |
| 6 | e_x = x; e_y = y; e_r = vis_radius; |
| 7 | |
| 8 | for ( a = 0; a <= 255; a+=5 ) { |
| 9 | |
| 10 | angle = itofix(a); |
| 11 | |
| 12 | do_line (bmp, (x_o+(x*16))+8, y_o+(y*16)+8, |
| 13 | (x_o+(x*16))+8 + fixtoi (60 * fcos (angle)), |
| 14 | y_o+(y*16)+8 + fixtoi (60 * fsin (angle)), |
| 15 | 1, plot_ray); |
| 16 | |
| 17 | bHaltRayCast = 0; |
| 18 | |
| 19 | } |
| 20 | |
| 21 | } |
If you don't have any real-time constraints. Just make the lines thinker so that they can't stream through small gaps. You could just do your routine four times with the offsets {0,0}{1,0}{0,1}{1,1} and if all four makes it through it is visible.
So your game is tile based? Otherwise I'd suggest using polygonal visibility regions.