## on the verge of gouging my eyes out

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

### on the verge of gouging my eyes out

ok this is killing me.. i have a 10x7 map and im trying to detect if the player can see the square
if the map is '#' then it is a wall, otherwise, it isnt.

how im doing this is basically imagining a line from the player (player.x+16,player.y+16) (the +16 is for the center of the player) to the center of the box (x+32 y+32) the argument to the function is the top left or the box

then im tracing along this line via means of the direction (end-begin).normal();

here is my current code:

Code: Select all
`bool CanSeeSQ(int x, int y){   //vec2 = float x,y : vec2i = int x,y   bool z=0;   int n=-30,m=-30;   vec2i start=vec2i((game.player.x+16),(game.player.y+16)); //start of line   vec2i end  =vec2i((x+32),(y+32));  //end of line   vec2  slope=vecsubtract(end,start);   float mag=normalize(&slope); // normalize(blah) returns vector magnitude   if (mag==0)      return true;   if (mag>=game.map.visibility)      return false;   slope.x*=32;   slope.y*=32;   for (vec2 curr=start;   ((slope.x)>0?(curr.x<end.x):(curr.x>end.x))&&((slope.y>0)?(curr.y<end.y):(curr.y>end.y)); // loop to the end of the line, yea this parts confusing :D   curr.x+=slope.x,curr.y+=slope.y){ // move out current position      if (z&&!(n==(int)curr.x/64)&&(m==(int)curr.y/64)) // if we saw a box, and its not the first box we saw, then we cant see the square         return false;      if (game.map[(int)curr.x/64][(int)curr.y/64]=='#'){ // this check is to make sure that we dont hide the box we're next to, else there will be a black box next to you when you normally can see the box next to you         n=(int)curr.x/64;         m=(int)curr.y/64;         z=1;      }   }   return true;}`

this pretty much crases and burns.. and i dont know why...

this is what it looks like:

http://et.ath.cx/chogblasters_current.jpg

and this is what it should look like:

http://et.ath.cx/chogblasters_iwishitwasthis.jpg

and i cant figure out whats wrong.. can anyone see a flaw in my method or something? because i sure cant :-\

thanks
:]

schloob

Posts: 1853
Joined: Mon Feb 16, 2004 10:29 am
Location: Seattle

Bugdude
Moderator

Posts: 2480
Joined: Sun Aug 29, 2004 1:58 am
Location: Living in sin

Bugdude wrote:Google for "Bresenham Line-Drawing Algorithm".

Hehe, "bresenham"... lol

??? Bugdude is back?

Invictus

Posts: 3054
Joined: Tue Oct 21, 2003 12:59 pm

Cold Flame wrote:??? Bugdude is back?

Yup.

Bugdude
Moderator

Posts: 2480
Joined: Sun Aug 29, 2004 1:58 am
Location: Living in sin

Bugdude wrote:
Cold Flame wrote:??? Bugdude is back?

Yup.

Darn... -.-

Bugdude's postcount = 666

lol..

Invictus

Posts: 3054
Joined: Tue Oct 21, 2003 12:59 pm

after i tried gouging my eyes out i found the solution, that line algorithm just confused me more..

here it is:

Code: Select all
`bool CanSeeSQ(int x2, int y2){   vec2  BEGIN = vec2(game.player.x+8,game.player.y+8);   vec2  END   = vec2(x2+32,y2+32);   vec2  SLOPE = vecsubtract(END,BEGIN);   float MAG   = normalize(&SLOPE);   if (MAG==0)      return true;   if (MAG > game.map.visibility)      return false;   SLOPE.x     *= 32;   SLOPE.y     *= 32;   for (   vec2 CURR   = BEGIN;   notthereyet(CURR.x, END.x, SLOPE.x) && notthereyet(CURR.y, END.y, SLOPE.y);   CURR.x+=SLOPE.x,CURR.y+=SLOPE.y){      if (game.map[(int)CURR.x/64][(int)CURR.y/64]=='#'){         if (!(x2/64==(int)CURR.x/64&&y2/64==(int)CURR.y/64))            return false;      }   }   return true;}`
:]

schloob

Posts: 1853
Joined: Mon Feb 16, 2004 10:29 am
Location: Seattle