Question

I'm pretty new to programming. I'm trying to code OOP as much as possible .

Problem: How can I detected if two graphics make a collision with each other in a single picturebox?

Now here are the questions:

  1. Is there anyway I can do this with graphics on a bitmap. I have seen a lot of examples where two separate picterbox are used to make a collision with each, but how to do this with my example?

  2. How can I improve my current code?

    Public Class RechtHoek
    
    'property
    
    Public Property Y As Integer
        Set(value As Integer)
            _y = value
        End Set
        Get
            Return _y
        End Get
    End Property
    
    Public Property X As Integer
        Set(value As Integer)
            _x = value
        End Set
        Get
            Return _x
        End Get
    End Property
    
    'Variables
    
    Dim _y As Integer
    Dim _x As Integer
    Dim _maxY As Integer
    Dim _maxX As Integer   
    
    
    'Variabele om kleur te wijzingen
    Dim kleur As Color = RandomKleur()
    Dim VeranderKleur As Boolean = False
    Dim brush2 As New SolidBrush(RandomKleur)
    
    
    'variablen om rechthoek te draaien
    
    'Variabelen voor bewegen van de rechthoeken
    Dim goingUp As Boolean = False
    Dim goingRight As Boolean = False
    'constructor 
    Public Sub New(x As Integer, y As Integer, maxX As Integer, MaxY As Integer)
    
        _y = y
        _x = x
        _maxX = maxX
        _maxY = MaxY
    End Sub
    
    Public Sub DrawRechthoekHorizontaal(graph As Graphics)
    
    
        Dim pen As New Pen(Color.Black)
        Dim rect As New Rectangle(_x, Y, 40, 100)
    
    
        graph.FillRectangle(brush2, rect)
        graph.DrawRectangle(pen, rect)
    
    End Sub
    
    Public Sub DrawRechthoekVerticaal(graph As Graphics)
    
    
        Dim pen As New Pen(Color.Black)
        Dim rect As New Rectangle(_x, Y, 100, 40)
    
    
    
        graph.FillRectangle(brush2, rect)
        graph.DrawRectangle(pen, rect)
    
    End Sub  
    
    
    Public Function RandomKleur() As Color
    
    
        Dim myAlpha As Integer = 0
        Dim myRed As Integer = 0
        Dim myGreen As Integer = 0
        Dim myBlue As Integer = 0
    
        Randomize()
    
    
        myAlpha = CInt(Int((254 * Rnd()) + 0))
        Randomize()
        myGreen = CInt(Int((254 * Rnd()) + 0))
        Randomize()
        myRed = CInt(Int((254 * Rnd()) + 0))
        Randomize()
        myBlue = CInt(Int((254 * Rnd()) + 0))
    
        'Color.FromArgb(myAlpha, myRed, myGreen, myBlue)
    
        Return Color.FromArgb(myAlpha, myRed, myGreen, myBlue)
    
    
    End Function
    
    Public Sub Up()
        Y -= 10
    End Sub
    
    Public Sub Down()
        Y += 10
    End Sub
    
    Public Sub Right()
        X -= 10
    End Sub
    
    Public Sub Left()
        X += 10
    End Sub
    
    
    Public Sub MoveX()
    
        If goingRight Then
    
            Right()
    
        Else
    
            Left()
        End If
    
        If X < 0 Then
    
            goingRight = False
    
            'verander van kleur 
            If X = -10 Then
                VeranderKleur = True
                brush2.Color = RandomKleur()
            End If
    
        ElseIf X > 1600 Then
    
    
            goingRight = True
    
            'verander van kleur 
            If X = 1610 Then
                VeranderKleur = False
                brush2.Color = RandomKleur()
            End If
    
        End If
    
    
    End Sub
    
    Public Sub MoveY()
    
        If goingUp Then
    
            Up()
    
        Else
    
            Down()
    
        End If
    
        If Y < 0 Then
    
            goingUp = False
    
            'verander van kleur 
            If Y = -10 Then
                VeranderKleur = True
                brush2.Color = RandomKleur()
            End If
    
        ElseIf Y > 900 Then
    
            goingUp = True
    
            'verander van kleur 
            If Y = 910 Then
                VeranderKleur = True
                brush2.Color = RandomKleur()
            End If
    
        End If
    
    End Sub
    
    End Class
    
    Public Class FormBewegendeRechtHoeken
    
    Dim rectY As RechtHoek
    Dim rectX As RechtHoek
    
    Dim bitmap As Bitmap
    
    Private img As Image
    
    Private Sub FormRechtHoek_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        bitmap = New Bitmap(PictureBox.Width, PictureBox.Height)
    
        'X = plaats waar rechthoek start op x as , Y = plaats waar rechthoekstart op y as 'Max x hoogete &' Max Y hoogte 
        rectY = New RechtHoek(PictureBox.Width \ 2, PictureBox.Height, PictureBox.Width, PictureBox.Height)
    
        rectX = New RechtHoek(PictureBox.Width, PictureBox.Height \ 2, PictureBox.Width, PictureBox.Height)
    
    
        Draw()
    
        TheTimer.Start()
    
    End Sub
    
    Private Sub Botsing()
    
    
    End Sub
    
    Private Sub Draw()
    
        Using graph As Graphics = Graphics.FromImage(bitmap)
    
            graph.Clear(Color.Yellow)
    
    
    
            rectX.DrawRechthoekHorizontaal(graph)
    
    
            'Omkeren van de rechthoek in het midden
            If rectY.Y < 450 Then
                rectY.DrawRechthoekHorizontaal(graph)
            End If
    
            If rectY.Y > 450 Then
    
                rectY.DrawRechthoekVerticaal(graph)
    
            End If
    
    
    
        End Using
    
        PictureBox.Image = bitmap
    
    End Sub
    
    Private Sub TheTimer_Tick(sender As Object, e As EventArgs) Handles TheTimer.Tick
    
    
        rectY.MoveY()
        rectX.MoveX()
        Draw()
    
    End Sub
    
    End Class
    
Was it helpful?

Solution

If you save the rectangle structures you draw in an array or a list, you can check a collision like this:

The rectangles collide when one rectangle contains at least one edge point of the other rectangle. So you can define a function:

Function CheckRectCollision(Rect1 as Rectangle, Rect2 as Rectangle) as Boolean
   If Rect1.Contains(New Point(Rect2.Left, Rect2.Top) Orelse _
      Rect1.Contains(New Point(Rect2.Left, Rect2.Bottom) Orelse _
      Rect1.Contains(New Point(Rect2.Right, Rect2.Top) Orelse _
      Rect1.Contains(New Point(Rect2.Right, Rect2.Bottom) Then
      Return True
    Else
      Return False
    End If
End Function

That way you can check if two rectangles collide. Find exclusive permutations for all rectangle pairs you have and call the function for each pair.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top