Graphics circles interact?

mitchmaj

Member
Joined
Dec 7, 2011
Messages
9
Programming Experience
1-3
Hey guys,
Right now i'm working on a project trying to design a game in visual basic 2008 where you control a ball with your mouse and must dodge other circular balls that bounce around the screen. To accomplish this, I use the graphics generator to both generate my ball and the computer balls. However, I want the balls to interact with one another so that when they touch, they will bounce off in opposite directions. I would know how to do this if the objects were simply squares but i was wondering if there was any way to use the actual perimeter points of the circle to have them interact. Any help anyone could give would help! thanks.
-Mitch
 
Can you give me any more information on regional area? And also will these fit to the actual dimensions of the circle, not just the square boundries defined by the coordinates? Thanks
 
You should have a look at the documentation pages for GraphicsPath class and Region class. With GraphicsPath you can define a circle from a rectangle bounds, and a Region can be constructed from the GraphicsPath object. The region only includes the GraphicsPath shape, not the bounding rectangle.
 
Thanks, This is my code so far, so what would i need to do, to do what you described. Any help would be appreciated!

Public Class Form1
Structure Enemy
Dim intX As Integer
Dim intY As Integer
Dim intChangeX As Integer
Dim intChangeY As Integer
Dim intMultiX As Integer
Dim intMultiY As Integer
Dim intSizeX As Integer
Dim intSizeY As Integer
Dim intUserArea As Integer
End Structure


Dim enemies(2) As Enemy
Dim intUserX As Integer
Dim intUserY As Integer
Dim pi As Double = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825






Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Randomize()
For intcount = 0 To enemies.Length - 1
enemies(intcount).intX = Int(757 * Rnd())
enemies(intcount).intY = Int(451 * Rnd())
enemies(intcount).intSizeX = 50
enemies(intcount).intSizeY = 50
enemies(intcount).intMultiX = Int(2 * (Rnd()) + 1)
'If enemies(intcount).intMultiX = 1 Then
' enemies(intcount).intMultiX = 1
'Else
' enemies(intcount).intMultiX = -1
'End If
'enemies(intcount).intMultiY = Int(2 * (Rnd()) + 1)
'If enemies(intcount).intMultiY = 1 Then
' enemies(intcount).intMultiY = 1
'Else
' enemies(intcount).intMultiY = -1
'End If
enemies(intcount).intChangeX = Int(6 * (Rnd()) + 1)
enemies(intcount).intChangeY = Int(6 * (Rnd()) + 1)
Next


End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
intUserX = e.X - 25
intUserY = e.Y - 25


Dim formsurface As Graphics = Me.CreateGraphics
Dim greenbrush As New SolidBrush(Color.Green)


Dim intUserSizeX As Integer = 50
Dim intUserSizeY As Integer = 50
formsurface.Clear(Me.BackColor)
formsurface.FillEllipse(greenbrush, e.X - 25, e.Y - 25, intUserSizeX, intUserSizeY)
tmrMoveComp.Enabled = True
'For intcount = 0 To enemies.Length - 1
' Call ComputerGenerate(enemies(intcount).intX, enemies(intcount).intY, enemies(intcount).intSizeX, enemies(intcount).intSizeY) 'inside timer
'Next


Dim UserArea As Integer
UserArea = pi * (25 ^ 2)


End Sub
Sub ComputerGenerate(ByVal intCX As Integer, ByVal intCY As Integer, ByVal intSCX As Integer, ByVal intSCY As Integer)
Dim formsurface2 As Graphics = Me.CreateGraphics
Dim redbrush As New SolidBrush(Color.Red)
formsurface2.FillEllipse(redbrush, intCX, intCY, intSCX, intSCY)
End Sub


Private Sub tmrMoveComp_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMoveComp.Tick
Dim formsurface As Graphics = Me.CreateGraphics
formsurface.Clear(Me.BackColor)
For intcount = 0 To enemies.Length - 1
enemies(intcount).intX += enemies(intcount).intChangeX
enemies(intcount).intY += enemies(intcount).intChangeY
Call ComputerGenerate(enemies(intcount).intX, enemies(intcount).intY, enemies(intcount).intSizeX, enemies(intcount).intSizeY) 'inside timer
Next


Dim greenbrush As New SolidBrush(Color.Green)
formsurface.FillEllipse(greenbrush, intUserX, intUserY, 50, 50)
For intcount = 0 To enemies.Length - 1
Call ComputerGenerate(enemies(intcount).intX, enemies(intcount).intY, enemies(intcount).intSizeX, enemies(intcount).intSizeY) 'inside timer
Next
End Sub


End Class
 
You certainly should not use CreateGraphics for drawing, use e.Graphics in the controls Paint event.
Apart from that you will find the GraphicsPath class very similar to the Graphics class you're already using. For example you use Graphics.FillEllipse, when you can create a circle object with GraphicsPath.AddEllipse you can draw it with Graphics.FillPath.
 
That would be the point of it.
 
thanks. Also right now, when my graphics move, they flicker. I am guessing this is because I am continually redrawing each circle to perpetuate the movement. Is there anyway to prevent this?
 
You can set DoubleBuffered property of the form to True. Apart from that you can only invalidate the parts of the form that needs to be redrawn, for example where the shape was and where it will be.
 
Could you pe4rhaps give me a sample coding for how I could fix my project? I could not figure out how to use the e.Graphics class. Please help!
 
I could not figure out how to use the e.Graphics class
You have already used the Graphics class. Your previous code use the Graphics instance returned by CreateGraphics method. Now you use the Graphics instance returned by e.Graphics property in the Paint event.
 
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?

ScreenShot.png

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
 
Back
Top