Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def check_intersection(x1, y1, x2, y2, x3, y3, x4, y4):
- # Compute denominator
- denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
- # If denominator is zero, lines are parallel or coincident
- if denominator == 0:
- return False
- # Compute t and u
- t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denominator
- u = ((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denominator
- # Check if intersection occurs within segment bounds
- return 0 <= t <= 1 and 0 <= u <= 1
- # Player movement segment
- x1, y1 = player.old_x, player.old_y
- x2, y2 = player.new_x, player.new_y
- crossed_border = False
- # Check against all border segments
- for border in border_list:
- x3, y3 = border.x1, border.y1
- x4, y4 = border.x2, border.y2
- if check_intersection(x1, y1, x2, y2, x3, y3, x4, y4):
- crossed_border = True
- break # Stop checking after first intersection
- # If crossed, revert movement
- if crossed_border:
- player.new_x = player.old_x
- player.new_y = player.old_y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement