
move line across the box

click mouse to set new ray position

simple test program to see the working of a 2d and 3d
ray box intersections where right
to make it work in 3d just give all the Z Coordinates some values
copy code below into DBP
#Constant INFINITY 99999999.9
// 3d point / vector
Type tVec
x as float
y as float
z as float
EndType
// 3d line / ray
Type tRay
x as float
y as float
z as float
u as float
v as float
w as float
EndType
// 3d box / cube
Type tBox
x1 as float
y1 as float
z1 as float
x2 as float
y2 as float
z2 as float
co as integer
EndType
// holds the in / out intersection points
Type tInfo
tmin as float
tmax as float
EndType
// global intersection info as cant return 2 params from a func
Global t_data as tInfo
//3d line / ray
cray as tRay
// axis aligned box / cube
aabb as tBox
// define a 2d box / cube
aabb.x1 = 128
aabb.y1 = 64
aabb.z1 = 0
aabb.x2 = aabb.x1 + 164
aabb.y2 = aabb.y1 + 164
aabb.z2 = 0
// starting point for the line / ray
cray.x = 0
cray.y = 0
cray.z = 0
// set up display
sync on
sync rate 0
set display mode 480,480,32
// main
repeat
// get mouse coords
mx = mousex()
my = mousey()
// set new line / ray starting point
if mouseclick()
cray.x = mx
cray.y = my
endif
//
cls 0x0
// show the box / cube visually
DrawBox( aabb.x1 , aabb.y1 , aabb.x2 , aabb.y2 , rgb(255,255,255) )
// show the line / ray visually
line cray.x , cray.y , mx , my
// get new direction of line / ray if mouse was clicked
dx# = mx - cray.x
dy# = my - cray.y
// get inverse length
di# = 1. / sqrt( dx# * dx# + dy# * dy# )
// set and normalize line / ray's direction
cray.u = dx# * di#
cray.v = dy# * di#
cray.w = 0
// check if line / box intersected
if RayBoxIntersect( cray , aabb ) = 1
// enter hit
cx# = cray.x + t_data.tmin * cray.u
cy# = cray.y + t_data.tmin * cray.v
ink rgb(255,255,0),0x0
circle cx#,cy#,8
// exit hit
qx# = cray.x + t_data.tmax * cray.u
qy# = cray.y + t_data.tmax * cray.v
ink rgb(255,255,0),0x0
circle qx#,qy#,8
// now a simple 2d ray march
stp = 10
sx# = (cx# - qx#)/(stp-1)
sy# = (cy# - qy#)/(stp-1)
rx# = qx#
ry# = qy#
for i = 0 to stp -1
ink rgb(55,255,255),0x0
circle rx#,ry#,4
rx# = rx# + sx#
ry# = ry# + sy#
next
//
endif
text 10 , 260 , "done."
sync
until escapekey()
end
Function DrawBox(x1,y1,x2,y2,pc)
ink pc,0x0
line x1,y1,x2,y1
line x2,y1,x2,y2
line x2,y2,x1,y2
line x1,y1,x1,y2
EndFunction
Function RayBoxIntersect( ray as tRay , aabb as tBox )
local t1 as float
local t2 as float
local tp as float
local tu as float
local tv as float
local tw as float
t_data.tmin = -INFINITY
t_data.tmax = INFINITY
tu = 1. / ray.u
tv = 1. / ray.v
tw = 1. / ray.w
if tu = 0
if ray.x < aabb.x1 or ray.x > aabb.x2 then exitfunction -1
else
t1 = ( aabb.x1 - ray.x ) * tu
t2 = ( aabb.x2 - ray.x ) * tu
if t1 > t2
tm = t1
t1 = t2
t2 = tm
endif
if t1 > t_data.tmin then t_data.tmin = t1
if t2 < t_data.tmax then t_data.tmax = t2
if t_data.tmin > t_data.tmax then exitfunction -1
if t_data.tmax < 0 then exitfunction -1
endif
if tv = 0
if ray.y < aabb.y1 or ray.y > aabb.y2 then exitfunction -1
else
t1 = ( aabb.y1 - ray.y ) * tv
t2 = ( aabb.y2 - ray.y ) * tv
if t1 > t2
tm = t1
t1 = t2
t2 = tm
endif
if t1 > t_data.tmin then t_data.tmin = t1
if t2 < t_data.tmax then t_data.tmax = t2
if t_data.tmin > t_data.tmax then exitfunction -1
if t_data.tmax < 0 then exitfunction -1
endif
if tw = 0
if ray.z < aabb.z1 or ray.z > aabb.z2 then exitfunction -1
else
t1 = ( aabb.z1 - ray.z ) * tw
t2 = ( aabb.z2 - ray.z ) * tw
if t1 > t2
tm = t1
t1 = t2
t2 = tm
endif
if t1 > t_data.tmin then t_data.tmin = t1
if t2 < t_data.tmax then t_data.tmax = t2
if t_data.tmin > t_data.tmax then exitfunction -1
if t_data.tmax < 0 then exitfunction -1
endif
EndFunction 1
hexeg0n