November 25, 2020, 07:28:06 AM

Author Topic: [bb] Ray-&gt;Sphere Intersection by elias_t [ 1+ years ago ]  (Read 600 times)

BlitzBot

• Jr. Member
• Posts: 1
[bb] Ray-&gt;Sphere Intersection by elias_t [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Ray->Sphere Intersection
Author : elias_t
Posted : 1+ years ago

Description : A ray to sphere intersection function.
Returns the ingoing and outgoing intersection points
of an infinite ray and a sphere.

Code :
Code: BlitzBasic
1. ;infinite ray to sphere intersection example.
2. ;The intersection points are returned.
3.
4. Dim picked1#(2);1st intersection point
5. Dim picked2#(2);2nd intersection point [optional]
6.
7.
8. ; x1,y1,z1    : 1st point of segment
9. ; x2,y2,z2    : 2nd point of segment
10. ; x3,y3,z3, r : coordinates And radius of sphere)
11.
12. Function sphere_line_intersection (x1#,y1#,z1#, x2#,y2#,z2#, x3#,y3#,z3#,r#)
13.
14. Local mu#
15.
16. Local dx1#=(x2-x1)
17. Local dy1#=(y2-y1)
18. Local dz1#=(z2-z1)
19.
20. Local dx2#=(x1-x3)
21. Local dy2#=(y1-y3)
22. Local dz2#=(z1-z3)
23.
24.
25. Local a# = dx1*dx1 + dy1*dy1 + dz1*dz1
26. Local b# = 2*( dx1*dx2 + dy1*dy2 + dz1*dz2 )
27. Local c# = (x3*x3)+(y3*y3)+(z3*z3)+(x1*x1)+(y1*y1)+(z1*z1)-2*(x3*x1+y3*y1+z3*z1)-(r*r)
28.
29.
30. Local bm#=(b * b)
31. Local da#=2*a
32. Local fac#=(4 * a * c)
33.
34. Local i#=bm - fac
35.
36. Local sqi#=Sqr(i)
37.
38.
39.
40. ;if segment crosses the sphere
41. If i>=0
42.
43.   ;incoming intersection
44.   mu = (-b - sqi) / da
45.   picked1(0) = x1 + mu*dx1
46.   picked1(1) = y1 + mu*dy1
47.   picked1(2) = z1 + mu*dz1
48.
49.   ;
50.   ;outgoing intersection
51.   ;remove this if you only need the first intersection point
52.   mu = (-b + sqi) / da
53.   picked2(0) = x1 + mu*dx1
54.   picked2(1) = y1 + mu*dy1
55.   picked2(2) = z1 + mu*dz1
56.   ;
57.
58. Return 1
59. EndIf
60.
61.
62.
63. End Function
64.
65.
66.
67.
68.
69.
70.
71. ;************************************************************************
72. ;EXAMPLE
73.
74.
75.
76.
77. Graphics3D 640,480,0,2
78.
79. cam=CreateCamera()
80. MoveEntity cam,0,3,-5
81. light=CreateLight()
82.
83.
84. ;------------------------------
85. ;create a "ray like triangle" near the camera's position 0,0,-5
86. ray=CreateMesh()
87. rs=CreateSurface(ray)
92. EntityColor ray,255,255,0
93. UpdateNormals ray
94. EntityFX ray,16
95. ;------------------------------
96.
97.
98. ;------------------------------
100. ob1=CreateSphere()
101. EntityColor ob1,255,0,0
102. EntityAlpha ob1,.75
103. EntityFX ob1,16
104.
105. ;create dummy objects
106. d1=CreateCube()
107. ScaleEntity d1,.05,.05,.05
108. EntityColor d1,0,0,255
109. d2=CreateCube()
110. ScaleEntity d2,.05,.05,.05
111. EntityColor d2,0,255,0
112. ;------------------------------
113.
114.
115. ;variables to control the ray
116. rx#=0
117. ry#=0
118. rz#=10
119.
120.
121. PointEntity cam,ob1
122.
123.
124. While Not KeyHit(1)
125. RenderWorld()
126.
127. ;move the ray
128. If KeyDown(200) ry=ry+.1 VertexCoords rs,v1,rx,ry,rz
129. If KeyDown(208) ry=ry-.1 VertexCoords rs,v1,rx,ry,rz
130. If KeyDown(203) rx=rx-.1 VertexCoords rs,v1,rx,ry,rz
131. If KeyDown(205) rx=rx+.1 VertexCoords rs,v1,rx,ry,rz
132.
133. ;x,y,z=1st point ,rx,ry,rz=2nd point ,sx,sy,sz,r=sphere pos and radius r
134. If sphere_line_intersection (0,0,-5, rx,ry,rz, 0,0,0,1)
135. PositionEntity d1,picked1(0),picked1(1),picked1(2)
136. PositionEntity d2,picked2(0),picked2(1),picked2(2)
137. EndIf
138.
139. Text 200,0,"[ Cursor keys change ray direction]"
140. Flip
141. Wend
142. ClearWorld()
143. End