Jump to content
  • Advertisement


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

Guest Anonymous Poster

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.

If you intended to correct an error in the post then please contact us.

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.


Share this post

Link to post
Share on other sites
Guest Anonymous Poster
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*/

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)) {
u1=x1; u2=x2;
v1=y1; v2=y2;
if((pnormal.x<=pnormal.y)&&(pnormal.z<=pnormal.y)) {
u1=x1; u2=x2;
v1=z1; v2=z2;
u1=y1; u2=y2;
v1=z1; v2=z2;

if((0<=beta)&&(beta<=1)) alpha =(v0-(beta *v2))/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 this post

Link to post
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).]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!