I'm trying to make the picturebox collide with the others.
I tried to do several things but they don't work.
I spoke to my teacher and he said I should do something like this:

PictureBox1.Left + PictureBox1.Width >= PictureBox2.Left And
         PictureBox1.Top + PictureBox1.Height >= PictureBox2.Top And
         PictureBox1.Top <= PictureBox2.Top + PictureBox2.Height And
         PictureBox1.Left <= PictureBox2.Left + PictureBox2.Width

But it didn't work either, not if it was because I didn't set the variables right or if I didn't put it in the right place.

This is The full code:

Imports System.Math
Imports System.Security.Policy

Public Class Form1
    Dim CoordX As Single
    Dim CoordY As Single
    Dim Velocity As Double = 0
    Dim Angle As Double
    Dim Time As Double = 0
    Dim Gravity As Double = 9.8
    Dim moving As Boolean = False
    Dim mousePosX As Integer
    Dim mousePosY As Integer
    Dim X As Integer = 80
    Dim Y As Integer = 250
    Dim picturebox1 As New PictureBox
    Dim Timer1 As New Timer
    Dim Timer2 As New Timer
    Dim Tracking As New RichTextBox
    Dim Angle2 As Single
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Time += 0.1
        'formula da trajetoria para o x e o y
        CoordX = Velocity * Time * Cos(Angle)
        CoordY = (Velocity * Time * Sin(Angle)) - ((Gravity / 2) * Time * Time)
        picturebox1.Invalidate() 'desenhar a bola 

        picturebox1.Top += 0.1
        If picturebox1.Bounds.IntersectsWith(PictureBox3.Bounds) Then
        End If
    End Sub
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim deltaY As Integer = 250 - mousePosY
        Dim deltaX As Integer = mousePosX - 86
        Dim temp As Single
        temp = Math.Atan2(deltaY, deltaX) * 180 / Math.PI
        If Not temp >= 0 Then
            temp += 360
        End If
        Angle2 = temp
        'mostar as cordenadas - mas para isso funconar é preciso criar uma label na public class
        'Tracking.Text = " Ball Coord_X: " & CoordX & vbNewLine & " Ball Coord_Y: " & CoordY & vbNewLine _
        ' & " Gravity: 9.8" & vbNewLine & " Velocity: " & Velocity & vbNewLine & " Angle: " & Angle2
        Tracking.Text = "Pontos:"
    End Sub
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        ' verificar se a bola está em movimento ou a saltar
        If Timer1.Enabled = True Then
            moving = False
        Else
            'reiniciar
            CoordX = 0
            CoordY = 0
            Time = 0
            Velocity = 0
            X = mousePosX - 8
            Y = mousePosY - 8
            moving = True
        End If
    End Sub
    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If Timer1.Enabled = False Then
            moving = False
            X = mousePosX - 8
            Y = mousePosY - 8
            Timer1.Interval = 10
            FindAngle()
            Timer1.Start()
            'label.Visible = False - isto só é valido no caso de mostrar-mos os dados na label
        End If
    End Sub
    Public Sub FindAngle()
        'obter 2 angulos x
        ' (250,80) está no meio 
        Dim deltaY As Integer = 250 - mousePosY
        Dim deltaX As Integer = mousePosX - 86
        Dim temp As Single
        temp = Math.Atan2(deltaY, deltaX) * 180 / Math.PI
        'Tirar numeros negativos
        If Not temp >= 0 Then
            temp += 360
        End If
        'angulo inverso
        'se a bola tiver em 225, entao vai ser lançado num angulo de 45
        If temp + 180 > 360 Then
            temp += 180
        Else
            temp -= 180
        End If
        'degrees to radians
        temp = temp * PI / 180
        Angle = temp
    End Sub
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        'Mover a bola
        If moving = True Then
            mousePosX = e.X
            mousePosY = e.Y
            picturebox1.Invalidate()
        End If
    End Sub
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        If moving = True Then
            ' regras
            If mousePosX - 8 < 120 And mousePosX - 8 > 10 Then
                e.Graphics.FillEllipse(Brushes.OrangeRed, mousePosX - 8, mousePosY - 8, 16, 16)
            Else
                If mousePosX - 8 > 120 Then
                    e.Graphics.FillEllipse(Brushes.OrangeRed, 118, mousePosY - 8, 16, 16)
                    mousePosX = 118
                Else
                    e.Graphics.FillEllipse(Brushes.OrangeRed, 12, mousePosY - 8, 16, 16)
                    mousePosX = 12
                End If
            End If
            'da velociade ao calcular a distancia que esta se encontra do  centro
            'quando mais for a distância maior é a força
            Dim xx As Integer
            Dim yy As Integer

            If mousePosX > 80 Then
                xx = mousePosX - 80
            Else
                xx = 80 - mousePosX
            End If

            If mousePosY > 250 Then
                yy = mousePosY - 250
            Else
                yy = 250 - mousePosY
            End If

            If yy > xx Then
                Velocity = yy
            Else
                Velocity = xx
            End If

        Else
            'single - para desenhar melhor 
            e.Graphics.FillEllipse(Brushes.Navy, X + CoordX, Y - CoordY, 16, 16)
        End If
        If Y - CoordY > 315 Then
            Timer1.Stop()
            Time = 0
            X = X + CoordX
            Y = Y - CoordY
            ' a velocidade tem de diminuir enquanto a bola tocar no chão 
            Velocity -= 5
            If Velocity < 1 Then
                Timer1.Stop()
                Velocity = 0
                e.Graphics.FillEllipse(Brushes.OrangeRed, 80, 250, 16, 16)
            Else
                Timer1.Start()
            End If

        End If
        e.Graphics.FillEllipse(Brushes.White, 82, 254, 8, 8)
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.BackColor = Color.Black
        Me.Size = New Size(900, 370)
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
        picturebox1.Location = New Point(0, 0)
        picturebox1.Size = New Size(900, 390)
        picturebox1.BackColor = Color.Transparent
        picturebox1.Name = "picturebox1"
        Tracking.Location = New Point(750, 12)
        Tracking.Size = New Size(200, 106)
        Tracking.BackColor = Color.Black
        Tracking.Cursor = Cursors.Default
        Tracking.BorderStyle = BorderStyle.None
        Tracking.ReadOnly = True
        Tracking.ForeColor = Color.GreenYellow
        Me.Controls.Add(picturebox1)
        Me.Controls.Add(Tracking)
        Tracking.BringToFront()
        AddHandler picturebox1.Paint, AddressOf PictureBox1_Paint
        AddHandler picturebox1.MouseMove, AddressOf PictureBox1_MouseMove
        AddHandler picturebox1.MouseUp, AddressOf PictureBox1_MouseUp
        AddHandler picturebox1.MouseDown, AddressOf PictureBox1_MouseDown
        AddHandler Timer1.Tick, AddressOf Timer1_Tick
        AddHandler Timer2.Tick, AddressOf Timer2_Tick
        Timer2.Start()
    End Sub
    End Class

