Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Simple Collision problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 15 December 1999 - 01:11 PM

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


Sponsor:

#2 ghowland   Members   -  Reputation: 134

Like
Likes
Like

Posted 14 December 1999 - 08:05 PM

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


#3 logistix   Members   -  Reputation: 122

Like
Likes
Like

Posted 15 December 1999 - 01:11 PM

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).]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS