Esempio n. 1
0
//test for collisions and report the results. Also provide a 
//simple collision response to colliding objects.
void CollisionTestReportAndRespond(void)
{
    VCReport report;

    vc.Collide( &report );  //perform collision test.
                            //default is VC_FIRST_CONTACT
    int j;

    for (j = 0; j < report.numObjPairs(); j++)
    {
      std::cout << "Detected collision between objects "
           << report.obj1ID(j) <<" and "<< report.obj2ID(j) <<"\n";
  
      polyObject *p1 = GetObjectWithId(report.obj1ID(j));
      polyObject *p2 = GetObjectWithId(report.obj2ID(j));

      Vector normal;
	
      normal = p1->position - p2->position;
      normal.normalize();
      ComputeResponse(normal, (p1->v), &(p1->v) );
    
      normal = p2->position - p1->position;
      normal.normalize();
      ComputeResponse(normal, (p2->v), &(p2->v) );
      
    }

    SinkAndSource();
}
bool ColDetect::coldetectWithoutTransformation(Rectangle2D rect1, Rectangle2D rect2)
{
	VCReport report;
	VCollide vc;
	addPolygon(rect1.toTriangles(), vc);
	addPolygon(rect2.toTriangles(), vc);

	vc.Collide(&report);
	//return true in case of collision
	return report.numObjPairs() > 0;

}
Esempio n. 3
0
//test for collisions and report the results.
void CollisionTestAndReport(void)
{
    VCReport report;

    vc.Collide( &report );  //perform collision test.
                            //default is VC_FIRST_CONTACT
    int j;

    for (j = 0; j < report.numObjPairs(); j++)
    {
      cout << "Detected collision between objects "
           << report.obj1ID(j) <<" and "<< report.obj2ID(j) <<"\n";
    }
  
}
int coldetect(int ntri1, int ntri2, int ntrans1, int ntrans2, double *tri1, double *tri2, double *trans1, double *trans2) {
    double vc_trans[4][4];
    VCReport report;
    VCollide vc;
    addTris(tri1, ntri1, vc);
    addTris(tri2, ntri2, vc);
    // Iterate through transformations
    for (int t(0); t < ntrans1; t++) {
        update(trans1, t, ntrans1, vc_trans);
        vc.UpdateTrans(0, vc_trans);
        update(trans2, t, ntrans2, vc_trans);
        vc.UpdateTrans(1, vc_trans);
        vc.Collide(&report);
        if (report.numObjPairs() > 0) {
            return t + 1;
        }
    }
    return 0;
}
Esempio n. 5
0
int main(int argc, char *argv[])
{

  if (argc != 3)
    {
		std::cerr<<argv[0]<<": USAGE: "<<argv[0]<<" <input-file> <transformation-file>\n";
      exit(1);
    }
  
  int num_tri;
  VCollide vc;
  int id[NO_OF_OBJECTS];
  
  int i;
  for (i=0; i<NO_OF_OBJECTS; i++)  //add the objects to the library.
    {
      std::cout<<"Reading object "<<i<<"\n";
      vc.NewObject(&(id[i]));
      std::cout<<"Adding triangles\n";
      FILE *fp = fopen(argv[1], "r");
      fscanf(fp, "%d", &num_tri);
      
      for (int j=1; j<=num_tri; j++)
	{
	  double v1[3], v2[3], v3[3];
	  fscanf(fp, "%lf %lf %lf", &(v1[0]), &(v1[1]), &(v1[2]));
	  fscanf(fp, "%lf %lf %lf", &(v2[0]), &(v2[1]), &(v2[2]));
	  fscanf(fp, "%lf %lf %lf", &(v3[0]), &(v3[1]), &(v3[2]));
	  
	  vc.AddTri(v1, v2, v3, j);  // Each triangle has an id
	}
      
      fclose(fp);
      
      std::cout<<"Calling finish_object\n";
      vc.EndObject();
      
      
      std::cout<<"Inserted object "<<i<<"\n";
    }
  
  
  FILE *fp = fopen(argv[2], "r");
  
  for (i=1; i<=SIMULATION_STEPS; i++)  //perform the simulation.
    {
      std::cout<<"Simulation step : "<<i<<"\n";
      int j;
      for (j=0; j<NO_OF_OBJECTS; j++)
	{
	  double trans[4][4];
	  
	  //read in the transformation matrix.
	  fscanf(fp, "%lf", &(trans[0][0]));
	  fscanf(fp, "%lf", &(trans[0][1]));
	  fscanf(fp, "%lf", &(trans[0][2]));
	  fscanf(fp, "%lf", &(trans[0][3]));
	  
	  fscanf(fp, "%lf", &(trans[1][0]));
	  fscanf(fp, "%lf", &(trans[1][1]));
	  fscanf(fp, "%lf", &(trans[1][2]));
	  fscanf(fp, "%lf", &(trans[1][3]));
	  
	  fscanf(fp, "%lf", &(trans[2][0]));
	  fscanf(fp, "%lf", &(trans[2][1]));
	  fscanf(fp, "%lf", &(trans[2][2]));
	  fscanf(fp, "%lf", &(trans[2][3]));
	  
	  fscanf(fp, "%lf", &(trans[3][0]));
	  fscanf(fp, "%lf", &(trans[3][1]));
	  fscanf(fp, "%lf", &(trans[3][2]));
	  fscanf(fp, "%lf", &(trans[3][3]));
	  
	  //update the object's transformation.
	  vc.UpdateTrans(id[j], trans);
	}


      VCReport report;
            
      vc.Collide( &report, VC_ALL_CONTACTS);  //perform collision test.
                                              //default is VC_FIRST_CONTACT
      
      
      for (j = 0; j < report.numObjPairs(); j++)
      {
	std::cout << "Detected collision between objects "
             << report.obj1ID(j) << " and " << report.obj2ID(j) << "\n";

	std::cout << "\tNumber of contacts = " << report.numTriPairs(j) << "\n";
        std::cout << "\tColliding triangle-pairs: ";
	
        int k;
        for ( k = 0; k < report.numTriPairs(j); k++ )
          std::cout << "[" << report.tri1ID(j, k) << "," << report.tri2ID(j, k) << "] ";

        std::cout << "\n";
      }

    }

  return 0;
}