exodus122

Bitc

Aug 18th, 2025
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. def ray_plane_intersection(ray_origin, ray_dir, tri_vertices, tri_normal):
  2. denom = dot(tri_normal, ray_dir)
  3. if abs(denom) < 1e-6: # Ray parallel to plane
  4. return None
  5. t = dot(tri_normal, tri_vertices[0] - ray_origin) / denom
  6. if t < 0:
  7. return None # Intersection behind ray origin
  8. intersection = ray_origin + t * ray_dir
  9. return intersection
  10.  
  11. def point_in_triangle(p, A, B, C):
  12. # Use barycentric coordinates to check if p is inside triangle ABC
  13. v0 = C - A
  14. v1 = B - A
  15. v2 = p - A
  16.  
  17. dot00 = dot(v0, v0)
  18. dot01 = dot(v0, v1)
  19. dot02 = dot(v0, v2)
  20. dot11 = dot(v1, v1)
  21. dot12 = dot(v1, v2)
  22.  
  23. invDenom = 1 / (dot00 * dot11 - dot01 * dot01)
  24. u = (dot11 * dot02 - dot01 * dot12) * invDenom
  25. v = (dot00 * dot12 - dot01 * dot02) * invDenom
  26.  
  27. return (u >= 0) and (v >= 0) and (u + v <= 1)
  28.  
  29. def find_floor_clip(pos, vel, dt, floor_triangles):
  30. # Step 2 & 3
  31. x_new = pos.x + vel.x * dt
  32. z_new = pos.z + vel.z * dt
  33.  
  34. # Step 4
  35. ray_origin = Vector3(x_new, pos.y + 0.1, z_new)
  36. ray_dir = Vector3(0, -1, 0)
  37.  
  38. for tri in floor_triangles:
  39. intersection = ray_plane_intersection(ray_origin, ray_dir, tri.vertices, tri.normal)
  40. if intersection is not None:
  41. if point_in_triangle(intersection, *tri.vertices):
  42. # Check if intersection is on seam/gap edge (optional debug)
  43. if is_on_gap_edge(intersection, tri):
  44. return intersection
  45.  
  46. return None
  47.  
Advertisement
Add Comment
Please, Sign In to add comment