'************************************** ' Name: Collision Math for 3D ' Description:This is two math collision ' functions I have used, along with some t ' ypical vector functions used, that help ' out in a collision calculation for 3D gr ' aphics. They are very basic to the overa ' ll use and one is 2D which is applied ag ' ainst x,y and then y,z for two parts of ' a whole in 3D view. The first function i ' s a normal-less and largest common diame ' ter triangle input, of that which is tes ' ted against a vertex in according to whe ' ther or not the vertex maybe considered ' on the broadside of the triangle or it i ' s somewhere occurring face side in front ' of it.The second function is merely a te ' st against the running polygon normalize ' d plane that if the supplied point falls ' on the interior side of the polygon poin ' t list it returns the ratio between a wh ' ole and actual point averaged impact. Ch ' anging the return data type from the lon ' g to a variant illustrates how the actua ' l location info is available. ' By: Nicholas Forystek ' ' ' Inputs:None ' ' Returns:None ' 'Assumes:None ' 'Side Effects:None '************************************** Public Type Vector3D x As Single y As Single z As Single End Type Public Function VertexPoint_Behind_TrianglePlane_VB( _ ByVal VertexPointX As Single, ByVal VertexPointY As Single, ByVal VertexPointZ As Single, _ ByVal TriangleLength1 As Single, ByVal TriangleLength2 As Single, ByVal TriangleLength3 As Single, _ ByVal TriangleNormalX As Single, ByVal TriangleNormalY As Single, ByVal TriangleNormalZ As Single) As Boolean VertexPoint_Behind_TrianglePlane_VB = ((VertexPointZ * TriangleLength3) + (TriangleLength2 * VertexPointY) + _ (TriangleLength1 * VertexPointX) - ((TriangleLength3 * TriangleNormalX) + _ (TriangleLength1 * TriangleNormalY) + (TriangleLength2 * TriangleNormalZ)) < 0) End Function Public Function TwoDimensionPointInsideOfPointList_VB(ByVal PointXCoord As Single, ByVal PointYCoord As Single, _ PointListsX() As Single, PointListsY() As Single, ByVal PointListsCount As Long) As Long Dim cnt As Long Dim slope1 As Single Dim slope2 As Single If (PointListsCount > 0) Then For cnt = 0 To PointListsCount - 1 slope1 = ((slope1 + Sqr(((PointXCoord - PointListsX(cnt)) ^ 2) + ((PointYCoord - PointListsY(cnt)) ^ 2))) / 2) slope2 = ((slope2 + Sqr(((PointListsX(0) - PointListsX(cnt)) ^ 2) + ((PointListsY(0) - PointListsY(cnt)) ^ 2))) / 2) If (slope1 < slope2) Then If (slope2 <> 0) Then TwoDimensionPointInsideOfPointList_VB = (slope1 / slope2) ElseIf (slope1 <> 0) Then TwoDimensionPointInsideOfPointList_VB = (slope2 / slope1) End If End If Next End If End Function Public Sub Main() 'make a 8x8 sized box Dim PointListsX(0 To 5) As Single Dim PointListsY(0 To 5) As Single PointListsX(0) = 4: PointListsY(0) = -4 PointListsX(1) = 4: PointListsY(1) = 4 PointListsX(2) = -4: PointListsY(2) = 4 PointListsX(3) = -4: PointListsY(3) = -4 PointListsX(4) = 4: PointListsY(4) = -4 Debug.Print "TwoDimensionPointInsideOfPointList_VB: " & _ TwoDimensionPointInsideOfPointList_VB(8, 0, PointListsX, PointListsY, 5) 'try ouside box 'returns 0 Debug.Print "TwoDimensionPointInsideOfPointList_VB: " & _ TwoDimensionPointInsideOfPointList_VB(0, 0, PointListsX, PointListsY, 5) 'try the center 'returns 1 Debug.Print "TwoDimensionPointInsideOfPointList_VB: " & _ TwoDimensionPointInsideOfPointList_VB(0, 0, PointListsX, PointListsY, 3) 'try less points 'returns 1 Debug.Print "TwoDimensionPointInsideOfPointList_VB: " & _ TwoDimensionPointInsideOfPointList_VB(0, 0, PointListsX, PointListsY, 2) 'even less again 'returns 0 Dim Lengths As Vector3D: Dim Normal As Vector3D: Dim Point As Vector3D Dim Vertex1 As Vector3D: Dim Vertex2 As Vector3D: Dim Vertex3 As Vector3D 'make three vertexes of a triangle Vertex1.x = 20: Vertex1.y = 0: Vertex1.z = 0 Vertex2.x = 20: Vertex2.y = 20: Vertex2.z = 0 Vertex3.x = -20: Vertex3.y = -20: Vertex3.z = 0 'get plane normal of a triangle Normal = TriangleNormal(Vertex1, Vertex2, Vertex3) 'get lengths of triangles edges Lengths.x = Distance(Vertex1, Vertex2) Lengths.y = Distance(Vertex2, Vertex3) Lengths.z = Distance(Vertex3, Vertex1) 'make up a point test scenario Point.x = 0: Point.y = 0: Point.z = -80 'check to see if the point is behind the ' traingle spun as a pi normal circles rea ' ch facing the point Debug.Print "VertexPoint_Behind_TrianglePlane_VB: " & VertexPoint_Behind_TrianglePlane_VB( _ Point.x, Point.y, Point.z, Lengths.x, Lengths.y, Lengths.z, Normal.x, Normal.y, Normal.z) 'returns True Point.x = 0: Point.y = 0: Point.z = 60 Debug.Print "VertexPoint_Behind_TrianglePlane_VB: " & VertexPoint_Behind_TrianglePlane_VB( _ Point.x, Point.y, Point.z, Lengths.x, Lengths.y, Lengths.z, Normal.x, Normal.y, Normal.z) 'returns False Point.x = 30: Point.y = 30: Point.z = 60 Debug.Print "VertexPoint_Behind_TrianglePlane_VB: " & VertexPoint_Behind_TrianglePlane_VB( _ Point.x, Point.y, Point.z, Lengths.x, Lengths.y, Lengths.z, Normal.x, Normal.y, Normal.z) 'returns Flase Point.x = 30: Point.y = 30: Point.z = -60 Debug.Print "VertexPoint_Behind_TrianglePlane_VB: " & VertexPoint_Behind_TrianglePlane_VB( _ Point.x, Point.y, Point.z, Lengths.x, Lengths.y, Lengths.z, Normal.x, Normal.y, Normal.z) 'returns True End Sub Public Function Distance(ByRef p1 As Vector3D, ByRef p2 As Vector3D) As Single Distance = Sqr(((p1.x - p2.x) ^ 2) + ((p1.y - p2.y) ^ 2) + ((p1.z - p2.z) ^ 2)) End Function Public Function TriangleNormal(ByRef v0 As Vector3D, ByRef v1 As Vector3D, ByRef v2 As Vector3D) As Vector3D TriangleNormal = VectorNormalize(VectorCrossProduct(VectorSubtract(v1, v0), VectorSubtract(v2, v0))) End Function Public Function DotProduct(ByRef v As Vector3D, ByRef u As Vector3D) As Single DotProduct = (u.x * v.x + u.y * v.y + u.z * v.z) End Function Public 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 Public 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 Public Function VectorNormalize(ByRef v As Vector3D) As Vector3D Dim l As Single l = Sqr(((v.x * v.x) + (v.y * v.y) + (v.z * v.z))) If l = 0 Then l = 1 VectorNormalize.x = (v.x / l) VectorNormalize.y = (v.y / l) VectorNormalize.z = (v.z / l) End Function