Help with detecting intersection
Help with detecting intersection
Help with detecting intersection
Help with detecting intersection
Help with detecting intersection
Help with detecting intersection Help with detecting intersection Help with detecting intersection Help with detecting intersection Help with detecting intersection Help with detecting intersection Help with detecting intersection Help with detecting intersection
Help with detecting intersection Help with detecting intersection
Help with detecting intersection
Go Back  Xtreme Visual Basic Talk > > > Help with detecting intersection


Reply
 
Thread Tools Display Modes
  #1  
Old 10-16-2004, 03:30 PM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default Help with detecting intersection


I've writen a pong game that works to some extent and i have the collision detection working *properly* but if the ball hits the bottom or top of the paddle, it sort ofbounces around inside of it until it leaves through another the other end. This really screws up the scoring system because the person using that paddle will get thousands of points because of all the collisions. Could this have something to do with the paddles not being filled? It is probably written with the most inefficient and unstable methods, but here is my complete code so far incase it is something else causing this.
Code:
Public Class Form1 Dim start As Boolean = False Dim x As Integer = 1 Dim y As Integer = 2 Dim ball As Rectangle = New Rectangle(50, 5, 10, 10) Dim lpaddle As Rectangle = New Rectangle(5, 5, 13, 60) Dim rpaddle As Rectangle = New Rectangle(Me.Width, 5, 13, 60) Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) Dim g As Graphics = e.Graphics g.FillRectangle(Brushes.White, ball) g.DrawRectangle(Pens.White, ball) g.DrawRectangle(Pens.Orange, rpaddle) g.DrawRectangle(Pens.Green, lpaddle) If Not start = False Then If ball.Top <= 1 Or ball.Bottom >= Me.ClientRectangle.Height Then y = -y ball.Offset(x, y) End If If ball.Left <= 1 Or ball.Right >= Me.ClientRectangle.Width Then x = -x ball.Offset(x, y) End If If ball.IntersectsWith(rpaddle) Then x = -x End If If ball.IntersectsWith(lpaddle) Then x = -x End If End If End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If Not start = False = True Then Me.Invalidate() ball.Offset(x, y) End If End Sub Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click start = Not start End Sub Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown If Not start = False Then If e.KeyCode = Keys.A Then lpaddle.Offset(0, -7) ElseIf e.KeyCode = Keys.Z Then lpaddle.Offset(0, 7) ElseIf e.KeyCode = Keys.K Then rpaddle.Offset(0, -7) ElseIf e.KeyCode = Keys.M Then rpaddle.Offset(0, 7) End If End If End Sub End Class
Also, when I call the "me.width" or "clientrectangle.width" as the x position for the right paddle, or anything else I want to place there, it only returns a value equal to half of the form's width. Is this a .net 2.0 specific thing? One more question. Is there any way to stop the flickering when "Me.Invalidate()" is used? I also want to that Iceplug for all of his helpful post that I based this code on from another thread.The last thing is if anyone has suggestions for making this code shorter or written better please tell me, thanks!
Reply With Quote
  #2  
Old 10-16-2004, 04:16 PM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default

I figure out that the thing about the me.width being only half of the actual width is only when defining it as the x position in a new rectangle. Is that normal?
Reply With Quote
  #3  
Old 10-18-2004, 08:40 AM
Iceplug's Avatar
IceplugHelp with detecting intersection Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

What you have to do is perform two collision detection checks.

But, before you can do that, you need to fix the actual collision detection.
First Law of Collisions (): Always do the collision check immediately after you move something.

Which means you should be doing the collision detection code in the timer, or in a sub that is called from the timer.
Putting it in OnPaint and then using Invalidate() to call it is a little OK, but you are moving the ball after calling OnPaint (which may cause problems, but not much), and then OnPaint can be called from an actual repaint of the form (when something comes over the form) calling your collision detection code again.

I would suggest that you put the collision detection in a subroutine.
Private Sub IntersectDetect()
...
End Sub

Now, to make the ball bounce correctly, you need to do two collision detections (as I mentioned).

This offset first needs to broken into two phases:
ball.Offset(x, y)
--->
ball.Offset(x, 0)
'1st phase - do first check.
ball.Offset(0, y)
'2nd phase - do second check.

Now, after each of those offsets, you will check for collision.
The first check (after the ball moves in the x direction only), will check if the ball hits the paddle head on (or from behind o.O). Here, your collision detection will invert the X velocity of the ball, making it bounce.

The second check (in the y direction) will check if the ball hits the top/bottom. Here, your collision detection will invert the Y velocity of the ball, making it bounce off the side.

Quote:
Also, when I call the "me.width" or "clientrectangle.width" as the x position for the right paddle, or anything else I want to place there, it only returns a value equal to half of the form's width.
You should only use Me.Width after the form has actually been created. The Form_Load event should be a decent time to set up the paddle data.
You should also avoid assigning things in the declarations section. That gets quite problematic (as you can see) and the Form_Load is a better place for that stuff to go.

