Results 1 to 17 of 17

Thread: Collision detection

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    11

    Question

    Okay - I need collision detetion. I know what my formula does, but I'm not exactly sure of the equation.

    Suppose you have two points, P1, and P2. Each has an X1, Y1, X2, and a Y2. Basically they're lines. I know if they're parallel or not, but if they aren't parallel, how can I find the point at which the two lines will interect? I just need the X and Y coordinates. Thanks.


    ________________________
    The Answer to Life, The Universe, And Everything...
    is...
    is...
    42.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Here's exactly what you need, i hope you understand the UDT's
    Code:
    Function ISCross(lne1 As Cline, lne2 As Cline, intersection As Coordinate) As Boolean
        Dim dx1!, dy1!, dx2!, dy2!, n!, s!
        dx1 = lne1.coord2.X - lne1.coord1.X
        dy1 = lne1.coord2.Y - lne1.coord1.Y
        dx1 = lne2.coord2.X - lne2.coord1.X
        dy2 = lne2.coord2.Y - lne2.coord1.Y
        If (dx2 * dy1 - dy2 * dx1) = 0 Then
            'they are parallell
            ISCross = False
            Exit Function
        End If
        
        n = (dx1 * (lne2.coord1.Y - lne1.coord1.Y) + dy1 * (lne1.coord1.X - lne2.coord1.X)) / (dx2 * dy1 - dy2 * dx1)
        t = (dx2 * (lne1.coord1.Y - lne2.coord1.Y) + dy2 * (lne2.coord1.X - lne1.coord1.X)) / (dy2 * dx1 - dx2 * dy1)
        ISCross = s >= 0 And s <= 1 And n >= 0 And n <= 1
        'the intersection is then:
        If ISCross Then intersection.X = X1 + t * dx: intersection.Y = Y1 + t * dy
    End Function
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Try this:

    Code:
    Function ISCross(lne1 As Cline, lne2 As Cline, intersection As Coordinate) As Boolean
        Dim dx1!, dy1!, dx2!, dy2!, n!, s!
        dx1 = lne1.coord2.X - lne1.coord1.X
        dy1 = lne1.coord2.Y - lne1.coord1.Y
        dx2 = lne2.coord2.X - lne2.coord1.X
        dy2 = lne2.coord2.Y - lne2.coord1.Y
        If (dx2 * dy1 - dy2 * dx1) = 0 Then
            'they are parallell
            ISCross = False
            Exit Function
        End If
        
        n = (dx1 * (lne2.coord1.Y - lne1.coord1.Y) + dy1 * (lne1.coord1.X - lne2.coord1.X)) / (dx2 * dy1 - dy2 * dx1)
        s = (dx2 * (lne1.coord1.Y - lne2.coord1.Y) + dy2 * (lne2.coord1.X - lne1.coord1.X)) / (dy2 * dx1 - dx2 * dy1)
        ISCross = s >= 0 And s <= 1 And n >= 0 And n <= 1
        'the intersection is then:
        If ISCross Then intersection.X = X1 + s * dx: intersection.Y = Y1 + s * dy
    End Function
    Just a couple of things that look like they're wrong chaged. Just typos.
    Harry.

    "From one thing, know ten thousand things."

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Red face

    Sorry about that.. Maybe got tired
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Yeah that was probably it
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457

    Question

    Hum...I think I once saw a function like that, but it consisted of an If with 4 conditions (not sure if this works). The guy who did it said it was the fastest method. It needed the type Rect (hope you know what I'm talking about!).

    Code:
    public function RectInRect(rRect1 as rect, rect2 as rect) as boolean
    
    if rect1.top < rect2.top and _
    rect1.left < rect2.left and _
    rect1.bottom > rect2.bottom and _
    rect1.right > rect3.right then RectInRect = True
    
    end function
    Of course, I'm not sure if it works. Give it a try.

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    11

    Question Uh...

    Thanks, but it's still not totally corrected...
    There are still some different variables, for example X1 suddenly pops up in the last line, and so does Y1, dx, and dy, and none of these have been used in an earlier equation.
    Code:
    Sub PostMessage(Message As String)
       On Error Goto Debug
       Forum.Messages.Post Message, Name
       Exit Sub
    Debug:
       For ctr = 1 To Bugs.Count
          Bugs(ctr).Squish
          If Bugs(ctr).Status <> vbDead Then
             Flamethrower.Target = Bug(ctr)
             Flamethrower.Burn
          End If
       Next ctr
    Resume
    End Sub

  8. #8
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    I think this is the function Jotaf is talking about

    Code:
    Declare Function IntersectRect Lib "user32" Alias "IntersectRect" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
    lpDestRect = Buffer to return the coordinates of the actual area of which lpSrc1Rect and lpSrc2Rect intersect, most likely not needed for what you are looking for
    lpSrc1Rect and lpSrc2Rect = The rectangles which define the two images you want detect if they collide

    You will have to run a loop to test all Images on the screen to see if they intersect.

    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Sorry about that (again)
    last line should be
    If ISCross Then intersection.X = lne1.coord2.X + s * dx1: intersection.Y = lne1.coord2.Y + s * dy1

    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    11

    Thumbs up Thanks

    Thanks, I have yet to test it, and I'll let you know if it works.
    I am not using the IntersectRect stuff because the things I am intersecting are not rectangular. So you don't need to tell me the rectangle things.
    Code:
    Sub PostMessage(Message As String)
       On Error Goto Debug
       Forum.Messages.Post Message, Name
       Exit Sub
    Debug:
       For ctr = 1 To Bugs.Count
          Bugs(ctr).Squish
          If Bugs(ctr).Status <> vbDead Then
             Flamethrower.Target = Bug(ctr)
             Flamethrower.Burn
          End If
       Next ctr
    Resume
    End Sub

  11. #11
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    You're right, you're not using rectangular objects, you're using square objects (joke)

    YoungBuck, thanks for the function! But I once saw it without the DLL... Wait, I now remember it:

    Code:
    If r1.Left > r2.Right And _
       r1.Left < r2.Right And _
       r1.Top > r2.Bottom And _
       r1.Top < r2.Bottom Then
           'Return True
    End If
    This is the exact code, give it a try! r1 is the first rect, r2 is the second one. If you want, I can explain the RECT type too, it's not very complex

  12. #12
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Oops, sometimes I'm soooo stupid: you said you were using lines and I completely forgot it, this one is for testing if an object is on top of the other one

  13. #13
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Just another thing, and sorry for posting this much:
    I'm just curious about where in agame intercecting 2 lines is needed...

  14. #14
    Lively Member Xero's Avatar
    Join Date
    Feb 2000
    Posts
    75

    Question

    I have another question about collision detection. I'm using IntersectRect for collision detection in a tile-based environment. Its using an array Map(#,#) = 0 or 1, 0 being water, 1 being grass. And i need to have it detect when the player is on a water(0) tile and not allow them to move over it. uhh.... help!

  15. #15
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    That one's easy!

    You have the player's coordinates: playerx and playery.

    When the player presses the key to move left, use this piece of code:

    Code:
    If Map(playerx - 1, playery) = tileGrass Then
        playerx = playerx - 1
    End If
    Same thing for right...

    Code:
    If Map(playerx + 1, playery) = tileGrass Then
        playerx = playerx + 1
    End If
    This one's for moving up:

    Code:
    If Map(playerx, playery - 1) = tileGrass Then
        playery = playery - 1
    End If
    And this one's for moving down:

    Code:
    If Map(playerx, playery + 1) = tileGrass Then
        playery = playery + 1
    End If
    That should do it! All you need now is to draw the player at the new coordinates!

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
    'your detection of keys goes here, and they should only set the offsets.
    if map(x+xoffset,y+yoffset)>0 then
     x=x+xoffset
     y=y+yoffset
    end if
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  17. #17
    Lively Member Xero's Avatar
    Join Date
    Feb 2000
    Posts
    75
    aaaah... mucho coolness. Thanks!

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