Advertisement

planes - what am I not doing right

Started by September 15, 2002 09:51 AM
7 comments, last by fatherjohn666 22 years, 5 months ago
As far as I know the following code:
    

struct CVector 
{
float x,y,z;
}

CVector calculateNormal(CVector vec[])
{
    CVector vector1;        // create a new vector

    CVector vector2;        // and another

    CVector vector3;        // one more...

    
    vector1 = subtract(vec[0],vec[1]);    // v1-v2

    vector2 = subtract(vec[1],vec[2]);    // v2-v3

    
    // now get the cross product (length)

    vector3 = crossProduct(vector1, vector2);
    
    // get the square root of the length

    float length = sqrt(    (vector3.x*vector3.x) +
                            (vector3.y*vector3.y) +
                            (vector3.z*vector3.z) 
                        );
    
    // now calculate the normal of this triangle!

    vector3.x /= length;                  
    vector3.y /= length;
    vector3.z /= length;
    
    // return the Normal!

    return vector3;
}

float dotProduct(CVector vec1, CVector vec2) 
{
			 //    (V1.x * V2.x        +        V1.y * V2.y        +        V1.z * V2.z)

	return ( (vec1.x * vec2.x) + (vec1.y * vec2.y) + (vec1.z * vec2.z) );
}

bool planeCollision(CVector vec[], CVector point)
{
    // vec[] is an array of vertices defining our triangle, point is a random point we are 

    // checking to see if it lies on our plane.

    // ooooh this is an evil equation, very evil, forget learning it as its TOO EVIL!

    
	CVector vector;	// for our "plane" normal

	
	vector = calculateNormal(vec);	// calculate our "planes" normal


	// calculate D from the plane equation

	// D = dot product of our planes normal and a point we know is on that plane...

	float D = -dotProduct(vector, point);		
	    
    // now we have 4 very EVIL values, now we check them in the equation with the vertices

    // of our random point (CVector point)

    // simply return the value, in the main function we check by asking - 

	// is there a collision? true or false? easy


    if( ((vector.x*point.x) + (vector.y*point.y) + (vector.z*point.z) + D) <= 0)
		return true;
	return false;
}


    
should produce me an answer. OK I am to believe: * get a normal of a triangle * ''-D'' = the dot product of this triangle AND a point we know exists on the triangle such as the first corner vertice. * with that we just then perform the Ax+By+Cz+D = 0 function and we get an answer..... Basically I have two triangles, one kinda sideways the other not. I check the plane of the sideways one and the corner point of the user moved one too see if I ''intersect'' that plane of the sideways one. If so, it will go red. But no, its still pretty colours. >8( I call the test with this: if( (planeCollision(triangle2,triangle1[0])) ) the two vectors I am playing with are two triangles of CVector structures: CVector triangle1[3] = { {-1, 0, 0}, {1, 0, 0}, {0, 1, 0} }; CVector triangle2[3] = { {-4, 0, -1},{-4, 0, 1}, {-4, 1, 0} }; man...im stuck....help from some of you lesser newbies would be extremely appreciated! CHEERS... PS: at least im thoroughly learning about vector maths Im just a beginner!!! http://www.actsofgord.com
~~~~~~~~~~~~~~~~~~~~~~~~~http://on.to/oni
A lesser newb is helping you:

quote:

float D = -dotProduct(vector, point);




there's your mistake...point is the point you are TRYING to check..not the one thats already on the triangle. Try using

float D = -dotProduct(vector, vec[0]); instead (or vec[1] or vec[2], it really doesnt matter which).

oh and,
quote:

vector1 = subtract(vec[0],vec[1]); // v1-v2
vector2 = subtract(vec[1],vec[2]); // v2-v3



works but its better to use
vector1 = subtract(vec[0],vec[1]);
vector2 = subtract(vec[2],vec[1]);
(direction should come out more consistant.)

oh, and what do you mean by "collision" of the point...your test seems incorrect...if the point is ON the plane then.

fabs(((vector.x*point.x) + (vector.y*point.y) + (vector.z*point.z) + D)) < tolerence (for some small real number tolerence)

what you are doing will test to see if point is on one of the sides of the triangle.


Salman.

[edited by - SporadicFire on September 15, 2002 11:23:49 AM]

[edited by - SporadicFire on September 15, 2002 11:24:31 AM]
Advertisement
Thanks for the reply mate!
HAILS!

ok my comments in the code are not correct

// calculate D from the plane equation // D = dot product of our planes normal and a point we know is on that plane... float D = -dotProduct(vector, point);

SHOULD BE

// calculate D from the plane equation // D = dot product of our planes normal and a random point...
float D = -dotProduct(vector, point);

when I call the function (planeCollision(triangle2[], triangle1[0]) ), i send in triangle 2[] (the sideways one on the left) and the right corner "point" of the user moved triangle....which then should be the first point to DISSECT the "plane" of the sideways triangle when moved left.

is that correct yes? -D(Normal of the triangle 2, point off triangle 1)....

OK.....

do we have to use FABS or an absolute integer/float?

I get strange results....perhaps I''ll go over my code again, one point at a time.

perhaps I get inaccuracy from my subtraction?

....frustrated....!

thanks so far for the help



Im just a beginner!!!

http://www.actsofgord.com
~~~~~~~~~~~~~~~~~~~~~~~~~http://on.to/oni
SporadicFire basically addressed both problems, so if you make those changes it should work. Also, in the last lines of planeCollision you are basically doing the dot product longhand between vector and point. You already wrote a dot product function, so use dotProduct(vector,point) + D instead.

Unless your cross product function is wrong, the code you have should work fine.
this is where im confused
Are youblokes saying that

float D = -dotProduct(Vector Normal, random point);

then if the random point is on the plane:
dotProduct(Vector Normal, random point) + D = 0

???? if this is right then I could type

if ( dotProduct(Vector Normal, random point) - dotProduct(Vector Normal, random point) == 0)
return true; // plane collision...


???this is confusing for me.

cheers guys

Im just a beginner!!!

http://www.actsofgord.com
~~~~~~~~~~~~~~~~~~~~~~~~~http://on.to/oni
Okay, I think I know where your confusion is. D is the distance from the plane to the origin , not the distance of the point we're checking to the plane. So to get the distance from the plane to the origin, you have to do dotProduct(normal, point_on_plane). When we later want to check to see if a point is on a plane (any random point for instance), we find the dot product between the normal and that random point and then subtract the distance (since D is negative in your code, you actually add it - it's basically the difference between subtracting a positive number and adding a negative number, same thing).

