Thread: Pixel perfect collision / "terrain" destruction

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    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
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.
    Last edited by VirtualAce; 09-09-2006 at 08:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collision Detection Problems
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 12-17-2006, 03:25 PM
  2. 2D pixel perfect collision detection
    By Warlax in forum Game Programming
    Replies: 0
    Last Post: 06-22-2006, 07:39 AM
  3. Pixel perfect 2D collision detection with Allegro
    By Warlax in forum C++ Programming
    Replies: 1
    Last Post: 06-21-2006, 02:14 PM
  4. bounding box collision detection
    By DavidP in forum Game Programming
    Replies: 7
    Last Post: 07-07-2002, 11:43 PM
  5. Replies: 4
    Last Post: 05-03-2002, 09:40 PM