# Thread: a vector question

1. ## 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...

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

2. ## Re: a vector question

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?

3. ## Re: a vector question

Code :
```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[1] - plane[0]) X (plane[2] - plane[0])...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

4. ## Re: a vector question

Originally posted by coredump:
Code :
```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[1] - plane[0]) X (plane[2] - plane[0])...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
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 ] );

5. ## Re: a vector question

the right hand rule can be applied here too. subtracting plane[0] from plane[1] creates a directional vector from plane[0] to plane[1]. we'll call this V1. similarly plane[2] - plane[0] creates a vector V2 from plane[0] to plane[2]. so now, you have 2 vectors coming off of plane[0]. 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•