So this code:
if ( dotProduct(Vector Normal, random point) - dotProduct(Vector Normal, random point) == 0)return true; // plane collision...  

Would be correct, except we're not using a random point for one of them:
if ( dotProduct(Vector Normal, random point) - dotProduct(Vector Normal, Point on Plane) == 0)return true; // plane collision... 

That makes sense. Distance between point and normal minus distance between normal and origin; (Point - Normal) - (Plane - Normal) = Point - Plane; tada!

[edited by - Zipster on September 15, 2002 12:10:57 AM]
Advertisement
champion!

so although unable to complete while at work it is:

D = -dotProduct(normal, point on plane)

distance of point from plane = dotProduct(normal, random point) + D....

if that == 0 then we have a plane collision?

thus
if ( (dotProduct(normal, random point) + D) == 0)
return true;


please tell me that right....


Im just a beginner!!!

http://www.actsofgord.com
~~~~~~~~~~~~~~~~~~~~~~~~~http://on.to/oni
You got it! Although hopefully you''re not chosing a random point per se, but actually a real point you''d like to test
starting to see why I got certain results now...cant wait to test at home.



thanks heaps fellas - ONE DAY i''ll write quake 7

Im just a beginner!!!

http://www.actsofgord.com
~~~~~~~~~~~~~~~~~~~~~~~~~http://on.to/oni

This topic is closed to new replies.

Advertisement