-1

I am making a game were I need to check that every time that my enemies collide, the bounce to the other side, I tried creating two for .. next and checking with intersects with, but it failed.

Public Class Form1
Private pic_Nave As Nave
Private pic_Base As Base
Private vDesecho(10) As Desecho
Private nivel As Integer
Private vAsteroide(10) As Asteroide
Private desechoEliminated As Integer
'Private cont(10) As Double
Private spawntime As Integer

Private Sub tmr_Mover_Tick(sender As Object, e As EventArgs) Handles tmr_Mover.Tick
    pic_Nave.Mover(Me)
    pic_Base.Mover(Me)
    spawntime -= 1
    lblitems.Text = spawntime


    For i = 0 To 9
        vDesecho(i).Mover(Me)
        If (pic_Nave.imagen.Bounds.IntersectsWith(vDesecho(i).imagen.Bounds)) Then

            Controls.Remove(vDesecho(i).imagen)
        End If
        If (pic_Base.imagen.Bounds.IntersectsWith(vDesecho(i).imagen.Bounds)) Then
            vDesecho(i).diry = -vDesecho(i).diry
        End If
    Next
    'For i = 0 To 9
    'For j = 9 To 0
    'If (vDesecho(i).imagen.Bounds.IntersectsWith(vDesecho(j).imagen.Bounds)) Then
    'spawntime = 5
    'vDesecho(i).dirx = -vDesecho(i).dirx
    'vDesecho(j).diry = -vDesecho(j).diry
    'vDesecho(j).Mover(Me)
    'End If
    'Next
    'Next

    For i = 0 To nivel - 1
        vAsteroide(i).Mover(Me)
        If (pic_Base.imagen.Bounds.IntersectsWith(vAsteroide(i).imagen.Bounds)) Then
            vAsteroide(i).diry = -vAsteroide(i).diry
        End If
    Next
End Sub

Private Sub makedesecho()
    For i = 1 To 1
        vDesecho(i) = New Desecho()
        Controls.Add(vDesecho(i).imagen)

        vDesecho(i).Mover(Me)
        If (pic_Nave.imagen.Bounds.IntersectsWith(vDesecho(i).imagen.Bounds)) Then

            Controls.Remove(vDesecho(i).imagen)
            desechoEliminated = desechoEliminated + 1
        End If
    Next
End Sub

Public Sub New()

    ' Esta llamada es exigida por el diseñador.
    InitializeComponent()

    ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
    nivel = 1
    desechoEliminated = 0
    spawntime = 5
    For i = 0 To 9
        vDesecho(i) = New Desecho()
        Controls.Add(vDesecho(i).imagen)
    Next

    For i = 0 To nivel - 1
        vAsteroide(i) = New Asteroide()
        Controls.Add(vAsteroide(i).imagen)
    Next

    pic_Nave = New Nave()
    Controls.Add(pic_Nave.imagen)

    pic_Base = New Base()
    Controls.Add(pic_Base.imagen)

    tmr_Mover.Start()

End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
    Select Case keyData
        Case Keys.Up
            pic_Nave.diry -= 1
        Case Keys.Down
            pic_Nave.diry += 1
        Case Keys.Left
            pic_Nave.dirx -= 1
        Case Keys.Right
            pic_Nave.dirx += 1

    End Select
    Return MyBase.ProcessCmdKey(msg, keyData)
End Function

End Class

This is the full class, I’m using also a timer to move some other objects like an spaceship and the enemies are space junk. i need a way to know when the space junks collide between them, to make them bounce

braX
  • 11,506
  • 5
  • 20
  • 33
  • A control (like picturebox) has four parameters 1) Top 2) Left 3) Height 4) Width. X,Y is the top left corner of the control. So just testing X,Y for collision doesn't work. Testing for collision means you have to check all four sides of the control. – jdweng Feb 25 '23 at 16:44
  • How are these things declared? Especially `vDesecho`. And how is it initialized? We have not enough information to find the error or debug. – Olivier Jacot-Descombes Feb 25 '23 at 16:44
  • @jdweng. `Bounds` is a `Rectangle`. So, this should work. There must be a problem somewhere else. – Olivier Jacot-Descombes Feb 25 '23 at 16:45
  • Print out the top, left, width, and height of vDesecho(i).imagen and vDesecho(j).imagen and see where the object are located. – jdweng Feb 25 '23 at 16:55
  • hi, added the full form class – Lizmarie Feriz Feb 25 '23 at 17:08
  • See this answer [How to determine if two rectangles overlap](https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other) (A very popular question and answer) – user20716902 Feb 25 '23 at 23:37

1 Answers1

0

The problem with your nested loop (which is commented out) is that it considers every pair of objects twice. E.g., vDesecho(3) with vDesecho(8) and also vDesecho(8) with vDesecho(3) making change its direction twice. Additionally also collisions with itself are detected. You can avoid this by changing the nested loop like this:

For i = 0 To 9
    For j = i + 1 To 9 ' <=========
        If (vDesecho(i).imagen.Bounds.IntersectsWith(vDesecho(j).imagen.Bounds)) Then
            spawntime = 5
            vDesecho(i).dirx = -vDesecho(i).dirx
            vDesecho(j).diry = -vDesecho(j).diry
            vDesecho(j).Mover(Me)
        End If
    Next
Next
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188