Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Collision Detection doesn't work!!

LTaxLTax Member Posts: 1
Hi out there,

we've got a little collision-problem !

DETAILS :

We wrote a little 2D game in which our hero have to walk in a level in order to
collect some gems.
Our hero is a 2D sprite and our leves sourroundings (stones, walls etc.) are 2D sprites too.
The collision will work when the hero touches the level-stones with his
right side and (or) his bottom.
But when the hero touches the level-stones with his left side or with his top the
collision doesn't work and he can walk through the level-stones.
We used the "IntersectRect" - API function to manage this rectangle-collision.
We posted a small part of our code, but if you need more, let us know !!!

We will praise your name if you can help us ;-) !!!!!!

Many thanxxx !!!!!!!!




'#############################
And here comes the code:

Private Function Kollision() As Boolean

Dim IndexX As Integer, IndexX2 As Integer
Dim IndexY As Integer, IndexY2 As Integer
Dim R1 As RECT, R11 As RECT, R2 As RECT, R22 As RECT, R3 As RECT

' LR-Rechteck setzen
With R1
.Top = kLR.PosY
.Left = kLR.PosX
.Bottom = kLR.PosY + 22
.Right = kLR.PosX + 22
End With

' In welchem Feld befindet sich die Figur?
IndexX = Val(R1.Left 22)
IndexY = Val(R1.Top 22)

' Zu prfendes Feld in WorldArray() ermitteln
Select Case kLR.Richtung
Case Links
IndexX = IndexX - 1
IndexX2 = IndexX
IndexY2 = IndexY - 1
Case Rechts
IndexX = IndexX + 1
IndexX2 = IndexX
IndexY2 = IndexY + 1
Case Hoch
IndexY = IndexY - 1
IndexX2 = IndexX + 1
IndexY2 = IndexY
Case Runter
IndexY = IndexY + 1
IndexY2 = IndexY
IndexX2 = IndexX + 1
End Select
If IndexX < 0 Then IndexX = 0
If IndexY < 0 Then IndexY = 0
If IndexX2 < 0 Then IndexX2 = 0
If IndexY2 < 0 Then IndexY2 = 0

' Befindet sich in beiden angrenzenden Feldern nix --> Kollision = False
If WorldArray(IndexX, IndexY) = 1 And WorldArray(IndexX2, IndexY2) = 1 Then
Kollision = False
Exit Function
End If


' Ziel-Rechteck setzen
With R2
.Top = SpriteSize * IndexY
.Bottom = (SpriteSize * IndexY) + SpriteSize
.Left = SpriteSize * IndexX
.Right = (SpriteSize * IndexX) + SpriteSize
End With

' 2. Ziel-Rechteck setzen
With R22
.Top = SpriteSize * IndexY2
.Bottom = (SpriteSize * IndexY2) + SpriteSize
.Left = SpriteSize * IndexX2
.Right = (SpriteSize * IndexX2) + SpriteSize
End With

' Kollisionsabfrage mit API-Funktion "IntersectRect"
If WorldArray(IndexX, IndexY) <> 1 Then
Kollision = CBool(IntersectRect(R3, R1, R2))
Debug.Print "R2: " & Kollision & " World: " & WorldArray(IndexX, IndexY)
End If

If WorldArray(IndexX2, IndexY2) <> 1 Then
Kollision = CBool(IntersectRect(R3, R1, R22))
Debug.Print "R22: " & Kollision & " World: " & WorldArray(IndexX2, IndexY2)
End If


End Function


Comments

  • Andre YoungAndre Young USAMember Posts: 0

    ____ // http://forcoder.org // free ebooks and video tutorials about || C++, Delphi, Go, Swift, Objective-C, PL/SQL, MATLAB, Perl, Scratch, PHP, Assembly, C#, R, C, Visual Basic, Visual Basic .NET, Python, Java, JavaScript, Ruby Fortran, FoxPro, COBOL, D, Alice, VBScript, Lua, Clojure, Scheme, Logo, Scala, Dart, Crystal, Lisp, F#, Prolog, ABAP, ML, Transact-SQL, Kotlin, Erlang, Hack, SAS, Bash, Rust, Ada, Awk, Apex, LabVIEW, Julia ||

Sign In or Register to comment.