Results 1 to 8 of 8

Thread: Collision detections with a background

  1. #1

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    Collision detections with a background

    I don't want to detect collisions with other sprites, just with certain areas of the background. What's the best way to do this? I am not using any form of DX.
    thanks in advance
    jmiller

  2. #2
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    Set up a coordinate array, and check to see if the coordinate is walkable. Something like this..

    VB Code:
    1. ' get a few global variables in here
    2. Private Map() As Long
    3. Private MapW As Long
    4. Private MapH As Long
    5.  
    6. Private Sub Form_Load()
    7.     ' set the map width and height
    8.     MapW& = 50
    9.     MapH& = 50
    10.    
    11.     ' redim the Map array to fit the size of the map
    12.     Redim Map(MapW& - 1, MapH& - 1) As Long
    13.    
    14.     ' make a few coords unwalkable
    15.     Map(5, 5) = 1
    16.     Map(5, 4) = 1
    17.     Map(5, 3) = 1
    18. End Sub
    19.  
    20. Private Sub MoveChar(X As Long, Y As Long)
    21.     ' check to see if coord can be walked upon
    22.     ' if not, then exit the movement function
    23.     If Map(X, Y) = 1 Then Exit Sub
    24.    
    25.     ' character movement code here..
    26. End Sub
    If you need more help, post up.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  3. #3

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    no that'll do nicely
    thanks a lot
    jmiller

  4. #4

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    on second thought...
    were you talking of actual pixels when you said 'coordinates'? i think what i want to do is use actual pixels, so movement is not confined to preset blocks of pixels. For simplicity, all the 'no-no spots' are going to be rectangles. would it be plausible to have rectangles of impassible areas for each map and then check all of them upon movement? The problem becomes a little more complicated when you consider that the users character is also a rectangle, and not just a pixel value. Speaking of that, how would one check for collision between two rectangles (I know how to check for a point and a rectangle)?
    also, correct me if i'm wrong, but isn't there an api call for collision detection. i think i remember hearing of one once.
    thanks
    jmiller

  5. #5
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    The API call is called IntersectRect, which asks for 2 RECT structs and returns 1, either 0,0,0,0 if no collision (I believe) or the area they collide in if they do collide. It's an invaluable API.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  6. #6

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    i'm confused about the rect_api type. it goes:
    Private Type RECT_API
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type
    going from the top-left corner of a rectangle clockwise, which variable applies to which corner?
    thanks

  7. #7

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    nevermind, i got it

  8. #8
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    Left is exactly what it means in VB: the X position
    Top is the same as in VB: the Y position
    Right is: the X position + width
    Bottom is: the Y position + height

    Edit: too fast
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

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