Search code examples
vb.netellipsecollision

ellipses collisions in vb.net?


I'm facing some problems in my game in fact I had to detect an ellipse-ellipse collision I'll prefer codes in vb.net language as I can't understand any other one any helps with ideas or codes?


Solution

  • You can simply check collisions with Region objects and Region.Intersect method:

    Dim gp1 As New Drawing2D.GraphicsPath()
    gp1.AddEllipse(rect1)
    
    Dim gp2 As New Drawing2D.GraphicsPath()
    gp2.AddEllipse(rect2)
    
    Dim reg1 As New Region(gp1)
    Dim reg2 As New Region(gp2)
    
    reg1.Intersect(reg2)
    
    If reg1.GetRegionScans(New Drawing2D.Matrix(1, 1, 0, 1, 1, 0)).Length > 0 Then
        MessageBox.Show("Collision!")
    End If
    

    In this example rect1 and rect2 are Rectangles that define (contain) the ellipses, like when you draw the ellipses with a rectangle:

    Dim rect1 As New Rectangle(5, 5, 150, 100)
    Me.CreateGraphics.DrawEllipse(Pens.Blue, rect1)
    


    EDIT:

    For performance and options you better use a game engine. Check this links: