Log In  


Cart #raymaster-0 | 2023-11-22 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
3

Casting a ray

a basic line_cast algorithm

3


1

This is always very useful,maybe put it in a neat function? I did something similar in the past,but I'm always excited to see other approaches.


2

@taxicomics sorry for talking so long to respond
here the function that i used in diss cart

function ray_cast(x,y,x2,y2)
 local start_x,start_y=x/8,y/8
 local check_x,check_y=flr(x/8),flr(y/8)
 local dir_x,dir_y=(
 (x2/8)-(x/8)
 ),(y2/8)-(y/8)

 local hyp=sqrt((dir_x*dir_x)+(dir_y*dir_y))
 dir_x/=hyp
 dir_y/=hyp

 local step_x,step_y=abs(1/dir_x),abs(1/dir_y)
 local ray_dir_x,ray_dir_y=0,0
 local len_x,len_y=0,0

 if dir_x<0 then
  ray_dir_x=-1
  len_x=(start_x-check_x)*step_x
 else
  ray_dir_x=1
  len_x=((check_x+1)-start_x)*step_x
 end

 if dir_y<0 then
  ray_dir_y=-1
  len_y=(start_y-check_y)*step_y
 else
  ray_dir_y=1
  len_y=((check_y+1)-start_y)*step_y

 end
 tile=false
 local fdis=100.0
 godis=0

 while not tile and godis<fdis do
  if len_x<len_y then
   check_x+=ray_dir_x
   godis=len_x
   len_x+=step_x
  else
   check_y+=ray_dir_y
   godis=len_y
   len_y+=step_y
  end

  if mget(check_x,check_y)==1 then
   tile=true
  end

  if  (check_x==flr(x2/8)
  and check_y==flr(y2/8)
  ) then
  break
  end

 end

 return dir_x,dir_y
end 

Cart #kujiwaduma-0 | 2024-02-25 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
2



[Please log in to post a comment]