(C#) Ray->box intersection

Miscellaneous Forums/General Discussion/(C#) Ray->box intersection

Hi!
I'm currently looking for a ray->box intersection function (in C#) for my raytracer.

This is my code so far, ported from elias_t's code from the code archives. it works but it doesn't return picked coordinates and normals. can anyone help me on with this?
thanks.

XYZ - Box coordinates
Width, Height, Depth - Box size
public bool Pick(double ray_x, double ray_y, double ray_z, double ray_nx, double ray_ny, double ray_nz, bool infinite)
		{
			double bminx = X - Width / 2;
			double bminy = Y - Height / 2;
			double bminz = Z - Depth / 2;
			double bmaxx = X + Width / 2;
			double bmaxy = Y + Height / 2;
			double bmaxz = Z + Depth / 2;
			double txmin = 0, txmax = 0, tymin = 0, tymax = 0, tzmin = 0, tzmax = 0;

			if (ray_nx >= 0)
			{
				txmin = (bminx - ray_x) / ray_nx;
				txmax = (bmaxx - ray_x) / ray_nx;
			}
			else
			{
				txmin = (bmaxx - ray_x) / ray_nx;
				txmax = (bminx - ray_x) / ray_nx;
			}
			if (ray_ny >= 0)
			{
				tymin = (bminy - ray_y) / ray_ny;
				tymax = (bmaxy - ray_y) / ray_ny;
			}
			else
			{
				tymin = (bmaxy - ray_y) / ray_ny;
				tymax = (bminy - ray_y) / ray_ny;
			}
			if (txmin > tymax || tymin > txmax) return false;
			if (tymin > txmin) txmin = tymin;
			if (tymax < txmax) txmax = tymax;
			if (ray_nz >= 0)
			{
				tzmin = (bminz - ray_z) / ray_nz;
				tzmax = (bmaxz - ray_z) / ray_nz;
			}
			else
			{
				tzmin = (bmaxz - ray_z) / ray_nz;
				tzmax = (bminz - ray_z) / ray_nz;
			}
			if (txmin > tzmax || tzmin > txmax) return false;

			Picker.PickedDis = Math.Sqrt((Picker.PickedX - ray_x) * (Picker.PickedX - ray_x) + (Picker.PickedY - ray_y) * (Picker.PickedY - ray_y) + (Picker.PickedZ - ray_z) * (Picker.PickedZ - ray_z));
			return true;
		}


Hello.

Because it's returning a Boolean?

Goodbye.

kidding me? this is a serious topic, not a joke.
no offense anyhow...
i guess a simple "Picker.PickedX = ....." would do it. but since i don't know the maths of that, well... I tried 20-30 pages found on google with no result.

Hello.

No joke, the function returns a boolean...and no offense taken.

it doesn't return picked coordinates and normals


That's what you asked for. Please note the commands that say 'Return true' and 'Return false' and the kinda giveaway 'public bool'.

If you'd said 'Why doesn't "picked" get populated with the correct values?' I wouldn't have answered at all (cos I wouldn't know). I was working on the basis that sometimes the most obvious mistakes can befuddle the smartest of us.

Goodbye.

thats actually not the problem. the problem is the maths here: how do i calculate the picked coordinates and normals.
i can store those 6 floats in an object of a class or as a global.