Not totally. There are still issues on certain angles.
Code: Select all
'2d Quad-Shadow caster
'by Ezekiel Gutierrez and Leopardpm
'
'requires light.bmpx 400x400 for the glow
'
function getslope(byref xdif as integer,byref ydif as integer, _
x1 as integer,y1 as integer, _
x2 as integer,y2 as integer) as integer
xdif = x2 - x1
ydif = y2 - y1
return atan2(abs(ydif), abs(xdif))*(180 / 3.14)
end function
sub makepoint(xdif as integer,ydif as integer,dist as integer, _
x1 as integer,y1 as integer, _
byref x2 as integer,byref y2 as integer)
xdif=xdif'*70'massively in need of optimization
ydif=ydif'*70'massively in need of optimization
x2=x1+(ydif) -(ydif+xdif)
y2=y1-(xdif) -(ydif-xdif)
end sub
screen 18,32
dim as integer x,y,x2,y2,xl,yl, _
shx1,shy1,shx2,shy2,dist,xdif,ydif
dim as integer slope1,slope2,slope3,slope4
x=100 'box
y=100 ' co-ordinate 1
x2=110'box
y2=110' co-ordinate 2
'create light glow from 32 bit bitmap
dim as any ptr light
light=imagecreate(400,400)
bload "light.bmpx",light
'====================================
color rgb(0,0,0),rgb(255,255,255)
dim as any ptr img
img=imagecreate(640,480,rgba(0,0,0,0))'create temporary image array
do
screenlock
cls
getmouse xl,yl'put light at mouse
line img,(0,0)-(640,480),rgba(0,0,0,0),bf
dim as integer chx1,chy1,chx2,chy2,testval
'calculate differences for each point
slope1=getslope(xdif,ydif,x2,y,xl,yl)
slope2=getslope(xdif,ydif,x2,y2,xl,yl)
slope3=getslope(xdif,ydif,x,y,xl,yl)
slope4=getslope(xdif,ydif,x,y2,xl,yl)
'find two edge points for the shadow caster
'===================================================
if slope1>testval then testval=slope1:chx1=x2:chy1=y :chx2=x:chy2=y2
if slope2>testval then testval=slope2:chx1=x2:chy1=y2 :chx2=x:chy2=y
if slope3>testval then testval=slope3:chx1=x:chy1=y :chx2=x2:chy2=y2
if slope4>testval then testval=slope4:chx1=x:chy1=y2 :chx2=x2:chy2=y
'===================================================
put img,(xl-200,yl-200),light,alpha' put light into it
getslope(xdif,ydif,chx1,chy1,xl,yl)'get slope difference for one edge of the shadow
makepoint(xdif,ydif,30,chx1,chy1,shx1,shy1)'create first point
getslope(xdif,ydif,chx2,chy2,xl,yl)'get slope difference for the other edge
makepoint(xdif,ydif,30,chx2,chy2,shx2,shy2)'create second point
'create shadow block
line img,(shx1,shy1)-(chx1,chy1),rgba(0,0,0,0)
line img,(chx1,chy1)-(chx2,chy2),rgba(0,0,0,0)
line img,(shx2,shy2)-(chx2,chy2),rgba(0,0,0,0)
line img,(shx1,shy1)-(shx2,shy2),rgba(0,0,0,0)
'paint img,(chx2+1,chy1+1),rgba(0,0,0,0),rgba(0,0,0,0)
paint img,(shx1+(chx2-shx1),shy1+(chy1-shy1)),rgba(0,0,0,0),rgba(0,0,0,0)
'draw glow
put (0,0),img,alpha
'draw shadow casting box
'line (x,y)-(x2,y2),rgb(0,0,0),bf
pset (shx1+(chx2-shx1),shy1+(chy1-shy1)),rgb(0,0,0)
print slope1;slope2;slope3;slope4
screenunlock
sleep 1
loop until multikey(1)