VB icon

Collision Function

Email
Submitted on: 6/2/2018 5:51:55 PM
By: Nicholas Forystek  
Level: Advanced
User Rating: Unrated
Compatibility: VB 6.0
Views: 661
author picture
 
     This is a single function from a collision library I put together that I’ve been recreating from code absolutely missing for years. Among other functions the focus of this code is about the first function below (there is a plethora of other math functions for vectors). This particular function takes in three vector or vertex sized argument inputs, where as you would usually see four, one for the point and one for each point in the triangle. The inputs form left to right is the points X, Y, Z then the three lengths of the triangles sides, and lastly the triangles normal. The center should be extracted from the point before you send it as an argument, the center need not be known by the function then, and just the normal (not vertex or coordinate normal, the triangle normal).
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Collision Function
' Description:This is a single function from a collision library I put together that I’ve been recreating from code absolutely missing for years. Among other functions the focus of this code is about the first function below (there is a plethora of other math functions for vectors). This particular function takes in three vector or vertex sized argument inputs, where as you would usually see four, one for the point and one for each point in the triangle. The inputs form left to right is the points X, Y, Z then the three lengths of the triangles sides, and lastly the triangles normal. The center should be extracted from the point before you send it as an argument, the center need not be known by the function then, and just the normal (not vertex or coordinate normal, the triangle normal).
' By: Nicholas Forystek
'**************************************

Public Type Vector3D
x As Double
y As Double
z As Double
End Type
Function PointHitFace(ByVal PX As Single, ByVal PY As Single, ByVal Pz As Single, ByVal NX As Single, ByVal NY As Single, ByVal Nz As Single, ByVal vx As Single, ByVal vy As Single, ByVal vz As Single) As Boolean
'assumes a zero coordinate centered for the triangle
'so the point should already have it subtracted which
'allows us just three sets of arguments instead of 4
'input px,py,pz = the point to test collision with a triangle
'input nx,ny,nz = numeric len of each side in the triangle
'input vx,vy,vz = the triangle normal, (not vertex normal)
PX = Abs(PX)
PY = Abs(PY)
Pz = Abs(Pz)
Dim a1 As Single
Dim a2 As Single
Dim a3 As Single
a1 = (Sqr((((PX + PY + Pz) * (vy + vz)) - (PX * vx)) * ((NX + NX + NX) * (vy + vz)) + _
 (((PX + PY + Pz) * (vz + vx)) - (PX * vy)) * ((NX + NX + NX) * (vz + vx)) + _
 (((PX + PY + Pz) * (vx + vy)) - (PX * vz)) * ((NX + NX + NX) * (vx + vy))) * vx)
a2 = (Sqr((((PY + Pz + PX) * (vz + vx)) - (PY * vy)) * ((NY + NY + NY) * (vz + vx)) + _
 (((PY + Pz + PX) * (vx + vy)) - (PY * vz)) * ((NY + NY + NY) * (vx + vy)) + _
 (((PY + Pz + PX) * (vy + vz)) - (PY * vx)) * ((NY + NY + NY) * (vy + vz))) * vy)
a3 = (Sqr((((Pz + PX + PY) * (vx + vy)) - (Pz * vz)) * ((Nz + Nz + Nz) * (vx + vy)) + _
 (((Pz + PX + PY) * (vy + vz)) - (Pz * vx)) * ((Nz + Nz + Nz) * (vy + vz)) + _
 (((Pz + PX + PY) * (vz + vx)) - (Pz * vy)) * ((Nz + Nz + Nz) * (vz + vx))) * vz)
PointHitFace = ((a1 + a2 + a3) > 0)
End Function
Sub Main()
Randomize
Dim PLX(0 To 2) As Single
Dim PLY(0 To 2) As Single
Dim PLZ(0 To 2) As Single
Dim PX As Single
Dim PY As Single
Dim Pz As Single
Dim p1 As Vector3D
Dim n1 As Vector3D
Dim v1 As Vector3D
Do While True
DoEvents
PX = RandomPositive(1, 20) + -RandomPositive(1, 30)
PY = RandomPositive(1, 20) + -RandomPositive(1, 30)
Pz = RandomPositive(1, 20) + -RandomPositive(1, 30)
PLX(0) = RandomPositive(1, 10) + -RandomPositive(1, 50)
PLY(0) = RandomPositive(1, 10) + -RandomPositive(1, 50)
PLZ(0) = RandomPositive(1, 10) + -RandomPositive(1, 50)
PLX(1) = RandomPositive(1, 10) + -RandomPositive(1, 50)
PLY(1) = RandomPositive(1, 10) + -RandomPositive(1, 50)
PLZ(1) = RandomPositive(1, 10) + -RandomPositive(1, 50)
PLX(2) = RandomPositive(1, 10) + -RandomPositive(1, 50)
PLY(2) = RandomPositive(1, 10) + -RandomPositive(1, 50)
PLZ(2) = RandomPositive(1, 10) + -RandomPositive(1, 50)
v1 = TriangleCenter(mv(PLX(0), PLY(0), PLZ(0)), mv(PLX(1), PLY(1), PLZ(1)), mv(PLX(2), PLY(2), PLZ(2)))
n1.x = Distance(mv(PLX(0), PLY(0), PLZ(0)), mv(PLX(1), PLY(1), PLZ(1)))
n1.y = Distance(mv(PLX(1), PLY(1), PLZ(1)), mv(PLX(2), PLY(2), PLZ(2)))
n1.z = Distance(mv(PLX(2), PLY(2), PLZ(2)), mv(PLX(0), PLY(0), PLZ(0)))
p1 = VectorSubtract(mv(PX, PY, Pz), v1)
v1 = TriangleNormal(mv(PLX(0), PLY(0), PLZ(0)), mv(PLX(1), PLY(1), PLZ(1)), mv(PLX(2), PLY(2), PLZ(2)))
pbp2 = PointHitFace(p1.x, p1.y, p1.z, n1.x, n1.y, n1.z, v1.x, v1.y, v1.z)
Debug.Print Padding(CStr(pbp1)) & " " & Padding(CStr(pbp2))
test = test + 1
Loop
End Sub
Public Function mv(ByRef x As Single, ByRef y As Single, ByRef z As Single) As Vector3D
mv.x = x
mv.y = y
mv.z = z
End Function
Function RandomPositive(Lowerbound As Long, Upperbound As Long) As Single
RandomPositive = CLng(Round((Upperbound - Lowerbound + 1) * Rnd, 0) + Lowerbound)
End Function
Public Function Padding(ByVal Value As String) As String
Static maxSize As Long
If maxSize = 0 Then maxSize = Len(CStr(Value))
Padding = String(Abs((maxSize * Len(" ")) - (Len(Value) \ Len(" "))), " ") & Value
If Len(CStr(Padding)) > maxSize Then maxSize = Len(CStr(Padding))
End Function
Function Distance(ByRef p1 As Vector3D, ByRef p2 As Vector3D) As Single
Distance = Sqr(((p1.x - p2.x) * (p1.x - p2.x)) + ((p1.y - p2.y) * (p1.y - p2.y)) + ((p1.z - p2.z) * (p1.z - p2.z)))
End Function
Function TriangleNormal(ByRef v0 As Vector3D, ByRef v1 As Vector3D, ByRef v2 As Vector3D) As Vector3D
TriangleNormal = VectorCrossProduct(VectorSubtract(v0, v1), VectorSubtract(v1, v2))
End Function
Function VectorCrossProduct(ByRef v As Vector3D, ByRef u As Vector3D) As Vector3D
VectorCrossProduct.x = ((v.y * u.z) - (v.z * u.y))
VectorCrossProduct.y = ((v.z * u.x) - (v.x * u.z))
VectorCrossProduct.z = ((v.x * u.y) - (v.y * u.x))
End Function
Function VectorSubtract(ByRef v As Vector3D, ByRef u As Vector3D) As Vector3D
VectorSubtract.x = (v.x - u.x)
VectorSubtract.y = (v.y - u.y)
VectorSubtract.z = (v.z - u.z)
End Function
Function TriangleCenter(ByRef v0 As Vector3D, ByRef v1 As Vector3D, ByRef v2 As Vector3D) As Vector3D
Dim vR As Vector3D
 
vR.x = (v0.x + v1.x + v2.x) / 3
vR.y = (v0.y + v1.y + v2.y) / 3
vR.z = (v0.z + v1.z + v2.z) / 3
TriangleCenter = vR
End Function


Other 18 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.