package game

import (
	rl "github.com/gen2brain/raylib-go/raylib"
)

func RayIntersectsBox(ray rl.Ray, boxMin, boxMax rl.Vector3) bool {
	tmin := (boxMin.X - ray.Position.X) / ray.Direction.X
	tmax := (boxMax.X - ray.Position.X) / ray.Direction.X

	if tmin > tmax {
		tmin, tmax = tmax, tmin
	}

	tymin := (boxMin.Z - ray.Position.Z) / ray.Direction.Z
	tymax := (boxMax.Z - ray.Position.Z) / ray.Direction.Z

	if tymin > tymax {
		tymin, tymax = tymax, tymin
	}

	if (tmin > tymax) || (tymin > tmax) {
		return false
	}

	if tymin > tmin {
		tmin = tymin
	}
	if tymax < tmax {
		tmax = tymax
	}

	tzmin := (boxMin.Y - ray.Position.Y) / ray.Direction.Y
	tzmax := (boxMax.Y - ray.Position.Y) / ray.Direction.Y

	if tzmin > tzmax {
		tzmin, tzmax = tzmax, tzmin
	}

	if (tmin > tzmax) || (tzmin > tmax) {
		return false
	}

	return true
}
