Results 1 to 10 of 10

Thread: [2005] Line Collision

  1. #1

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    [2005] Line Collision

    I want to know how to detect if a line is colliding or intersecting with another line.
    I don't know what angle the line is at etc... I only know the 'xstart', 'ystart', 'xend' and 'yend' of both lines.
    Anyone got any ideas?

    Thanks in advance, knxrb(again)
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  2. #2

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: [2005] Line Collision

    Bump
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

  4. #4

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: [2005] Line Collision

    Thanks but what do I put for the the a1, a2, b1 and b2 arguments?
    I presume the A1 and A2 arguments are angles. Are the B1 and B2 arguments how long the line is?
    At the moment my code is:
    Code:
    SegmentsIntersect(5,5,50,5,??,??,??,??)
    What are the bold arguments?
    Code:
    ' Return True if the segments intersect.
    Private Function SegmentsIntersect(ByVal X1 As Single, _
        ByVal Y1 As Single, ByVal X2 As Single, ByVal Y2 As _
        Single, ByVal A1 As Single, ByVal B1 As Single, ByVal _
        A2 As Single, ByVal B2 As Single) As Boolean
    Dim dx As Single
    Dim dy As Single
    Dim da As Single
    Dim db As Single
    Dim t As Single
    Dim s As Single
    
        dx = X2 - X1
        dy = Y2 - Y1
        da = A2 - A1
        db = B2 - B1
        If (da * dy - db * dx) = 0 Then
            ' The segments are parallel.
            SegmentsIntersect = False
            Exit Function
        End If
        
        s = (dx * (B1 - Y1) + dy * (X1 - A1)) / (da * dy - db * _
            dx)
        t = (da * (Y1 - B1) + db * (A1 - X1)) / (db * dx - da * _
            dy)
        SegmentsIntersect = (s >= 0# And s <= 1# And _
                             t >= 0# And t <= 1#)
    
        ' If it exists, the point of intersection is:
        ' (x1 + t * dx, y1 + t * dy)
    End Function
    Last edited by knxrb; Nov 27th, 2008 at 05:25 AM.
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Line Collision

    At a glance I'd guess that the Xs and Ys are the end points of one line and the As and Bs are the end points of the other.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: [2005] Line Collision

    With the arguments set to the end points of the lines it doesn't work:

    Code:
            e.Graphics.DrawLine(Pens.Black, 5, 5, 50, 50)
            e.Graphics.DrawLine(Pens.Black, 50, 5, 20, 80)
            e.Graphics.DrawString(SegmentsIntersect(5, 5, 50, 5, 50, 50, 20, 80), Me.Font, Brushes.Black, 50, 50)
    [EDIT]
    What I'm trying to do is get it so I can check if the direction the circle is heading in will make the circle collide with the square(see picture).

    [EDIT]
    Last edited by knxrb; Nov 27th, 2008 at 05:45 AM.
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [2005] Line Collision

    Why dont you use the values of the line, rather than hand written co-ords remove room for error. If that still fails can you post up your app. I may have time to take a look

    Pino

  8. #8

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: [2005] Line Collision

    Here's my project files:
    Attached Files Attached Files
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [2005] Line Collision

    Ok, can you give me the lowdown on what you are trying to achieve?

  10. #10

    Thread Starter
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    Re: [2005] Line Collision

    I've got a picturebox and I want it to be able to check if it is going to collide with something in the direction it is currently going in.
    If it does collide it will change direction until it finds somewhere where it will not collide but can still get past the object it was going to collide with.
    Did I help you with your problem? If I did rate me by clicking here: Rate knxrb

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