Quote:
Is there any way to stop the flickering when "Me.Invalidate()" is used?
Yes, don't use it. Me.Invalidate clears the screen. If you clear the screen everytime before you redraw, you'll more than likely have flicker.
What you should do is make a subroutine that does your drawing.
You'll have to create your own Graphics object from the form.
Dim GFX As Graphics
GFX = Me.CreateGraphics()

This will keep you from having to call Invalidate just to redraw your form.

Of course, since the screen is not cleared, you'll have multiple drawings on the form (I think someone called it a 'slime trail' once), so what you can do is draw a background.

GFX.FillRectangle(Brushes.CadetBlue, Me.ClientRectangle)
'And no, I don't know what cadetblue looks like except for it being blue.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #4  
Old 10-18-2004, 05:19 PM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default

One thing I wanted to do to add challenge to the game is to make the paddles get smaller by one pixel or point when the ball hits them, would I just say "lpaddle.height -= 1" in the intersection?

Last edited by blindreaper666; 10-18-2004 at 05:30 PM.
Reply With Quote
  #5  
Old 10-18-2004, 07:57 PM
Iceplug's Avatar
IceplugHelp with detecting intersection Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Sure, you can do that or use the .Infliate subroutine.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #6  
Old 10-19-2004, 01:19 PM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default

Would the method you decribed for detecting collision cause more lagging if it was used on a slower system?
Reply With Quote
  #7  
Old 10-19-2004, 05:09 PM
Iceplug's Avatar
IceplugHelp with detecting intersection Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Cause more lagging than what? Doing two collision checks will definitely be slower than one, but by how much, I don't know.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #8  
Old 10-20-2004, 12:28 AM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default

Hows this for an update
Code:
Public Class Form1 Dim start As Boolean = False Dim x As Integer = 3 Dim y As Integer = 4 Dim ball As Rectangle = New Rectangle(50, 50, 10, 10) Dim lpaddle As Rectangle Dim rpaddle As Rectangle Dim area As Rectangle Dim lscore As Integer Dim rscore As Integer Dim lasthit As Integer Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) Dim g As Graphics = e.Graphics g.FillRectangle(Brushes.White, ball) g.DrawRectangle(Pens.White, ball) g.DrawRectangle(Pens.Orange, rpaddle) g.DrawRectangle(Pens.Aquamarine, lpaddle) g.DrawRectangle(Pens.White, area) End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If Not start = False = True Then If ball.Top <= area.Top Or ball.Bottom >= area.Bottom Then y = -y End If If ball.Left <= area.Left Or ball.Right >= area.Right Then x = -x End If ball.Offset(x, 0) If ball.IntersectsWith(lpaddle) Then lasthit = 0 lpaddle.Inflate(0, -2) lscore += 100 x = -x ball.Offset(x, y) End If If ball.IntersectsWith(rpaddle) Then lasthit = 1 rpaddle.Inflate(0, -2) rscore += 100 x = -x ball.Offset(x, y) End If ball.Offset(0, y) If ball.IntersectsWith(lpaddle) Then lasthit = 0 lpaddle.Inflate(0, -2) lscore += 100 y = -y x = -x ball.Offset(x, y) End If If ball.IntersectsWith(rpaddle) Then lasthit = 1 rpaddle.Inflate(0, -2) rscore += 100 y = -y x = -x ball.Offset(x, y) End If Me.Invalidate() End If End Sub Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click start = Not start End Sub Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown If Not start = False Then If e.KeyCode = Keys.A Then lpaddle.Offset(0, -7) ElseIf e.KeyCode = Keys.Z Then lpaddle.Offset(0, 7) ElseIf e.KeyCode = Keys.K Then rpaddle.Offset(0, -7) ElseIf e.KeyCode = Keys.M Then rpaddle.Offset(0, 7) End If End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load area = New Rectangle(5, 30, Me.ClientRectangle.Width - 10, Me.ClientRectangle.Height - 35) lpaddle = New Rectangle(area.X + 5, area.Y + 5, 13, 110) rpaddle = New Rectangle(area.Right - 18, area.Y + 5, 13, 110) End Sub End Class
Still gotta work on that graphics object, paddle collision with the boundaries, and the display for the score but I am like 80% done with this now! Thanks for your help. any ideas to get me started with the graphics object? would I just make it as a sub finction and replace the "me.invalidate()"s with "drawingsub()' or whatever I name it the just put in all of the stuff from the "onpaint()" override sub in it?
Reply With Quote
  #9  
Old 10-20-2004, 12:39 AM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default

