I need help with my function In3DTriangle. It does not correctly check if the point you specify is in the triangle. I have a feeling that it has something to do with my dotproducts. This function checks if the point is in the triangle by creating 3 planes arround the triangle and checking if the point is on all the planes front sides. The other function below works. It is just there for readibility. Sorry for no HTML formatting.
Function In3DTriangle(PointToCheck As D3DVECTOR, CircleRadius As Single, p1 As D3DVECTOR, p2 As D3DVECTOR, p3 As D3DVECTOR) As Boolean
On Error Resume Next
Dim PlaneNormals(2) As D3DVECTOR
Dim OpposingVector As D3DVECTOR
''Check Bounding Circle First
If Not Abs(GetPlaneDistance(PointToCheck, p1, p2, p3)) < CircleRadius Then Exit Function
''Do this for Side One
OpposingVector.X = (p1.X + p3.X) / 2
OpposingVector.Y = (p1.Y + p3.Y) / 2
OpposingVector.Z = (p1.Z + p3.Z) / 2
PlaneNormals(0).X = p2.X - OpposingVector.X
PlaneNormals(0).Y = p2.Y - OpposingVector.Y
PlaneNormals(0).Z = p2.Z - OpposingVector.Z
''Do this for Side Two
OpposingVector.X = (p1.X + p2.X) / 2
OpposingVector.Y = (p1.Y + p2.Y) / 2
OpposingVector.Z = (p1.Z + p2.Z) / 2
PlaneNormals(1).X = p3.X - OpposingVector.X
PlaneNormals(1).Y = p3.Y - OpposingVector.Y
PlaneNormals(1).Z = p3.Z - OpposingVector.Z
''Do this for Side Three
OpposingVector.X = (p2.X + p3.X) / 2
OpposingVector.Y = (p2.Y + p3.Y) / 2
OpposingVector.Z = (p2.Z + p3.Z) / 2
PlaneNormals(2).X = p1.X - OpposingVector.X
PlaneNormals(2).Y = p1.Y - OpposingVector.Y
PlaneNormals(2).Z = p1.Z - OpposingVector.Z
''This is just to fix my vectors
dx.VectorNormalize PlaneNormals(0)
dx.VectorNormalize PlaneNormals(1)
dx.VectorNormalize PlaneNormals(2)
''now check if it is in the triangle
If dx.VectorDotProduct(PointToCheck, PlaneNormals(0)) > 0 And _
dx.VectorDotProduct(PointToCheck, PlaneNormals(1)) > 0 And _
dx.VectorDotProduct(PointToCheck, PlaneNormals(2)) > 0 Then
In3DTriangle = True
End If
End Function
Function GetPlaneDistance(PointToCheck As D3DVECTOR, p1 As D3DVECTOR, p2 As D3DVECTOR, p3 As D3DVECTOR) As Single
On Error Resume Next
Dim V1 As D3DVECTOR, V2 As D3DVECTOR, CP As D3DVECTOR
Dim TempSingle As Single
V1.X = p1.X - p2.X
V1.Y = p1.Y - p2.Y
V1.Z = p1.Z - p2.Z
V2.X = p1.X - p3.X
V2.Y = p1.Y - p3.Y
V2.Z = p1.Z - p3.Z
dx.VectorCrossProduct CP, V1, V2
TempSingle = (V1.X ^ 2 + V1.Y ^ 2 + V1.Z ^ 2)
CP.X = CP.X / TempSingle
CP.Y = CP.Y / TempSingle
CP.Z = CP.Z / TempSingle
GetPlaneDistance = dx.VectorDotProduct(PointToCheck, CP)
End Function
It is using DX7 with Visual Basic. Object DX is valid also.