Sign in to follow this  

Collision detection problem

This topic is 3461 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

I've been writing a game in C++ but i've a problem with collision detection. My code works when I load a model of a box but it doesn't work with anything more complex. I translated my variablenames to english (they were dutch) and added comments in the part of my program below. If you want to see the whole program(with dutch variable names and just a few comments that are also in Dutch) just post a reaction and I will host it somewhere. I've got al the equations from this site: [url=http://www.geocities.com/SiliconValley/2151/math3d.html]basic 3d math[/url] I used the 3th solution from this site in my code: [url=http://local.wasp.uwa.edu.au/~pbourke//geometry/insidepoly/]Determining if a point lies on the interior of a polygon[/url]
struct vector3d // I've made a class with overloaded operators for this. But to keep the code short I've made it a struct. I do use those operators in my code.
{
  float x, y, z;
};

vector3d crossproduct (vector3d temp, vector3d temp2)
{
  vector3d returntemp;
  returntemp.x = temp2.y * temp.z - temp2.z * temp.y ;
  returntemp.y = temp2.z * temp.x - temp2.x * temp.z ;
  returntemp.z = temp2.x * temp.y - temp2.y * temp.x ;
  return returntemp;
}

float dotproduct (vector3d temp, vector3d temp2)
{
  return (temp.x * temp2.x + temp.y * temp2.y + temp.z * temp2.z);
}

class plane3d
{
  public:
  vector3d normal;
  float k;
  
  float sideofplane(vector3d punt)
  {
    return (dotproduct( punt, normaal )-k);
  }
  
  void computeplane (vector3d point0, vector3d point1, vector3d point2) // compute a plane out of three points
  {
    normal = crossproduct( point0-point1, point2-point1 );
    k = dotproduct( normal, point0 );
  }
};
  ////////////////////////////////////
  //        Read this first:        //
  ////////////////////////////////////
   In the complete program there's a class named object. It loads the data from a .obj file into an array with vertices(of the type vector3d) and an array of triangles(the class below)
   The class triangle3d contains:
   3 corners. These are references to the array wtih the vertices. All functions that use the array of vertices have a pointer to this array as an argument. It's always named vertices.
   If I want to use the x coordinate of the first corner I do it this way:
       vertices[corner[0]].x

class triangle3d : public plane3d
{
  public:
  signed int corners[3];
  
  void computeplane (vector3d* vertices)
  {
    normal = crossproduct( vertices[corners[0]]-vertices[corners[1]], vertices[corners[2]]-vertices[corners[1]] );
    k = dotproduct( normaal, vertices[corners[0]] );
  }

  ///////////////////////////////////////////////////////////////////
  //        Here starts my line triangle intersection test.        //
  ///////////////////////////////////////////////////////////////////

  bool linecrossestriangle (vector3d begin, vector3d direction, vector3d* vertices)
  {
    if (dotproduct(direction, normal)==0)			// check if the triangle(its plane) and the direction are paralel
    {
      return false;						// in that case the line doesn't cross the triangle
    }
    float u = (k - dotproduct(begin,  normaal)) / dotproduct(beweging, normaal); // Compute u. The intersectionpoint can be found by multiplying the direction by u and adding that to the begin point
    //int(u) is the amount of frames it will take to collide if you keep moving that way. When int(u)=0 you will collide with the plane of the triangle this frame.

    if (u >= 1) // if u>1 you won't collide in this frame
    {
      return false;
    }
    if (u < 0) // if u<0 you're moving away from the triangle
    {
      return false;
    }
    
    vector3d point = begin + beweging*u; // compute the intersection point between the line and the plane of the triangle
    
    plane3d sideplane[3]; // in the code below the computer will create planes out of the lines of the triangle.
			  // It uses 2 corners and the first corner plus the normal of the plane of the triangle

    sideplane[0].computeplane( vertices[corners[0]], vertices[corners[1]], vertices[corners[0]]+normal );
    sideplane[1].computeplane( vertices[corners[1]], vertices[corners[2]], vertices[corners[1]]+normal );
    sideplane[2].computeplane( vertices[corners[2]], vertices[corners[0]], vertices[corners[2]]+normal );
    
                          // If the point is at the same side of those three planes it's inside the triangle. And so the triangle and the line cross each other.
    if ( ( sideplane[0].sideofplane(point) >= 0 && sideplane[1].sideofplane(point) >= 0 && sideplane[2].sideofplane(point) >= 0 )
       ||( sideplane[0].sideofplane(point) <= 0 && sideplane[1].sideofplane(point) <= 0 && sideplane[2].sideofplane(point) <= 0 ) )
    {
      return true;	  // the line and the triangle dross each other. Return true
    }
    else
    {
      return false;
    }
  }
};

//Thank you for looking at my code.
//Sorry for my bad english...
//If you want to see the complete program just post a reaction.
[Edited by - d1rk on June 26, 2008 9:18:12 AM]

Share this post


Link to post
Share on other sites

This topic is 3461 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this