#### Archived

This topic is now archived and is closed to further replies.

# Simple Collision problem

This topic is 6880 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

You can try putting a collision flag on your camera and not moving it until the view to the character is clear again. Then you can pan it around while testing to find a good view point. This should eliminate the shaking (as its not moving until the view is clear) and the move-through problem (its obviously finding the conflicting tri if its shaking).

Most likely you are moving back and forth in front of the tri and one of the calcs goes over the border for the tri moving towards it so it fails the collision test and you just keep moving.

-Geoff

##### Share on other sites
I'm having problems with my collision, i usually refrain from asking..but i'm a bit lost, if anyone could help.

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

##### Share on other sites
Are you using even numbers for the speed?

You keep moving the camera back with dir, a normalized vector(which obviously is equal to distance of one), so if speed is say 1.7 you collide, move back to 0.7 units from the original camera position and then on the next frame the speed of 1.7 + 0.7 = 2.4 from original camera. If that collides you end up at 0.4 from camera. Next frame 2.1, end up at 0.1 from original camera, etc, etc, and then you shake.

So in four frames the cameras jumped from (along the vector) 0.0, 0.7, 0.4, 0.1.

Hope that makes sense.

Maybe you could have the SPEED slow down if the camera would collide (dividing the speed by an arbitrary number) and then speed up(multipying by the same number) if doesn't collide until it reaches a maximum speed.

[This message has been edited by logistix (edited December 15, 1999).]

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632914
• Total Posts
3009192
• ### Who's Online (See full list)

There are no registered users currently online

×