pixel level collision detection using VB/DX7

Started by
0 comments, last by MarcusLM 24 years, 2 months ago
I am writing a simple tile game and I already have it doing collision detection using bounding boxes but now I want to go further and put in some pixel perfect collision detection. Anyone know where I can get some good VB/DX7 source code for this?? I haven''t been able to find any on the net so far. Help please. Thanks. Marcus
Advertisement
I am having the same trouble and this still is not accurate enough. It is pretty damn close. If anyone finds out an answer to better collision detection then please post it

The way the code works
It uses 3 slope intercept equations to check if the point is inside of a 2D triangle. To check a 3D triangle it checks 3 triangles from one 3D triangle. Look at In3DPoly()


If you cannot read this code then sorry
''-------------------Codes starts here---------------------
Option Explicit
Private Type PointAPI
X As Single
Y As Single
End Type
Function InTriangle(Ray As PointAPI, V1 As PointAPI, V2 As PointAPI, V3 As PointAPI, Optional Accuracy As Single = 1) As Boolean
''this function uses Accuracy really badly and inaccurately so you can try to change it later
On Error Resume Next
Const TapVertex As Single = 1
Dim M As Single
Dim B As Single
Dim Temp As Single
Dim Intersected(2) As Boolean

''Tap verticies if necessary (this method can result to inaccuracy)
If (V1.X) = (V2.X) Then V1.X = V1.X + (TapVertex)
If (V1.X) = (V3.X) Then V3.X = V3.X + (TapVertex)
If (V2.X) = (V3.X) Then V2.X = V2.X + (TapVertex)
''1
M = (V1.Y - V3.Y) / (V1.X - V3.X)
B = V1.Y - (M * V1.X)
Temp = (M * Ray.X + B)
If V2.Y >= (M * V2.X + B) Then
If Not Ray.Y >= Temp - Accuracy Then Exit Function
Else
If Not Ray.Y <= Temp + Accuracy Then Exit Function
End If

''2
M = (V2.Y - V3.Y) / (V2.X - V3.X)
B = V2.Y - (M * V2.X)
Temp = (M * Ray.X + B)
If V1.Y >= (M * V1.X + B) Then
If Not Ray.Y >= Temp - Accuracy Then Exit Function
Else
If Not Ray.Y <= Temp + Accuracy Then Exit Function
End If

''3
M = (V1.Y - V2.Y) / (V1.X - V2.X)
B = V1.Y - (M * V1.X)
Temp = (M * Ray.X + B)
If V3.Y >= (M * V3.X + B) Then
If Not Ray.Y >= Temp - Accuracy Then Exit Function
Else
If Not Ray.Y <= Temp + Accuracy Then Exit Function
End If

If Err = 0 Then InTriangle = True
End Function
Function Get_Y_Int(Ray As D3DVECTOR, RayDirUp As Boolean, V1 As D3DVERTEX, V2 As D3DVERTEX, V3 As D3DVERTEX, ReturnValue As Long) As Boolean
On Error Resume Next
''First check if point is within the x and z of the triangle
If InTriangle(MakePoint(Ray.X, Ray.Z), MakePoint(V1.X, V1.Z), MakePoint(V2.X, V2.Z), MakePoint(V3.X, V3.Z)) = False Then Exit Function


Get_Y_Int = True
End Function
Function In3DPoly(Ray As D3DVECTOR, V1 As D3DVERTEX, V2 As D3DVERTEX, V3 As D3DVERTEX) As Boolean
If noclip = True Then Exit Function
If InTriangle(MakePoint(Ray.X, Ray.Y), MakePoint(V1.X, V1.Y), MakePoint(V2.X, V2.Y), MakePoint(V3.X, V3.Y)) = False Then ''Go down z
Exit Function
End If
If InTriangle(MakePoint(Ray.X, Ray.Z), MakePoint(V1.X, V1.Z), MakePoint(V2.X, V2.Z), MakePoint(V3.X, V3.Z)) = False Then ''Go down y
Exit Function
End If
If InTriangle(MakePoint(Ray.Z, Ray.Y), MakePoint(V1.Z, V1.Y), MakePoint(V2.Z, V2.Y), MakePoint(V3.Z, V3.Y)) = False Then ''Go down x
Exit Function
End If
In3DPoly = True
End Function
Private Function MakePoint(X As Single, Y As Single) As PointAPI
With MakePoint
.X = X
.Y = Y
End With
End Function

Questions or better solutions? Please mail me
ManilaThrilla@juno.com

This topic is closed to new replies.

Advertisement