endo

04-25-2002, 08:16 AM

I am just beginning collision detection and have run into a problem with line/plane collisions. Does this code look to have any problems? I am a bit unsure about the calculation of the normal, it seems to be pointng the wrong way...

bool intersectPlane( Vector3d* poly, Vector3d* line )

{

float distance1 = 0, distance2 = 0;

Vector3d normal = crossProduct( (poly[ 2 ] - poly[ 1 ]), (poly[ 1 ] - poly[ 0 ]) );

normal.normalise( );

// ofstream out( "test.txt" );

// out << "normal : " << normal << endl;

float originDistance = planeDistance( normal, poly[ 0 ] );

//using the plane equation we can now get the 2 distances

//from the ends of the line and substituting originDistance for D

distance1 = ( (normal.triple[ X ] + line[ 0 ].triple[ X ]) +

(normal.triple[ Y ] + line[ 0 ].triple[ Y ]) +

(normal.triple[ Z ] + line[ 0 ].triple[ Z ]) ) + originDistance;

distance2 = ( (normal.triple[ X ] + line[ 1 ].triple[ X ]) +

(normal.triple[ Y ] + line[ 1 ].triple[ Y ]) +

(normal.triple[ Z ] + line[ 1 ].triple[ Z ]) ) + originDistance;

// out << "originDistance : " << originDistance << endl;

// out << "distance1 : " << distance1 << endl;

// out << "distance2 : " << distance2 << endl;

if( distance1 * distance2 >= 0 )

{

return false; //points must be on either side of the plane

}

else

{

return true; //points on the same side of plane

}

}

The plane data I am testing this on looks like this:

Vector3d plane[ 3 ]; //a/c normal = downwards

plane[ 0 ].setValues( 0, -50, 50 );

plane[ 1 ].setValues( -50, -50, -50 );

plane[ 2 ].setValues( 50, -50, -50 );

bool intersectPlane( Vector3d* poly, Vector3d* line )

{

float distance1 = 0, distance2 = 0;

Vector3d normal = crossProduct( (poly[ 2 ] - poly[ 1 ]), (poly[ 1 ] - poly[ 0 ]) );

normal.normalise( );

// ofstream out( "test.txt" );

// out << "normal : " << normal << endl;

float originDistance = planeDistance( normal, poly[ 0 ] );

//using the plane equation we can now get the 2 distances

//from the ends of the line and substituting originDistance for D

distance1 = ( (normal.triple[ X ] + line[ 0 ].triple[ X ]) +

(normal.triple[ Y ] + line[ 0 ].triple[ Y ]) +

(normal.triple[ Z ] + line[ 0 ].triple[ Z ]) ) + originDistance;

distance2 = ( (normal.triple[ X ] + line[ 1 ].triple[ X ]) +

(normal.triple[ Y ] + line[ 1 ].triple[ Y ]) +

(normal.triple[ Z ] + line[ 1 ].triple[ Z ]) ) + originDistance;

// out << "originDistance : " << originDistance << endl;

// out << "distance1 : " << distance1 << endl;

// out << "distance2 : " << distance2 << endl;

if( distance1 * distance2 >= 0 )

{

return false; //points must be on either side of the plane

}

else

{

return true; //points on the same side of plane

}

}

The plane data I am testing this on looks like this:

Vector3d plane[ 3 ]; //a/c normal = downwards

plane[ 0 ].setValues( 0, -50, 50 );

plane[ 1 ].setValues( -50, -50, -50 );

plane[ 2 ].setValues( 50, -50, -50 );