ray-intersect-quad?

Blitz3D Forums/Blitz3D Programming/ray-intersect-quad?

hi!

i'm looking forward to do a raycaster project. i have a question about the ray-intersection function



as you see in the pic, the rays are sent out from the camera in lots of directions. my question is how to calculate the red points (the hitpoints of the ray) on >>2d space<<!

i saw this thread http://www.blitzbasic.com/codearcs/codearcs.php?code=1029 but there was no intersection position return.

im in 9th class right now and we did not have these kind of maths yet !
if anyone could help me, i would be very thankful :)
bye :)

Graphics 1024, 768, 32, 2
SetBuffer BackBuffer()

;Globs
Type Wall
	Field x1#, z1#, x2#, z2#
	Field r, g, b
End Type
Global CamX#, CamZ#, CamYaw#
Global WalkXSpeed#, WalkZSpeed#, WalkRotSpeed#
Global IntersectX#, IntersectY#

For i = 1 To 5
	w.Wall = New Wall
	w\x1# = Rnd(-10, 10)
	w\z1# = Rnd(-10, 10)
	yaw# = Rand(0, 360)
	w\x2# = w\x1# + Sin(yaw#) * 3
	w\z2# = w\z1# + Cos(yaw#) * 3
	w\r = Rand(0, 255)
	w\g = Rand(0, 255)
	w\b = Rand(0, 255)
Next

MoveMouse GraphicsWidth() / 2, GraphicsHeight() / 2
FlushMouse()
FlushKeys()
While Not KeyHit(1)
	Cls
	UpdateMovement()
	Raycast()
	Flip
Wend
End

Function Raycast()
gw = GraphicsWidth()
gh = GraphicsHeight()
gw2 = gw / 2
gh2 = gh / 2
For x = -gw2 To gw2
	dir_x# = Sin(CamYaw# + x * 90.0 / gw) * 1000
	dir_z# = Cos(CamYaw# + x * 90.0 / gw) * 1000
	depth# = 1000
	For w.Wall = Each Wall
		x1# = w\x1#
		y1# = w\z1#
		x2# = w\x2#
		y2# = w\z2#
		x3# = CamX#
		y3# = CamZ#
		x4# = CamX# + dir_x#
		y4# = CamZ# + dir_z#
		
		in = LinesIntersect(x1#,y1#, x2#,y2#, x3#,y3#, x4#,y4#)
		If in Then
			dis# = Sqr((CamX# - IntersectX#) ^ 2 + (CamZ# - IntersectZ#) ^ 2)
			If dis# < depth# Then
				If x = 0 Then Text 0, 0, dis
				depth# = dis#
				h = 1000.0 / dis#
				Color w\r, w\g, w\b
				Rect x + gw2, gh2 - h / 2, 1, h
			EndIf
		EndIf
	Next
Next
End Function


Function UpdateMovement()
s# = .1
If KeyDown(17) Then WalkZSpeed# = WalkZSpeed# + s#
If KeyDown(31) Then WalkZSpeed# = WalkZSpeed# - s#
If KeyDown(30) Then WalkXSpeed# = WalkXSpeed# + s#
If KeyDown(32) Then WalkXSpeed# = WalkXSpeed# - s#
WalkXSpeed# = WalkXSpeed# * .8
WalkZSpeed# = WalkZSpeed# * .8
Slide(CamYaw#, WalkZSpeed#)
Slide(CamYaw# - 90, WalkXSpeed#)

WalkRotSpeed# = (WalkRotSpeed# + MouseXSpeed()) * .5
MoveMouse GraphicsWidth() / 2, GraphicsHeight() / 2
CamYaw# = CamYaw# + WalkRotSpeed#
End Function

Function Slide(yaw#, speed#)
CamX# = CamX# + Sin(yaw#) * speed#
CamZ# = CamZ# + Cos(yaw#) * speed#
End Function

Function LinesIntersect(x1#,y1#, x2#,y2#, x3#,y3#, x4#,y4#)

	numeratorA#  = (x4-x3)*(y1-y3)-(y4-y3)*(x1-x3)
	numeratorB#  = (x2-x1)*(y1-y3)-(y2-y1)*(x1-x3)
	denominator# = (y4-y3)*(x2-x1)-(x4-x3)*(y2-y1)

	If denominator# = 0.0 Then
		Return False
	Else
		Ua# = numeratorA/denominator
		Ub# = numeratorB/denominator
		range1# = Ua >= 0.0 And Ua <= 1.0
		range2# = Ub >= 0.0 And Ub <= 1.0
		If range1 And range2 Then
			IntersectX# = x1 + Ua*(x2-x1)+.5
			IntersectY# = y1 + Ua*(y2-y1)+.5
			Return True
		Else
;			Text(10,20,"No Intersection")
			Return False
		End If
    End If
End Function

(WSDA to move+mouse)

i tried to make a raycaster, but it acts weird. what is it about all this?

how to calculate the red points



http://student.kuleuven.be/~m0216922/CG/raycasting.html#The_Basic_Idea_

i already got one, but the sample which i posted above is not working properly.
anybody an idea?

wow i fixed that bug!
i just mixed up y and z :)
thank you anyway!!

Man you are sooooo clever dude!

IPete2.

oh thank you so much xD