Results 1 to 5 of 5

Thread: Collision Detection

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Collision Detection

    Hmm, I don't know where to start here. This is my code for a moving button:


    If e.KeyCode = Keys.W Then
    Button1.Top = Button1.Top - 10

    ElseIf e.KeyCode = Keys.S Then
    Button1.Top = Button1.Top + 10

    ElseIf e.KeyCode = Keys.A Then
    Button1.Left = Button1.Left - 10

    ElseIf e.KeyCode = Keys.D Then
    Button1.Left = Button1.Left + 10


    How would I use the intersects/bounds thing a
    nd where would I put it? Would it go here?


    If e.KeyCode = Keys.W Then
    --here
    Button1.Top = Button1.Top - 10


    People have told me the commands but I don't know how to use them...

    Any help on collision detection would help :P

  2. #2
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Collision Detection

    Look at .Bounds.IntersectsWith()
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Re: Collision Detection

    Actaully I found it before you posted but when I get there I get stuck because I made it so if it is intersecting with button3 then the keypress won't work. I don't know how to get around it though :/ Heres the code:



    If e.KeyCode = Keys.W Then
    If (Button1.Bounds.IntersectsWith(Button3.Bounds) = False) Then
    Button1.Top = Button1.Top - 10

    ElseIf e.KeyCode = Keys.S Then
    If (Button1.Bounds.IntersectsWith(Button3.Bounds) = False) Then
    Button1.Top = Button1.Top + 10

    ElseIf e.KeyCode = Keys.A Then
    If (Button1.Bounds.IntersectsWith(Button3.Bounds) = False) Then
    Button1.Left = Button1.Left - 10

    ElseIf e.KeyCode = Keys.D Then
    If (Button1.Bounds.IntersectsWith(Button3.Bounds) = False) Then
    Button1.Left = Button1.Left + 10
    End If
    End If
    End If
    End If
    End If

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    43

    Re: Collision Detection

    Now I did this and it worked ok...but is there a better way?:





    If (Button1.Bounds.IntersectsWith(Button3.Bounds) = True) Then
    Button1.Top = Button1.Top + 1
    Else
    If e.KeyCode = Keys.W Then

    Button1.Top = Button1.Top - 10

    ElseIf e.KeyCode = Keys.S Then

    Button1.Top = Button1.Top + 10

    ElseIf e.KeyCode = Keys.A Then

    Button1.Left = Button1.Left - 10

    ElseIf e.KeyCode = Keys.D Then

    Button1.Left = Button1.Left + 10
    End If
    End If

  5. #5
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Collision Detection

    Well what if you move left into button3, then moving the control up one space won't do anything.

    If you want button3 to be an object that stops you from moving then I store the location of Button1 (the piece you are moving) at the beginning of that code. Then at the end, check for a collision after it has been moved. If there is a collision, then restore Button1 back to the position it was in, keeping it from moving into button3.

    Also you can clean up your move code by using a select case statement.

    Code:
    Select Case e.KeyCode
    Case Keys.W
    Button1.Top = Button1.Top - 10
    etc...
    End Case
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

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