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