collision checks

Please don't post Bullet support questions here, use the above forums instead.
Pushapjit
Posts: 2
Joined: Thu Jul 20, 2006 9:04 am
Location: Chandigarh

collision checks

Post by Pushapjit »

I am implementing BSP tree for organising 3d set of mesh polygons. tree compilation is ok. now i want to check collision of the 3d environment with the camera. my lineofsight() function is for ray/plane ray/polygon intersection. but the camera is still moving form walls. why is this so?



1)in lineofsight(), first i find the ray plane intersection details using getIntersect().
2) in lineofsight(), it's meant to allow the camera to cross
the polygon when going from behind to the front of the polygon but not when
going from the front to behind.
3) pointInPolygon is used to find whether point is in polygon or not
4)I have calculated the dot product and cross product of two 3d vector.

Am i going in right direction?
Code:


//-----------------------------------------------------------------------------
// Name : lineOfSight() (Recursive)
// Desc : Tests line of sight between two points.
//-----------------------------------------------------------------------------
bool My3DApplication::lineOfSight( D3DXVECTOR3 *Start, D3DXVECTOR3 *End, NODE *Node )
{
D3DXVECTOR3 intersection;
float temp = 0.0f;
bool flag;

if( Node->bIsLeaf )
return true;

int PointA = classifyPoint(Start, Node->Splitter);
int PointB = classifyPoint(End, Node->Splitter);

// Check if only in front or on polygon
if( PointA != POINTPOSITION_BACK && PointB != POINTPOSITION_BACK )
return lineOfSight(Start,End,Node->Front);

// check if only in back
if( PointA != POINTPOSITION_FRONT && PointB != POINTPOSITION_BACK )
return lineOfSight(Start,End,Node->Back);

// Crossing polygon, need intersection point
getIntersect(Start, End, (D3DXVECTOR3 *)&Node->Splitter->VertexList[0],
&Node->Splitter->Normal, &intersection, &temp);

if( PointA == POINTPOSITION_FRONT )
{
// Crossing from front to back.
// return lineOfSight( Start, &intersection, Node->Front ) ||

// Don't allow crossing polygon from front to back.
//check whether point is in polygon
flag = pointInPolygon( &intersection, (D3DXVECTOR3 *)&Node->Splitter->VertexList[0],
Node->Splitter->nNumberOfVertices);
if(flag == true)
return false;
lineOfSight( &intersection, End, Node->Back );
} // End of if

else
{
// Crossing from back to front.
return lineOfSight( Start, &intersection, Node->Back ) ||
lineOfSight( &intersection, End, Node->Front );
}
}




//---------------------------------------------------------------------------
//Name:pointInPolygon
//Desc:
//---------------------------------------------------------------------------
bool My3DApplication::pointInPolygon(D3DXVECTOR3 *point, D3DXVECTOR3 *vertices, int numVertices)
{
D3DXVECTOR3 p = cross(vertices[numVertices-1] - (*point), vertices[0] - (*point));
for (int i = 0; i < numVertices - 1; i++)
{
D3DXVECTOR3 q = cross(vertices - (*point), vertices[i+1] - (*point));
if (dot(p, q) < 0)
// Point not in polygon
return false;
}
return true;
}

//-----------------------------------------------------------------------------
//Name:dot
//Desc:Determines the dot-product of two 3-D vectors
//-----------------------------------------------------------------------------
inline float dot(D3DXVECTOR3& v1, D3DXVECTOR3& v2)
{
return ( v1.x * v2.x + v1.y * v2.y + v1.z * v2.z );
}

//-----------------------------------------------------------------------------
//Name:cross
//Desc: Determines the cross-product of two 3-D vectors.
//-----------------------------------------------------------------------------
inline D3DXVECTOR3 cross (D3DXVECTOR3 v1, D3DXVECTOR3 v2)
{
D3DXVECTOR3 result;

result.x = (v1.y * v2.z) - (v2.y * v1.z);
result.y = (v1.z * v2.x) - (v2.z * v1.x);
result.z = (v1.x * v2.y) - (v2.x * v1.y);

return (result);
}


//-----------------------------------------------------------------------------
// Name : getIntersect
// Desc : Get the ray / plane intersection details
//-----------------------------------------------------------------------------
bool My3DApplication::getIntersect( D3DXVECTOR3 *linestart, D3DXVECTOR3 *lineend,
D3DXVECTOR3 *vertex, D3DXVECTOR3 *normal,
D3DXVECTOR3 *intersection, float *percentage )
{
D3DXVECTOR3 direction;
D3DXVECTOR3 L1;
float linelength;
float dist_from_plane;

direction.x = lineend->x - linestart->x;
direction.y = lineend->y - linestart->y;
direction.z = lineend->z - linestart->z;

linelength = D3DXVec3Dot(&direction, normal);

if( fabsf( linelength ) < 0.001f )
return false;

L1.x = vertex->x - linestart->x;
L1.y = vertex->y - linestart->y;
L1.z = vertex->z - linestart->z;

dist_from_plane = D3DXVec3Dot(&L1, normal);

// How far from Linestart , intersection is as a percentage of 0 to 1
*percentage = dist_from_plane / linelength;

// The ray does not reach, or is in front of the plane
if( *percentage < 0.0f || *percentage > 1.0f )
return false;

// add the percentage of the line to line start
intersection->x = linestart->x + direction.x * (*percentage);
intersection->y = linestart->y + direction.y * (*percentage);
intersection->z = linestart->z + direction.z * (*percentage);
return true;
}