Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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

Posted

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.

Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted

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. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...