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