Help
This is one small portion of my coding. However, I am having a small problem with the ball interactions. As you can see from below, when the balls touch, the directions are simply reversed (through the use of the multiplier). However, this presents a small problem: when the balls touch for instance, they bounce off in peculiar directions. Does anyone have a solution for this problem?
This is a picture of my form design window.
Dim intCX1horizontal As Integer = 1 Dim intCX2horizontal As Integer = 1
Dim intCX3horizontal As Integer = 1
Dim intCY1verticle As Integer = 1
Dim intCY2verticle As Integer = 1
Dim intCY3verticle As Integer = 1
Private Sub CompMove_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompMove.Tick
Static intMoveX1 As Integer = Int(8 * Rnd() + 1)
Static intMoveY1 As Integer = Int(8 * Rnd() + 1)
Static intMoveX2 As Integer = Int(8 * Rnd() + 1)
Static intMoveY2 As Integer = Int(8 * Rnd() + 1)
Static intMoveX3 As Integer = Int(8 * Rnd() + 1)
Static intMoveY3 As Integer = Int(8 * Rnd() + 1)
Call EnemyMove(pctCompBall1, intMoveX1, intMoveY1, intCX1horizontal, intCY1verticle)
Call EnemyMove(pctCompBall2, intMoveX2, intMoveY2, intCX2horizontal, intCY2verticle)
Call EnemyMove(pctCompBall3, intMoveX3, intMoveY3, intCX3horizontal, intCY3verticle)
Dim intCX1 As Integer = pctCompBall1.Location.X
Dim intCY1 As Integer = pctCompBall1.Location.Y
Dim intCX2 As Integer = pctCompBall2.Location.X
Dim intCY2 As Integer = pctCompBall2.Location.Y
Dim intCX3 As Integer = pctCompBall3.Location.X
Dim intCY3 As Integer = pctCompBall3.Location.Y
If pctCompBall1.Bounds.IntersectsWith(pctCompBall2.Bounds) Then
intCX1horizontal *= -1
intCY1verticle *= -1
intCX2horizontal *= -1
intCY2verticle *= -1
End If
If pctCompBall1.Bounds.IntersectsWith(pctCompBall3.Bounds) Then
intCX1horizontal *= -1
intCY1verticle *= -1
intCX3horizontal *= -1
intCY3verticle *= -1
End If
If pctCompBall2.Bounds.IntersectsWith(pctCompBall3.Bounds) Then
intCX2horizontal *= -1
intCY2verticle *= -1
intCX3horizontal *= -1
intCY3verticle *= -1
End If
Dim redball1 As PictureBox = pctCompBall1
'intCX1 += 1 * intCX1horizontal
'intCY1 += 8 * intCY1verticle
'If redball1.Bounds.IntersectsWith(UserPicBox1.Bounds) Or redball1.Bounds.IntersectsWith(UserPicBox2.Bounds) Or redball1.Bounds.IntersectsWith(UserPicBox3.Bounds) Then
' CompMove.Enabled = False
' MessageBox.Show("Hit")
' btnReset.Enabled = True
' Time.Enabled = False
' intTime = 0
'End If
If intCX1 >= 707 Then
intCX1horizontal = -1
ElseIf intCX1 <= 0 Then
intCX1horizontal = 1
End If
If intCY1 >= 390 Then
intCY1verticle = -1
ElseIf intCY1 <= 0 Then
intCY1verticle = 1
End If
redball1.Location = New Point(intCX1, intCY1)
Dim redball2 As PictureBox = pctCompBall2
'intCX2 += 2 * intCX2horizontal
'intCY2 += 4 * intCY2verticle
'If redball2.Bounds.IntersectsWith(UserPicBox1.Bounds) Or redball2.Bounds.IntersectsWith(UserPicBox2.Bounds) Or redball2.Bounds.IntersectsWith(UserPicBox3.Bounds) Then
' CompMove.Enabled = False
' MessageBox.Show("Hit")
' btnReset.Enabled = True
' Time.Enabled = False
' intTime = 0
'End If
If intCX2 >= 707 Then
intCX2horizontal = -1
ElseIf intCX2 <= 0 Then
intCX2horizontal = 1
End If
If intCY2 >= 390 Then
intCY2verticle = -1
ElseIf intCY2 <= 0 Then
intCY2verticle = 1
End If
redball2.Location = New Point(intCX2, intCY2)
Dim redball3 As PictureBox = pctCompBall3
'intCX3 += 3 * intCX3horizontal
'intCY3 += 7 * intCY3verticle
'If redball3.Bounds.IntersectsWith(UserPicBox1.Bounds) Or redball3.Bounds.IntersectsWith(UserPicBox2.Bounds) Or redball3.Bounds.IntersectsWith(UserPicBox3.Bounds) Then
' CompMove.Enabled = False
' MessageBox.Show("Hit")
' btnReset.Enabled = True
' Time.Enabled = False
' intTime = 0
'End If
If intCX3 >= 707 Then
intCX3horizontal = -1
ElseIf intCX3 <= 0 Then
intCX3horizontal = 1
End If
If intCY3 >= 390 Then
intCY3verticle = -1
ElseIf intCY3 <= 0 Then
intCY3verticle = 1
End If
redball3.Location = New Point(intCX3, intCY3)
'If pct1Enemy2.Bounds.IntersectsWith(pct2Enemy1.Bounds) Or pct1Enemy2.Bounds.IntersectsWith(pct2Enemy2.Bounds) Or pct1Enemy2.Bounds.IntersectsWith(pct2Enemy3.Bounds) Then
' intCX3horizontal *= -1
' intCY3verticle *= -1
' intCX2horizontal *= -1
' intCY2verticle *= -1
'End If
'If redball3.Bounds.IntersectsWith(redball1.Bounds) Then
' intCX3horizontal *= -1
' intCY3verticle *= -1
' intCX1horizontal *= -1
' intCY1verticle *= -1
'End If
End Sub
Sub EnemyMove(ByRef redball As PictureBox, ByVal intMoveX As Integer, ByVal intMoveY As Integer, ByVal intMultiplierX As Integer, ByVal intMultiplierY As Integer)
Dim intCX As Integer = redball.Location.X
Dim intCY As Integer = redball.Location.Y
'If intCX >= 707 Then
' intMultiplierX = -1 'used up above instead
'ElseIf intCX <= 0 Then
' intMultiplierX = 1
'End If
'If intCY >= 390 Then
' intMultiplierY = -1
'ElseIf intCY <= 0 Then
' intMultiplierY = 1
'End If
intCX += intMoveX * intMultiplierX 'Redball movement X
intCY += intMoveY * intMultiplierY 'Redball movement Y
'intPCX1 += intMoveX * intMultiplierX 'Box movement
'intPCY1 += intMoveY * intMultiplierY
'intPCX2 += intMoveX * intMultiplierX 'Horiz Box movement
'intPCY2 += intMoveY * intMultiplierY
'intPCX3 += intMoveX * intMultiplierX 'Vert Box movement
'intPCY3 += intMoveY * intMultiplierY
redball.Location = New Point(intCX, intCY) 'setting new position
If redball.Bounds.IntersectsWith(UserPicBox1.Bounds) Or redball.Bounds.IntersectsWith(UserPicBox2.Bounds) Or redball.Bounds.IntersectsWith(UserPicBox3.Bounds) Then
CompMove.Enabled = False
MessageBox.Show("Hit")
btnReset.Enabled = True
Time.Enabled = False
intTime = 0
End If
End Sub