I've got a line in poly collision test(code below), which backs the camera up gently if it is colliding with the triangle.

I have two problems with this code:

1)When i touch the triangle, and keep moving forward, the camera shakes badly, depending on the speed i approached.

2)If i keep heading forwards, i eventually pass right through the triangle!

Which i can't understand, because i test the line(last_camera position to current camera position), so there is no way i could pass the triangle,

without the function noticing!!

My other two solutions are:

1)Just use camera=last_camera, on a collision..but this is far too inaccurate for fast objects

2)camera=intersection-dir; so my camera will always be the same distance away from the poly when it collides..

this would stop a jittery camera..?

I have included a very cut-down source code below(i thought it would just confuse matters adding the whole code..

as my problem is simple logic, and not dependant on much else in the engine)

If you need clarifacation on any bit, just post, and i will update.

/*Globals not shown here:

*camera=a D3DVECTOR holding the position of the camera

*dir= a normalized D3DVECTOR holding the camera direction

*speed= a float containing the camera speed

*triangle[]=a single triangle we are using for the collision test, it is an array of 3 D3DVECTORs

*/

void UpdateCamera(){

static D3DVECTOR last_camera=camera;//dummy value

D3DVECTOR intersection;//storage for intersection point

camera+=(dir*speed);//Update the camera position

/* Check if we have passed through a triangle, and if so..back up, until the line(last_camera->camera) is no

longer crossing the triangle*/

if(LinePlaneIntersect(&pPoint,last_camera,camera,triangle)){

while(LinePlaneIntersect(&intersection,last_camera,camera,triangle)){

camera-=dir;

}

return 1;

}

}

last_camera=camera;//update last_camera

/*Update screen etc*/

}

/**************************************************************************

A Line Plane Intersection function...

Parameter: Name should make it pretty self explanatory

***************************************************************************/

bool LinePlaneIntersect( D3DVECTOR *IntersecPoint,

D3DVECTOR LinePoint,

D3DVECTOR LineVector,

D3DVECTOR *Polygon)//3 vertices

{

//first we get the plain constant and normal

D3DVECTOR pnormal,pq, pr,pn,px; // Difference vectors

pq= Polygon[1]-Polygon[0] ;

pr= Polygon[2]-Polygon[0] ;

pnormal = CrossProduct( pq, pr);

float pconstent=DotProduct(pnormal,Polygon[0]);

//now we get the intersection point

float lambda = (pconstent - DotProduct(pnormal,LinePoint))

/(float)(DotProduct(pnormal,LineVector) + 1e-7);

IntersecPoint->x = LinePoint.x + lambda * LineVector.x;

IntersecPoint->y = LinePoint.y + lambda * LineVector.y;

IntersecPoint->z = LinePoint.z + lambda * LineVector.z;

//first we check if lamda is positive

if(lambda<0)return false;

float u0,u1,u2,v0,v1,v2,beta,alpha;//,xy,xz,yz;

//calculate the dominate Axis of Alignment

float x1=Polygon[1].x-Polygon[0].x;

float x2=Polygon[2].x-Polygon[0].x;

float y1=Polygon[1].y-Polygon[0].y;

float y2=Polygon[2].y-Polygon[0].y;

float z1=Polygon[1].z-Polygon[0].z;

float z2=Polygon[2].z-Polygon[0].z;

if(pnormal.x<0)pnormal.x*= -1;

if(pnormal.y<0)pnormal.y*= -1;

if(pnormal.z<0)pnormal.z*= -1;

if((pnormal.x<=pnormal.z)&&(pnormal.y<=pnormal.z)) {

u0=IntersecPoint->x-Polygon[0].x;

v0=IntersecPoint->y-Polygon[0].y;

u1=x1; u2=x2;

v1=y1; v2=y2;

}

else

if((pnormal.x<=pnormal.y)&&(pnormal.z<=pnormal.y)) {

u0=IntersecPoint->x-Polygon[0].x;

v0=IntersecPoint->z-Polygon[0].z;

u1=x1; u2=x2;

v1=z1; v2=z2;

}

else

{

u0=IntersecPoint->y-Polygon[0].y;

v0=IntersecPoint->z-Polygon[0].z;

u1=y1; u2=y2;

v1=z1; v2=z2;

}

if(u1==0)

{beta=u0/u2;

if((0<=beta)&&(beta<=1)) alpha =(v0-(beta *v2))/v1; }

else

{beta=(((v0*u1)-(u0*v1))/((v2*u1)-(u2*v1)));

if((0<=beta)&&(beta<=1)) alpha =(u0-(beta *u2))/u1; }

//if point is inside the poly then all condition will be met.

return (((alpha>=0)&&(beta>=0))&&((alpha+beta)<=1));

}