Ooops
November 28, 2020, 01:24:24 AM

### Author Topic: [bb] Ray intersect by GrahamK [ 1+ years ago ]  (Read 604 times)

#### BlitzBot

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

Description : Calculates the intersection between a triangle and a ray(vector) in 3d space.

Similar idea to Elias_t's intersect DLL, but in blitz native code. Not sure of the speed difference between the two approaches.

Code :
Code: BlitzBasic
1. ; a vector type
2. Type vector3D
3.         Field dx#,dy#,dz#
4. End Type
5.
6. ; create a new vector object
7. Function vector3D_Create.vector3D(x#,y#,z#)
8.         v.vector3d = New vector3d
9.         vector3d_set(v,x,y,z)
10.         Return v
11. End Function
12.
13. ; set a vector object to a set of values
14. Function vector3D_Set(v1.vector3d,x#,y#,z#)
15.         v1dx# = x#
16.         v1dy# = y#
17.         v1dz# = z#
18. End Function
19.
20.
21. Const SAME_CLOCKNESS = 1
22. Const DIFF_CLOCKNESS = 0
23. Function vector3D_CheckSameClockDir(pt1.vector3d, pt2.vector3d,pt3.vector3d,norm.vector3d)
24.    ; calculate normal
25.    test.vector3d = vector3d_trinormal(pt1,pt2,pt3)
26.    dotprod# = testdx*normdx + testdy*normdy + testdz*normdz
27.    Delete test
28.
29.    If(dotprod < 0) Then
30.         Return DIFF_CLOCKNESS;
31.    Else
32.         Return SAME_CLOCKNESS;
33.   End If
34. End Function
35.
36.
37. ; calculate the normal of a triangle
38. Function Vector3D_TriNormal.Vector3d(v1.Vector3d,v2.Vector3d,v3.Vector3d)
39.         ; calculate differences
40.         ux#= v3dx#- v2dx#
41.         uy#= v3dy#- v2dy#
42.         uz#= v3dz#- v2dz#
43.     vx#= v1dx#- v2dx#
44.     vy#= v1dy#- v2dy#
45.         vz#= v1dz#- v2dz#
46.
47.         ; calculate vector
48.         n.Vector3D = vector3d_create((uy#*vz#)-(vy#*uz#),(uz#*vx#)-(vz#*ux#),(ux#*vy#)-(vx#*uy#)); New Vector3D
49.
50.     mag# = Sqr#(ndx#^2+ndy#^2+ndz#^2)
51.
52.         ; create normal by scaling by 1/magnitude
53.         If mag#<>0 Then
54.                 vector3d_set(n,ndx#/mag#,ndy#/mag#,ndz#/mag#)
55.         Else
56.                 vector3d_set(n,0,0,1) ; to remove and div 0 errors
57.         End If
58.
59.         Return n
60. End Function
61.
62.
63. ; check if a ray from a given point intersects a triangle.
64. ; Only considers an intersect if the triangle is 'facing' the point, ie. only intersects with the front of the triangle
65. Function vector3d_LineTriIntersect(pt1.vector3d, pt2.vector3d, pt3.vector3d, linept.vector3d, vect.vector3d, pt_int.vector3d)
66.
67.    ; vector form triangle pt1 To pt2
68.    V1x# = pt2dx - pt1dx
69.    V1y# = pt2dy - pt1dy
70.    V1z# = pt2dz - pt1dz
71.
72.    ; vector form triangle pt2 To pt3
73.    V2x# = pt3dx - pt2dx
74.    V2y# = pt3dy - pt2dy
75.    V2z# = pt3dz - pt2dz
76.
77.    ; vector normal of triangle
78.    norm.vector3d = vector3d_trinormal(pt1,pt2,pt3) ;vector3d_create(V1y*V2z-V1z*V2y,V1z*V2x-V1x*V2z,V1x*V2y-V1y*V2x)
79.
80.    ; dot product of normal And Line's vector If zero Line is parallel To triangle
81.    dotprod# = normdx*vectdx + normdy*vectdy + normdz*vectdz;
82.
83.    If(dotprod < 0)
84.       ;Find point of intersect To triangle plane.
85.       ;find t To intersect point
86.       t1# = -(normdx*(lineptdx-pt1dx)+normdy*(lineptdy-pt1dy)+normdz*(lineptdz-pt1dz))
87.           t2# =  (normdx*vectdx+normdy*vectdy+normdz*vectdz)
88.
89.           t#=t1/t2
90.       ; If ds is neg Line started past triangle so can't hit triangle.
91.       If(t < 0) Then Delete norm:Return 0
92.
93.           vector3d_set(pt_int,lineptdx + vectdx*t,lineptdy + vectdy*t,lineptdz + vectdz*t)
94.
95.       If(vector3D_CheckSameClockDir(pt1, pt2, pt_int, norm) = SAME_CLOCKNESS) Then
96.          If(vector3D_CheckSameClockDir(pt2, pt3, pt_int, norm) = SAME_CLOCKNESS) Then
97.             If(vector3D_CheckSameClockDir(pt3, pt1, pt_int, norm) = SAME_CLOCKNESS) Then
98.                            Delete norm
99.                Return 1;
100.             End If
101.          End If
102.       End If
103.    End If
104.    Delete norm
105.    Return 0
106. End Function
107.
108.
109.
110. ; example code below... function has not been heavily tested as yet
111.
112. p1.vector3d = vector3d_create(-1,1,-1)
113. p2.vector3d = vector3d_create( 1,1,-1)
114. p3.vector3d = vector3d_create( 0,1, 1)
115.
116. linept.vector3d = vector3d_create(0,0,0)
117. vect.vector3d = vector3d_create(0,10,1)
118.
119. pt_int.vector3d = vector3d_create(0,0,0)
120.
121.
122. ci = vector3d_LineTriIntersect(p1,p2,p3,linept,vect,pt_int)
123. If ci Then
124.         DebugLog "Intersected at x:"+pt_intdx+" y:"+pt_intdy+" z:"+pt_intdz
125. Else
126.         DebugLog "Does not intersect"
127. End If
128.
129. DebugLog "---------------------------------------------"
130.
131.
132. vector3d_set(vect,0,10,20)
133. ci = vector3d_LineTriIntersect(p1,p2,p3,linept,vect,pt_int)
134. If ci Then
135.         DebugLog "Intersected at x:"+pt_intdx+" y:"+pt_intdy+" z:"+pt_intdz
136. Else
137.         DebugLog "Does not intersect"
138. End If
139.
140.
141.
142. While Not KeyDown(1)
143. Wend
144.
145. Delete Each vector3d
146.
147. End