# Pixel perfect collision / "terrain" destruction

• 09-09-2006
cboard_member
Pixel perfect collision / "terrain" destruction
In 2D. The project I (well, we) are working on right now has lost momentum - I'm really not in a position to do it. I'm still learning, lots and lots, pretty much every day.

Meantime, we're thinking about another small-ish project.

Worms clone / insprired by Worms.

Questions. How do / can you perform pixel perfect collision in OpenGL ? Obviously we'll want the terrain destruction-ness of the Worms series, I mean that's half the strategy gone if we decide against it, right (Worms fans) ?

Any other comments about our idea are welcome, of course. I'm going to leave checking the replies until morning. Tired. Yeah, it's 19:20 over here but I've been up since like 4am.

Bai :)
• 09-09-2006
VirtualAce
Is this 2D terrain or 3D?

For 2D you can find the startpoint and endpoint of the line and then use good old equation of a line to find X and Y. You can also make an equation for the line that represents your object's travel and then set the two line equations equal to each other and solve.

For 3D you find which triangle of the quad your object is in. Then you retrieve the 3 Y values of the triangle and do a bi linear interpolation, one interpolation on X and one on Y to determine the height of the ground at it's location.

Here is the code from my terrain system taken, in part, from a book I have. The hardest part is determining which triangle you are in. For voxels I used to do bilinear interpolation, but did not need to determine which triangle I was in which was easier.

Code:

```float CTerrain::GetHeightFromWorld(float x,float z) {   x=((float)m_iWorldWidth/2.0f)+x;   z=((float)m_iWorldDepth/2.0f)-z;     x/=(float)m_iCellSize;   z/=(float)m_iCellSize;     float col=floorf(x);    float row=floorf(z);     float A=GetHeight(row,col);   float B=GetHeight(row,col+1);   float C=GetHeight(row+1,col);   float D=GetHeight(row+1,col+1);     float dx=x-col;   float dz=z-row;     float height=0.0f;     if (dz<1.0f-dx)   {     float uy=B-A;     float vy=C-A;         height =A+Lerp(0.0f,uy,dx)+               Lerp(0.0f,vy,dz);   }   else   {     float uy=C-D;     float vy=B-D;         height =D+Lerp(0.0f,uy,1.0f-dx)+               Lerp(0.0f,vy,1.0f-dz);       }     return height; }```
And here is Lerp (inlined in my class header file):

Code:

```float Lerp(float a,float b,float t) {   return a-(a*t)+(b*t); }```
Note that neither of these take into account object speed, etc, etc. That requires a bit more.

EDIT:

Terrain destruction in 3D is not hard as long as you have no overhangs but it must fit into your render hierarchy which is not easy.