jusanthermemory Posted December 12, 2005 Posted December 12, 2005 Im trying to get my collision detection to work and the picture boxes move in certain directions when they hit each other. Is there anyway to change this code so that It specifies a certain side? If Me.linkviewer.Bounds.IntersectsWith(enemyviewer.Bounds) Then Me.linkviewer.Left = Me.linkviewer.Left + 100 Me.enemyviewer.Left = Me.enemyviewer.Left - 100 End If so if linkviewer hit enemyviewer on the right, linkviewer would go left and enemyviewer would go right and vice versa. same for top Thanks for any help Quote
Diesel Posted December 12, 2005 Posted December 12, 2005 Never used that function...but you could trick it into just checking a certain side...by creating a new bounds rectangle 1 pixel thick on whichever side you want to check. For example the left side: Rectangle rec = new Rectangle(this.Bounds.X, this.Bounds.Y, this.Bounds.X+1, this.Bounds.Bottom) That's one idea Quote
jusanthermemory Posted December 15, 2005 Author Posted December 15, 2005 I'm sry but I don't really understand how to use that code. If you dont mind could you please give me a sample of how to use it? thanks for your help and sry its taken so long to reply Quote
Cags Posted December 15, 2005 Posted December 15, 2005 You would use that code like this, but I'm not sure it's the best way to achieve what you want.... Dim rectOne As Rectangle = New Rectangle(0, 0, 100, 100) Dim rectTwo As Rectangle = New Rectangle(100, 100, 100, 100) If rectOne.IntersectsWith(New Rectangle(rectOne.Left, rectOne.Top, 1, rectOne.Height)) Then ' Left side ElseIf rectOne.IntersectsWith(New Rectangle(rectOne.Left, rectOne.Bottom - 1, rectOne.Width, 1)) Then ' Bottom side ElseIf rectOne.IntersectsWith(New Rectangle(rectOne.Right - 1, rectOne.Top, 1, rectOne.Height)) Then ' Right side ElseIf rectOne.IntersectsWith(New Rectangle(rectOne.Left, rectOne.Top, rectOne.Width, 1)) Then ' Top side End If rectOne & rectTwo would be the bounds of your two picture boxes. Quote Anybody looking for a graduate programmer (Midlands, England)?
Leaders Iceplug Posted December 17, 2005 Leaders Posted December 17, 2005 The trick to doing it is to perform two collision checks: once after the image moves horizontally and again, after the image moves vertically. Obj.Left = Obj.Left + 100 If Obj.Bounds.IntersectsWith(enemyviewer.Bounds) Then 'collided on left or right side. End If Obj.Top = Obj.Top + 100 If Obj.Bounds.IntersectsWith(enemyviewer.Bounds) Then 'collided on top or bottom side. End If To determine which of the sides you actually collided on within the If blocks, look at the sign of the object's velocity (i.e. the 100 that you add to the left/top positions. This method is not foolproof, especially if you have small objects moving large distances. :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.