From 00a4f37a33ea5d39f300d4a3b4b7dd38448be759 Mon Sep 17 00:00:00 2001 From: Katajisto Date: Mon, 15 Sep 2025 22:19:46 +0300 Subject: [PATCH] improve ray plane collision check --- src/ray.jai | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/ray.jai b/src/ray.jai index 74c9acb..775b736 100644 --- a/src/ray.jai +++ b/src/ray.jai @@ -42,14 +42,13 @@ Ray_Collision :: struct { // At which 2D XZ point does a ray hit the ground plane at a height of // plane_height. Used for the level editor and maybe later the trile editor as well. -ray_plane_collision_point :: (ray: Ray, plane_height: float) -> Vector2 { - // @ToDo: This is stupid, but I don't feel like bothering to do this correctly right now... - collision_cube : Collision_Cube = .{ - .{-100000, plane_height, -100000}, - .{100000, plane_height + 0.001, 100000} - }; - collision := does_ray_hit_cube(ray, collision_cube); - return .{collision.point.x, collision.point.z}; +ray_plane_collision_point :: (ray: Ray, plane_height: float) -> (bool, Vector2) { + dist_to_plane := ray.origin.y - plane_height; + if ray.direction.y >= 0 then return false, .{0,0}; + multi := dist_to_plane / abs(ray.direction.y); + ray_to_plane := ray.direction * multi; + planePoint := ray_to_plane + ray.origin; + return true, .{planePoint.x, planePoint.z}; } // Ported over from Raylib.