lineintersectiongdscriptline-intersection

Check if two lines are intersecting? (if so, not where so)


I saw this asked but I couldn't understand the answers!

I got 4 vector2s, P1 & P2 for line 1, P3 & P4 for line 2. Code for intersection position works, but how do I check if that intersection is happening?

More specifically, I want to check what side of a polygon an imaginary line is passing through/colliding with

...

...Had something like it working in an old test-script, however I made no annotations and I can't adapt it. I don't know if there's anything here that could be used but thought I'd share:


    if rotation_angle > PI/2 && rotation_angle < 3*PI/2:
        if rad_overflow(($Position2D.position-position).angle()-PI/2) < rad_overflow(rotation_angle-PI/2) or rad_overflow(($Position2D.position-position).angle()-PI/2) > rad_overflow(rotation_angle+PI/2):
            actives.x = 1
        else:
            actives.x = 0
            
        
        if rad_overflow(($Position2D2.position-position).angle()-PI/2) < rad_overflow(rotation_angle-PI/2) or rad_overflow(($Position2D2.position-position).angle()-PI/2) > rad_overflow(rotation_angle+PI/2):
            actives.y = 1
        else:
            actives.y = 0
    
    
    else:
        if rad_overflow(($Position2D.position-position).angle()-PI/2) < rad_overflow(rotation_angle-PI/2) && rad_overflow(($Position2D.position-position).angle()-PI/2) > rad_overflow(rotation_angle+PI/2):
            actives.x = 1
        else:
            actives.x = 0
        
        if rad_overflow(($Position2D2.position-position).angle()-PI/2) < rad_overflow(rotation_angle-PI/2) && rad_overflow(($Position2D2.position-position).angle()-PI/2) > rad_overflow(rotation_angle+PI/2):
            actives.y = 1
        else:
            actives.y = 0

    var point1 = $Position2D.position
    var point2 = $Position2D2.position
    
    var limit3 = Vector2(0,1).rotated(rotation_angle+PI/2)
    var limit4 = Vector2(0,1).rotated(rotation_angle-PI/2)
    
    
    var det = (point1.x - point2.x)*(limit3.y - limit4.y) - (point1.y - point2.y)*(limit3.x - limit4.x)
    
    var new_position = Vector2(
    ((point1.x*point2.y - point1.y*point2.x) * (limit3.x-limit4.x)  -  (point1.x-point2.x) * (limit3.x*limit4.y - limit3.y*limit4.x))/det,
    ((point1.x*point2.y - point1.y*point2.x) * (limit3.y-limit4.y)  -  (point1.y-point2.y) * (limit3.x*limit4.y - limit3.y*limit4.x))/det)

    if actives.x != actives.y:
        print("hit")
    else:
        print("miss")

Solution

  • You ask:

    I got 4 vector2s, P1 & P2 for line 1, P3 & P4 for line 2. Code for intersection position works, but how do I check if that intersection is happening?

    If you are using Godot, you can use the Geometry class for this, in particular the line_intersects_line_2d method. I quote from the documentation:

    Variant line_intersects_line_2d ( Vector2 from_a, Vector2 dir_a, Vector2 from_b, Vector2 dir_b )

    Checks if the two lines (from_a, dir_a) and (from_b, dir_b) intersect. If yes, return the point of intersection as Vector2. If no intersection takes place, returns null.

    Note: The lines are specified using direction vectors, not end points.

    So that gives you both if they intersect (if it returns null they don't intersect) and where (if it does not return null it returns a Vector2 with the position of the intersection).