Results 1 to 2 of 2

Thread: object collision

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    object collision

    hello all, I was wondering how I could have an object bounce back when it collides with an rectangle shape. There are multiple rectangle shapes and I don't want to do it all seperately for each one. Does someone know how to do this?
    Thanks a lot, Nick Dekker

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: object collision

    here's an example:

    vb Code:
    1. Public Class Form2
    2.  
    3.     Dim objects() As PictureBox
    4.  
    5.     Private Enum direction
    6.         N
    7.         NE
    8.         E
    9.         SE
    10.         S
    11.         SW
    12.         W
    13.         NW
    14.     End Enum
    15.  
    16.     Dim currentDirection As direction = direction.N
    17.  
    18.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19.         objects = New PictureBox() {PictureBox1, PictureBox2, PictureBox3}
    20.         Timer1.Enabled = True
    21.     End Sub
    22.  
    23.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    24.  
    25.         Select Case currentDirection
    26.             Case direction.N
    27.                 PictureBox4.Top -= 10
    28.             Case direction.NE
    29.                 PictureBox4.Top -= 10
    30.                 PictureBox4.Left += 10
    31.             Case direction.E
    32.                 PictureBox4.Left += 10
    33.             Case direction.SE
    34.                 PictureBox4.Top += 10
    35.                 PictureBox4.Left += 10
    36.             Case direction.S
    37.                 PictureBox4.Top += 10
    38.             Case direction.SW
    39.                 PictureBox4.Top += 10
    40.                 PictureBox4.Left -= 10
    41.             Case direction.W
    42.                 PictureBox4.Left -= 10
    43.             Case direction.NW
    44.                 PictureBox4.Top -= 10
    45.                 PictureBox4.Left -= 10
    46.         End Select
    47.  
    48.         Dim hit As PictureBox = objects.FirstOrDefault(Function(o) o.Bounds.IntersectsWith(PictureBox4.Bounds))
    49.         If hit IsNot Nothing Then
    50.             'PictureBox4 has collided with another picturebox
    51.             'change currentDirection
    52.         End If
    53.     End Sub
    54.  
    55. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width