Recommended Answers

All 4 Replies

Where did you attempt to put the code that didn’t work? I’m not seeing it in your full code. Also, the code you are saying isn’t working is just a conditional that checks the size/placements of picturebox1 and pixturebox2 with respect to each other. It doesn’t actually move either of them.

At the beginning I put the code in the load form and as nothing happened I tried to put it in the timers 1 and 2.
But that makes the picturebox1 not move so I removed it.
Then I tried this one:

Private Function Collision(ByVal PictureBox1 As PictureBox, ByVal PictureBox2 As PictureBox)
        Dim Collided As Boolean = False
        If PictureBox1.Left + PictureBox1.Width >= PictureBox2.Left And
         PictureBox1.Top + PictureBox1.Height >= PictureBox2.Top And
         PictureBox1.Top <= PictureBox2.Top + PictureBox2.Height And
         PictureBox1.Left <= PictureBox2.Left + PictureBox2.Width Then
            Collided = True
        End If
        Return Collided
    End Function

I think it didn't work because it can't identify the coordinates, but I don't know how to do that.

So that function just returns true or false when called, it doesn't actually move anything.

Unfortunately I haven't looked at VB code in 25 years, so I can't be of much further assistance to you, although I'm confused by what you mean when you say you can't identify the coordinates. because it looks like PictureBox1.Top and PictureBox1.Left is identifying its coordinates, no?

I think that for anything to happen he has to identify well the coordinates of the picturebox he is going to "delete".
I don't understand much of vb.net, it's literally the 3rd time I've used it hahahaha.
I created a new project to see if the problem was in my current code and it worked except it's not how I want it.
What I'm trying to do is kind of an Angry birds game so I used a lot of physics stuff in my original code. So it can be launched and with that display me the coordinates, speed and angle.
Collision code new project:

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
 If PictureBox1.Bounds.IntersectsWith(PictureBox2.Bounds) Then 'Determine If The Two PictureBoxes Collided

            PictureBox2.Hide()
            MessageBox.Show("boom")
        End If
    End Sub

I used the keyDown because I wasn't being able to use the MouseDown.
After I used this code in the new project I put it in the "old" one but it still doesn't work.
I guess I'll have to wait for the teacher to answer me, but I really appreciate you trying to help me <333

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.