# a vector question

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 );
``````

Update : it seems to be working now but the normal for that plane data is calculated as (0, 1, 0) not (0, -1, 0). Is that what it should be?

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 );

for these values the normal should be (0, -1, 0). this normal should be calculated as the cross product: (plane - plane) X (plane - plane)…then normalized. that should give you (0, -1, 0).

i use the right-hand rule: using your right hand, curl your fingers in the direction corresponding to your vertex ordering and your thumb will be pointing along the normal.

b

Originally posted by coredump:
[b][quote]

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 );

for these values the normal should be (0, -1, 0). this normal should be calculated as the cross product: (plane - plane) X (plane - plane)…then normalized. that should give you (0, -1, 0).

i use the right-hand rule: using your right hand, curl your fingers in the direction corresponding to your vertex ordering and your thumb will be pointing along the normal.

b[/b][/QUOTE]

I have changed my function to te values you suggest and instantly my normal is pointing in the correct direction. Why does it matter which order they are specified in when calculating the normal? I was using this before:
cross( plane[ 2 ] - plane[ 1 ], plane[ 1 ] - plane[ 0 ] );

the right hand rule can be applied here too. subtracting plane from plane creates a directional vector from plane to plane. we’ll call this V1. similarly plane - plane creates a vector V2 from plane to plane. so now, you have 2 vectors coming off of plane. the cross product of two vectors yeilds a third vector orthogonal to the first 2. using your right hand, point your fingers in the direction of V1 and curl them towards V2. your thumb is pointing in the direction of the normal (the result of V1 X V2). notice how the normal is flipped when you cross V2 X V1. i’m sure doing a search on vector mathematics or cross product will result in tons of diagrams that may display this more clearly. good luck.

b