Results 1 to 4 of 4

Thread: Collision Detection.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26

    Question Collision Detection.

    Anyone have any idea how to do collision detection? I sort of have one right now, but it takes MANY lines of code and is pretty burdensome. Not to mention I can only have PictureBox1 not be able to walk through PictureBox2, yet can still walk through PictureBox3. I don't know any DirectX yet so I'm just trying to make a simple RPG using PictureBoxes and things like that.

    I'd likje to be able to make it so that PictureBox1 can't walk through any other pictureboxes, but I don't know how hard that would be. Any help would be highly appreciated.

  2. #2
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    i'm afraid i can't answer your question yet as i haven't read the book but i would suggest it!!!


    http://www.amazon.co.uk/exec/obidos/...806050-2959007
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  3. #3
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    I can think of one way off the top of my head, which is not likely the best way (probably slow) - as you move a particular picturebox, you could loop through each of the other pictureboxes in whatever container (say a panel), examine their top/left/height/width and see if there is any overlap:

    VB Code:
    1. Dim pb As Control
    2. Dim bAllowMove As Boolean
    3. bAllowMove = True
    4.  
    5. For Each pb In Panel1.Controls
    6.     If GetType(pb).ToString = "System.Windows.Forms.PictureBox" Then
    7.         bAllowMove = CheckOverlap(pb)  ' a function elsewhere...
    8.     End If
    9. Next
    10.  
    11. If bAllowMove = True Then
    12.     ' Allow move...
    13. End If

    As I try to figure out how to say exactly how to check for overlap in VB I realize my skills are inadequate (it's harder than I gave it credit for). Here are a couple of discussions on the topic though:
    http://www.gamedev.net/reference/art...article754.asp
    http://www.gamedev.net/reference/lis...tegoryid=45#99
    http://www.gamedev.net/reference/art...article735.asp
    ^^ this actually has the code to do it in C, although it shouldn't be terribly hard to translate - object1->width in C corresponds to PictureBox.Width in VB.NET. For rectangular objects this seems to be the most accurate way, although as I said it isn't likely to be the fastest.

  4. #4
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    ps: the above loop has a small flaw - there's no check to see if you're looking at the character picturebox (you will of course be in collision with yourself). You could put some check like ... AND pb.Name <> "Foo".

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