Ooops
January 20, 2021, 12:38:39 PM

### Author Topic: [bb] Ray Box Intersection by elias_t [ 1+ years ago ]  (Read 1346 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Ray Box Intersection by elias_t [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Ray Box Intersection
Author : elias_t
Posted : 1+ years ago

Description : Fast ray box intersection function.

Code :
Code: BlitzBasic
1. ;Ray box intersection function:
2. ;
3. ;rox,roy,roz = ray start point
4. ;rdx,rdy,rdz = ray destination point
5. ;
6. ;bminx,bminy,bminz = box min corner
7. ;bmaxx,bmaxy,bmaxz = box max corner
8.
9. Function raybox(rox#,roy#,roz#, rdx#,rdy#,rdz#, bminx#,bminy#,bminz#, bmaxx#,bmaxy#,bmaxz#)
10.
11. Local txmin#,txmax#,tymin#,tymax#
12.
13. Local ddx#=1.0/(rox-rdx)
14. Local ddy#=1.0/(roy-rdy)
15.
16. If ddx >= 0
17. txmin = (bminx - rox) * ddx
18. txmax = (bmaxx - rox) * ddx
19. Else
20. txmin = (bmaxx - rox) * ddx
21. txmax = (bminx - rox) * ddx
22. EndIf
23.
24. If ddy >= 0
25. tymin = (bminy - roy) * ddy
26. tymax = (bmaxy - roy) * ddy
27. Else
28. tymin = (bmaxy - roy) * ddy
29. tymax = (bminy - roy) * ddy
30. EndIf
31.
32. If ( (txmin > tymax) Or (tymin > txmax) ) Return 0
33.
34. If (tymin > txmin) txmin = tymin
35. If (tymax < txmax) txmax = tymax
36.
37. Local tzmin#,tzmax#
38. Local ddz#=1.0/(roz-rdz)
39.
40. If ddz >= 0
41. tzmin = (bminz - roz) * ddz
42. tzmax = (bmaxz - roz) * ddz
43. Else
44. tzmin = (bmaxz - roz) * ddz
45. tzmax = (bminz - roz) * ddz
46. EndIf
47.
48. If (txmin > tzmax) Or (tzmin > txmax) Return 0
49.
50. Return 1
51.
52. End Function
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65. ;EXAMPLE
66.
67. Graphics3D 640,480,32,2
68.
69. cam=CreateCamera()
70. PositionEntity cam,0,5,-10
71. li=CreateLight()
72.
73. ;start and end point of line segment
74. p1x#=8:p1y#=10:p1z#=-10
75. p2x#=-8:p2y#=-5:p2z#=45
76.
77.
78. ;define the bounding box
79. bx#=1 ; box x min
80. by#=2 ; box y min
81. bz#=3 ; box z min
82.
83. bw#=3 ; box width
84. bh#=4 ; box height
85. bd#=5 ; box depth
86.
87. bxm#=bx+bw ; box x max
88. bym#=by+bh ; box y max
89. bzm#=bz+bd ; box z max
90.
91.
92. box=CreateCube()
93. EntityColor box,0,255,100
94. EntityFX box,16
95. EntityAlpha box,.7
96. FitMesh box, bx,by,bz, bw,bh,bd
97.
98.
99. ;visual ray
100. m=CreateMesh()
101. s=CreateSurface(m)
102. v0=AddVertex(s,v0x,v0y,v0z)
103. v1=AddVertex(s,v1x,v1y,v1z)
104. v2=AddVertex(s,v2x,v2y,v2z)
105. AddTriangle (s,v0,v2,v1)
106. EntityColor m,255,255,0
107. EntityFX m,16
108. VertexCoords(s,0,p1x+.15,p1y,p1z)
109. VertexCoords(s,1,p2x,p2y,p2z)
110. VertexCoords(s,2,p1x-.15,p1y,p1z)
111. UpdateNormals m
112.
113.
114.
115. PositionEntity cam,10,15,-10
116. PointEntity cam ,m
117.
118.
119. While Not KeyHit(1)
120.
121. RenderWorld()
122.
123. If KeyDown(200) Then VertexCoords(s,1,VertexX(s,1),VertexY(s,1)+.1,VertexZ(s,1))
124. If KeyDown(208) Then VertexCoords(s,1,VertexX(s,1),VertexY(s,1)-.1,VertexZ(s,1))
125.
126. If KeyDown(203) Then VertexCoords(s,1,VertexX(s,1)-.1,VertexY(s,1),VertexZ(s,1))
127. If KeyDown(205) Then VertexCoords(s,1,VertexX(s,1)+.1,VertexY(s,1),VertexZ(s,1))
128.
129. If KeyDown(201) Then VertexCoords(s,1,VertexX(s,1),VertexY(s,1),VertexZ(s,1)+.1)
130. If KeyDown(209) Then VertexCoords(s,1,VertexX(s,1),VertexY(s,1),VertexZ(s,1)-.1)
131.
132. dx#=VertexX(s,1)
133. dy#=VertexY(s,1)
134. dz#=VertexZ(s,1)
135.
136. res=raybox(p1x,p1y,p1z, dx,dy,dz, bx,by,bz, bxm,bym,bzm)
137.
138. Text 0,0,"Arrow Keys = Move ray Destination"
139. Text 10,10,res
140.
141. Flip
142.
143. Wend
144.
145.
146. End

Comments :

TartanTangerine (was Indiepath)(Posted 1+ years ago)

Very good. I think I will try to use this very soon.

Olive(Posted 1+ years ago)

very good, thanks for sharing :)btw, 3 lines missing : Text 0,0,resFlip FalseWend

eBusiness(Posted 1+ years ago)

Editing should work.

CyberHeater(Posted 1+ years ago)

Try adding RenderWorldText 0,0,"res = " + resFlipWendto the end of it.

elias_t(Posted 1+ years ago)

Oi.Sorry. ctrl+A did'nt worked...Updated now.

bytecode77(Posted 1+ years ago)

great code! especialy useful for raytracing apps. but useless without intersection point coordinates return...

SimplePortal 2.3.6 © 2008-2014, SimplePortal