Advertisement
Guest User

Untitled

a guest
Jan 31st, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. def check_intersection(x1, y1, x2, y2, x3, y3, x4, y4):
  2. # Compute denominator
  3. denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
  4.  
  5. # If denominator is zero, lines are parallel or coincident
  6. if denominator == 0:
  7. return False
  8.  
  9. # Compute t and u
  10. t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denominator
  11. u = ((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denominator
  12.  
  13. # Check if intersection occurs within segment bounds
  14. return 0 <= t <= 1 and 0 <= u <= 1
  15.  
  16. # Player movement segment
  17. x1, y1 = player.old_x, player.old_y
  18. x2, y2 = player.new_x, player.new_y
  19.  
  20. crossed_border = False
  21.  
  22. # Check against all border segments
  23. for border in border_list:
  24. x3, y3 = border.x1, border.y1
  25. x4, y4 = border.x2, border.y2
  26.  
  27. if check_intersection(x1, y1, x2, y2, x3, y3, x4, y4):
  28. crossed_border = True
  29. break # Stop checking after first intersection
  30.  
  31. # If crossed, revert movement
  32. if crossed_border:
  33. player.new_x = player.old_x
  34. player.new_y = player.old_y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement