VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Collision Function

by Nicholas Forystek (14 Submissions)
Category: Games
Compatability: VB 6.0
Difficulty: Advanced
Date Added: Mon 10th February 2020
Rating: (0 Votes)

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).

Rate Collision Function

'**************************************
' 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

Download this snippet    Add to My Saved Code

Collision Function Comments

No comments have been posted about Collision Function. Why not be the first to post a comment about Collision Function.

Post your comment

Subject:
Message:
0/1000 characters