News:

Function Finder  Find all the functions within source code files

Main Menu

Ray-Hit-Box Example

Started by kevin, January 10, 2025, 09:28:59 PM

Previous topic - Next topic

kevin


  Ray-Hit-Box   Example

   This function is written as visual aid to try and help people understand the process of doing a ray intersection  (line intersection)  with a box.   I made it for use in a video about Wolf 3D rendering concepts.  Primarily what ray intersections give us when using  RayIntersectWorld functions build in PlayBASIC. 

   


   
PlayBASIC Code: [Select]
; PROJECT : Ray-Hit-Box
; AUTHOR :
; CREATED : 11/01/2025
; EDITED : 11/01/2025
; ---------------------------------------------------------------------

setfps 30

Ray_X1=rnd(getScreenWidth())
Ray_Y1=rnd(getScreenHEight())

Box_X1 = 200
Box_Y1 = 200
Box_X2 = 600
Box_Y2 = 400


Do
cls

if MouseButton()
Ray_X1=rnd(getScreenWidth())
Ray_Y1=rnd(getScreenHEight())
endif

Ray_X2=mousex()
Ray_Y2=mousey()

Ray_Hit_Box(Ray_X1,Ray_Y1,Ray_X2,Ray_Y2, Box_X1,Box_Y1 ,Box_X2,Box_Y2)

Sync
loop spacekey()




Function Ray_Hit_Box(Ray_X1,Ray_Y1,Ray_X2,Ray_Y2, Box_X1,Box_Y1 ,Box_X2,Box_Y2)

// Array to hold possible impacts
Dim Impacts(100)


// Check the line with the top edge of the
State=LinesIntersect(Ray_X1,Ray_Y1,Ray_X2,Ray_Y2, Box_X1,Box_Y1 ,Box_X2,Box_Y1)
if State
Impacts(Index) =GetIntersectX#(0)
Impacts(Index+1) =GetIntersectY#(0)
Index+=2
endif


// Bottom
State=LinesIntersect(Ray_X1,Ray_Y1,Ray_X2,Ray_Y2, Box_X1,Box_Y2 ,Box_X2,Box_Y2)

if State
Impacts(Index) =GetIntersectX#(0)
Impacts(Index+1) =GetIntersectY#(0)
Index+=2
endif



// Left edge
State=LinesIntersect(Ray_X1,Ray_Y1,Ray_X2,Ray_Y2, Box_X1,Box_Y1 ,Box_X1,Box_Y2)

if State
Impacts(Index) =GetIntersectX#(0)
Impacts(Index+1) =GetIntersectY#(0)
Index+=2
endif


// Right edge
State=LinesIntersect(Ray_X1,Ray_Y1,Ray_X2,Ray_Y2, Box_X2,Box_Y1 ,Box_X2,Box_Y2)

if State
Impacts(Index) =GetIntersectX#(0)
Impacts(Index+1) =GetIntersectY#(0)
Index+=2
endif

// draw the box
boxc Box_X1,Box_Y1,Box_X2,Box_Y2,true, $203040

// Draw the edges that are facing the RAY
DrawEdge(Ray_X1,Ray_Y1,Ray_X2,Ray_Y2,Box_X1,Box_Y1 ,Box_X2,Box_Y1)
DrawEdge(Ray_X1,Ray_Y1,Ray_X2,Ray_Y2,Box_X2,Box_Y1 ,Box_X2,Box_Y2)
DrawEdge(Ray_X1,Ray_Y1,Ray_X2,Ray_Y2,Box_X2,Box_Y2 ,Box_X1,Box_Y2)
DrawEdge(Ray_X1,Ray_Y1,Ray_X2,Ray_Y2,Box_X1,Box_Y2 ,Box_X1,Box_Y1)


// Draw the ray / Change colour is there's impact
C=rgb(0,255,0)
if Index>0 then C=rgb(255,0,0)
linec Ray_X1,Ray_Y1,Ray_X2,Ray_Y2,c

// draw the impact points
for lp=0 to Index-1 step 2
circlec Impacts(lp),Impacts(lp+1),5,true,$ff0000
next

EndFunction



function DrawEdge(Ray_X1,Ray_Y1,Ray_X2,Ray_Y2,x1,y1,x2,y2)

state=IsPointInFrontOfLine(Ray_X1,Ray_Y1,x1,y1,x2,y2) <0
C= rgb(255,255,255) * state
linec X1,Y1 ,X2,Y2,c
ENdFunction



Function IsPointInFrontOfLine(px, py, x1, y1, x2, y2 )
dx = x2 - x1
dy = y2 - y1
Result = dx * (py - y1) - dy * (px - x1)
EndFunction result






   Point In Front Of Plane /  Point In Front Of LIne