Ok, heres what I've for my redraw sub, but when I click the mouse, It says, "No source code available for the specified location" heres what I have, wasn't sure what to do for arguments(I'm not really GDI+ saavy yet)
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick ... ... ... redraw(e) 'instead of the "Me.Invalidate()" End Sub 'and Public Sub redraw(ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics g.FillRectangle(Brushes.Black, Me.ClientRectangle) g.FillRectangle(Brushes.White, ball) g.DrawRectangle(Pens.White, ball) g.DrawRectangle(Pens.Orange, rpaddle) g.DrawRectangle(Pens.Aquamarine, lpaddle) g.DrawRectangle(Pens.White, area) End Sub
Reply With Quote
  #10  
Old 10-20-2004, 07:38 AM
Iceplug's Avatar
IceplugHelp with detecting intersection Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Since you are not actually raising the Paint event now, you cannot receive PaintEventArgs.
Instead, you need to go to a declarations section on your form and declare a graphics object that will draw onto the form.
Dim GFX As Graphics

And then, in Form_Load, you set up GFX like before.

For your redraw sub, you do not need to pass any arguments... just use the GFX that you have declared in the declarations section.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #11  
Old 10-20-2004, 03:35 PM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default

I tried g = me.creategraphics() but that is worse than invalidate. It doesn't draw strings and is just as if not more flickery than me.invalidate was! Do you know of any other ways to do what I need?

Last edited by blindreaper666; 10-21-2004 at 06:26 AM.
Reply With Quote
  #12  
Old 10-21-2004, 07:28 AM
Iceplug's Avatar
IceplugHelp with detecting intersection Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

What code are you using?
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #13  
Old 10-21-2004, 01:00 PM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default

Code:
Dim g as graphics '... '... '... Public Sub Timer1_Tick(arguments)Handles timer1.tick 'Collision Stuff Redraw() End Sub Public Sub Redraw() g.drawrectangle(brushes.black, me.clientrectangle) 'draw paddles and ball g.drawstring(arguments)'to draw the scores "" End Sub Public Sub Form1_Load(arguments)handles form1.load '... '... '... g = me.creategraphics() End Sub

Last edited by blindreaper666; 10-21-2004 at 01:08 PM.
Reply With Quote
  #14  
Old 10-23-2004, 07:28 AM
Iceplug's Avatar
IceplugHelp with detecting intersection Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Hmm...
Perhaps you should use an image backbuffer.
Dim Backup As Bitmap
Backup = New Bitmap(w, h) 'Width and height of your form display.

G = Graphics.FromImage(Backup)

Then, you'd need another graphics object to draw on the form.

Check out this thread.
GDI+ Backbuffers in VB.NET - Projectile Demo
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #15  
Old 10-23-2004, 06:41 PM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default

That completely stopped the flickering, but it does do that "slime trail." I probably just forgot to fill the background bitmap. Thanks!

*added* That doesn't seem to clear the form at all! Is the bitmap transparent when you first declare it?

Last edited by blindreaper666; 10-23-2004 at 07:04 PM.
Reply With Quote
  #16  
Old 10-24-2004, 06:28 PM
Iceplug's Avatar
IceplugHelp with detecting intersection Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

The bitmap should be black when you declare it with just width and height.

You do have to draw the background onto the bitmap to get rid of the slime trails.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #17  
Old 10-24-2004, 08:42 PM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default

I'm not sure this is right, but I did draw and fill a black rectangle *with* the bitmap graphics object like
Code:
gfx.drawrectangle(pens.black, rectangle1)
gfx being the graphics object from the bitmap
or is that not what to do?
Reply With Quote
  #18  
Old 10-24-2004, 08:45 PM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default

Also, I am having problems drawing strings with graphics objects. Any suggestions?
Reply With Quote
  #19  
Old 10-25-2004, 10:19 AM
Iceplug's Avatar
IceplugHelp with detecting intersection Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Use .FillRectangle to draw the background.
FillRectangle(Brushes.Black, rect)

What problems are you having with Drawing strings with the Graphics object?
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #20  
Old 10-26-2004, 07:00 AM
blindreaper666's Avatar
blindreaper666 blindreaper666 is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 317
Default

Quote:
Originally Posted by Iceplug
What problems are you having with Drawing strings with the Graphics object?
Well, it won't do it . I had a harddrive failure and lost my exact code but it was something like this:
Code:
'graphics object(not the one for the background) Dim g as graphics Private Sub Form1_Load(arguments)handles form1.load 'make the graphics object from something g = me.creategraphics() End Sub Public Sub Redraw() 'all the stuff for drawing paddles and such 'I forgot what the syntax was for drawing strings, but it was correct in my code. I did declare a new font 'in the declarations section and added a font, size, and color to it 'Draw the string with the left player's score integer and my font, once again, forgot the correct syntax g.drawstring(lscore, font1) End Sub

Last edited by blindreaper666; 10-26-2004 at 07:03 AM. Reason: Corrected spelling
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Help with detecting intersection
Help with detecting intersection
Help with detecting intersection Help with detecting intersection
Help with detecting intersection
Help with detecting intersection
Help with detecting intersection Help with detecting intersection Help with detecting intersection Help with detecting intersection Help with detecting intersection Help with detecting intersection Help with detecting intersection
Help with detecting intersection
Help with detecting intersection
 
Help with detecting intersection
Help with detecting intersection
 
-->