ray-plane intersection issue

Started by
2 comments, last by Eelco 19 years, 4 months ago
First, the code: The plane is initialized like this:

//v0-v2 are thee arbitrary points

normal = VecNormalize(VecCrossProduct(v2 - v0, v1 - v0));

A = v0.y * (v1.z - v2.z) + v1.y * (v2.z - v0.z) + v2.y * (v0.z - v1.z);
B = v0.z * (v1.x - v2.x) + v1.z * (v2.x - v0.x) + v2.z * (v0.x - v1.x);
C = v0.x * (v1.y - v2.y) + v1.x * (v2.y - v0.y) + v2.x * (v0.y - v1.y);
D = VecDotProduct(&-v0, &normal);

For: v0 = v(-256, 0, 0) v1 = v(-256, 256, 0) v2 = v(-256, 0, 256) the plane equation becomes: A = 65536 B = 0 C = 0 D = -256 Last time I remember Ax + By + Cz + D had to equal 0. Substituting D in the above code snippet with: D = -(v0.x * (v1.y * v2.z - v2.y * v1.z) + v1.x * (v2.y * v0.z - v0.y * v2.z) + v2.x * (v0.y * v1.z - v1.y * v0.z)); gives: D = 1.67772e+007 Not correct, I say. The second issue: ray-plane intersection code. Since D is correct (-256), the below code should work (but, you guessed it - it doesn't):

int IntersectRayWithPlane(TPlane plane, TVector3f origin, TVector3f direction, TIntersectionInfo * info)
{
	float dp = VecDotProduct(&plane.normal, &direction);

	if(info)
		(*info).point = TVector3f(0, 0, 0);

	//ray is parallel to plane
	if(fltcomp(dp, 0.f)) //floating point proximity test
		{
		if(info)
			{
			(*info).flag = 0;
			(*info).normal = plane.normal;
			(*info).point = origin;
			}

		return 0;
		}

	//if dp > 0, then plane points away from ray, otherwise points at plane

	float dp2 = -(VecDotProduct(&plane.normal, &origin) + plane.D);
	float t = dp2 / dp;

	//intersection point is in front of the ray -> calculate coordinates
	if(t > 0)
		{
		if(info)
			{
			(*info).flag = 1;
			(*info).point = TVector3f(origin.x + t * direction.x, origin.y + t * direction.y, origin.z + t * direction.z);
			(*info).normal = -plane.normal;	//?
			}

		return 1;
		}

	if(info)
		(*info).flag = -1;
        
        //don't care about the intersection point or normal

	return -1;
}

The intersection point is always wrong, even for major-axis orientated planes (the point simply doesn't end up on the plane). Admittedly, I am absolutely no good at maths (even more basic maths such as this), so cooking up the above has taken my more time than it ever should have and I'm rather annoyed that it's still not working the way I'd expect it to. And no - this is not a school assignment. I don't have maths at school. Can anyone look through the above code snippets and correct any errors I might have made?
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Advertisement
Quote:the plane equation becomes:

A = 65536
B = 0
C = 0
D = -256


I'm guessing that (A B C) should be normalized here... if it were

A = 1
B = 0
C = 0
D = -256

then it'd be correct.
Okay, thanks.

I take it you mean normalized as in v[A1, B1, C1] = normalize_vector(A, B, C)?

That won't however explain why the intersection doesn't work properly...

"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
why the whole messing with plane equations thing?

just use the planenormal.

then use the fact that all points on the plane multiplied with the planenormal yield zero, ie:

plane.normal.x*(plane.origin.x-x)+plane.normal.y*(plane.origin.y-y)+plane.normal.z*(plane.origin.z-z)=0

(x,y,z) is then the set of points lying on the plane. now we want the points that both lie on the plane aswell as the ray, so we give this formula as input the ray equation:

(x,y,z) = ray.origin.x+ray.direction.x*t,ray.origin.y+ray.direction.y*t,,ray.origin.z+ray.direction.z*t

plug it in, and solve for the value of t. you can then use that to find your point using the ray equation.

its not too hard to write out and solve on paper, but if you dont feel like it: the solution is all over the net.

This topic is closed to new replies.

Advertisement