向量碰撞类---2D图形

来源:互联网 发布:志鸿优化系列丛书2016 编辑:程序博客网 时间:2024/05/20 19:28

        我设计的这个程序通过执行少量的数学向量运算来解决两个向量碰撞的问题。

        很希望这个程序能对那些正在进行2D图形程序设计并需要一个算法去解决两个对象碰撞的问题的朋友有用,这个程序并没有依靠著名的被许多图形对象定义的"bounding box"算法,你只需要简单的执行这个类来定义一组向量,无论何时图形对象被移动了,你都可以调用这些为对象设置了的命令。

        这是个仍然在检验的代码,但是多次的测试显示如果向量在一个点相遇或者交叉,那么向量通过比较就会显示他们已经相遇。

        这个类不处理对象内部的形态差异。如果一个对象完全处在了另一个对象的内部那么向量碰撞可能失败,然儿,这可能对那些要执行对象间跳跃的程序有用。

       

[纯文本代码]

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <math.h>
  4. #include <iomanip>
  5.  
  6. /*
  7.  *A Simple collision-detecting class
  8.  *Made by Alex Edwards, modified by Ancient Dragon
  9.  **/
  10. using namespace std;
  11.  
  12. class Point;
  13. class Triangle;
  14. class P2DVector;
  15.  
  16. class Point{    
  17.       private:
  18.              double xP;
  19.              double yP;
  20.  
  21.       public:
  22.              Point(double x, double y){
  23.                    xP = x;
  24.                    yP = y;
  25.              };
  26.              Point() {xP = 0; yP = 0;}
  27.              void Set(double x,double y) {xP = x; yP = y;}
  28.              double getX(){return xP;};
  29.              double getY(){return yP;};
  30.              void showCoord(){
  31.                   cout << "(" << getX() << ", " << getY() << ")" << endl;
  32.              };
  33. };
  34.  
  35. /*
  36.  *class P2DVector
  37.  *Named P2DVector to differentiate between the vector class and 3D vectors.
  38.  **/
  39. class P2DVector{
  40.       private:
  41.              Point points[2];
  42.  
  43.       public:
  44.              P2DVector(Point& first, Point& second){
  45.                       points[0] = first;
  46.                       points[1] = second;       
  47.              };
  48.              P2DVector() {points[0] = Point(0,0); points[1] = Point(0,0); }
  49.              void Set(Point& first, Point& second){
  50.                       points[0] = first;
  51.                       points[1] = second;       
  52.              };
  53.              double getXDir(){
  54.                     return (points[1].getX() - points[0].getX());
  55.              };
  56.              double getYDir(){
  57.                     return (points[1].getY() - points[0].getY());
  58.              };
  59.              double magnitude(){
  60.                     return sqrt( (pow( points[1].getX() - points[0].getX() ,2)
  61.                                  +pow(points[1].getY() - points[0].getY() ,2)));
  62.              };
  63.              Point startPoint(){
  64.                    Point p(points[0].getX(), points[0].getY());
  65.                    return p;
  66.              };
  67.              Point endPoint(){
  68.                    Point p(points[1].getX(), points[1].getY());
  69.                    return p;
  70.              };
  71.              P2DVector unitP2DVector(){     
  72.                     Point unitPoint[2];
  73.                     unitPoint[0].Set(points[0].getX() / magnitude(),
  74.                                                points[0].getY() / magnitude());
  75.                     unitPoint[1].Set(points[1].getX() / magnitude(),
  76.                                                points[1].getY() / magnitude());
  77.                     P2DVector temp (unitPoint[0], unitPoint[1]);
  78.                     return temp;
  79.              };
  80.              void displayLocation(){
  81.                   cout << "This P2DVector Starts-> "
  82.                           << ""
  83.                           << points[0].getX()
  84.                           << ", "
  85.                           << points[0].getY();
  86.                   cout << "/nEnds-] "
  87.                           << "/t/t/t"
  88.                           << points[1].getX()
  89.                           << ", "
  90.                           << points[1].getY();
  91.                   cout << "/nDirection: "
  92.                           << "/t/t"
  93.                           << "<"
  94.                           << getXDir()
  95.                           << ", "
  96.                           << getYDir()
  97.                           <<">";
  98.                   cout << "/nContains magnitude: /t"
  99.                           << magnitude()
  100.                           << "/n"
  101.                           << endl;
  102.              };
  103.              bool operator==(P2DVector &other){
  104.                   double otherXDirection = other.getXDir();
  105.                   double otherYDirection = other.getYDir();
  106.                   double xDirection = getXDir();
  107.                   double yDirection = getYDir();
  108.  
  109.                   //The statements below are a solution to a system
  110.                   //of equations for vector-to-vector collisions
  111.                   double time2 = ((other.startPoint().getY()-startPoint().getY())-
  112.                   (((other.getYDir())*((other.startPoint().getX())-
  113.                   (startPoint().getX())))/(other.getXDir())))/((getYDir())-
  114.                   ((other.getYDir())*((getXDir())/(other.getXDir()))));   
  115.  
  116.                   double time1 =  ((startPoint().getX() - other.startPoint().getX())
  117.                                   + ((getXDir()) * (time2)))/(other.getXDir());
  118.  
  119.                  return time1 >= 0 && time1 <=1 && time2 >= 0 && time2 <=1;
  120.              };
  121. };
  122.  
  123. /*
  124.  *A Sample shape that utilizes the vectors
  125.  **/
  126. class Triangle{
  127.       private:
  128.               Point coordinates[3], shortestCollision[3];
  129.               P2DVector innerVectors[3], outterVectors[6];
  130.  
  131.       public:
  132.              Triangle(double xLoc, double yLoc, Point points[3]){      
  133.                   Set(xLoc, yLoc, points);
  134.              };
  135.              void Set(double xLoc, double yLoc, Point points[3]){        
  136.                   for(int i = 0; i < 3; i++)
  137.                         coordinates[i].Set( xLoc + points[i].getX(),
  138.                                                       yLoc + points[i].getY());   
  139.  
  140.                   outterVectors[0].Set(coordinates[0], coordinates[1]);
  141.                   outterVectors[1].Set(coordinates[1], coordinates[2]);
  142.                   outterVectors[2].Set(coordinates[2], coordinates[0]);
  143.                   outterVectors[3].Set(coordinates[1], coordinates[0]);
  144.                   outterVectors[4].Set(coordinates[2], coordinates[1]);
  145.                   outterVectors[5].Set(coordinates[0], coordinates[2]);                 
  146.  
  147.                   shortestCollision[0].Set(outterVectors[1].startPoint().getX()
  148.                   + ((outterVectors[1].getXDir() ) / 2),
  149.                   outterVectors[1].startPoint().getY()
  150.                   + ((outterVectors[1].getYDir() ) / 2));
  151.                   innerVectors[0].Set(coordinates[0], shortestCollision[0]);
  152.  
  153.                   shortestCollision[1].Set(outterVectors[2].startPoint().getX()
  154.                   + ((outterVectors[2].getXDir() ) / 2),
  155.                   outterVectors[2].startPoint().getY()
  156.                   + ((outterVectors[2].getYDir() ) / 2));
  157.                   innerVectors[1].Set(coordinates[1], shortestCollision[1]);
  158.  
  159.                   shortestCollision[2].Set(outterVectors[0].startPoint().getX()
  160.                   + ((outterVectors[0].getXDir() ) / 2),
  161.                   outterVectors[0].startPoint().getY()
  162.                   + ((outterVectors[0].getYDir() ) / 2));
  163.                   innerVectors[2].Set(coordinates[2], shortestCollision[2]);                                
  164.              };            
  165.  
  166.              void displayParameters(){
  167.                   cout << "This triangle is defined by the points/vectors (in this order):/n"
  168.                           << flush;
  169.                   for(int i = 0; i < 3; i++){
  170.                           coordinates[i].showCoord();
  171.                           outterVectors[i].displayLocation();
  172.                           cout << "/n" << flush;
  173.                   }
  174.              };
  175.              Point *getPoints(){
  176.                    return coordinates;
  177.              };
  178.              P2DVector *getInnerVectors(){
  179.                    return innerVectors;
  180.              }
  181.              P2DVector *getOutterVectors(){
  182.                    return outterVectors;
  183.              }       
  184.              bool operator==(Triangle &other){
  185.                   P2DVector myVectors[] = {getOutterVectors()[0], getOutterVectors()[1],
  186.                                            getOutterVectors()[2], getOutterVectors()[3],
  187.                                            getOutterVectors()[4], getOutterVectors()[5]};                                          
  188.  
  189.                   P2DVector rogueVectors[] = {other.getOutterVectors()[0],
  190.                                            other.getOutterVectors()[1],
  191.                                            other.getOutterVectors()[2], other.getOutterVectors()[3],
  192.                                            other.getOutterVectors()[4], other.getOutterVectors()[5]};                                        
  193.  
  194.                   for(int i = 0; i < 6; i++){
  195.                           for(int j = 0; j < 6; j++){
  196.                                   if(myVectors[i] == rogueVectors[j]){
  197.                                       cout << "Intersection met!" << endl;
  198.                                       return true;
  199.                                   }
  200.                           }
  201.                   }
  202.                   cout << "No intersection met!" << endl;
  203.                   return false;
  204.              };                        
  205. };
  206.  
  207. /*
  208.  *Tests the shapes, which are triangles in this case, for a collision.
  209.  **/
  210. int main(int argc, char *argv[]){
  211.     Point myPoints[3], otherPoints[3], finalPoints[3];
  212.     myPoints[0].Set(0, 0), myPoints[1].Set(2, 2), myPoints[2].Set(2, 1);
  213.     otherPoints[0].Set(0, 0), otherPoints[1].Set(0, 1), otherPoints[2].Set(1, 0);
  214.     finalPoints[0].Set(0, 0), finalPoints[1].Set(1, 1), finalPoints[2].Set(1, 0);
  215.     Triangle tri(0, 2, myPoints), otherTri(1.5, 2, otherPoints), finalTri(2, 2, finalPoints);
  216.  
  217.     tri.displayParameters();
  218.     otherTri.displayParameters();
  219.     finalTri.displayParameters();
  220.  
  221.     //Checking for intersection between the first triangle with the second
  222.     (tri == otherTri);    
  223.     //Checking for intersection between the first triangle with the third
  224.     (tri == finalTri);   
  225.     //Checking for intersection between the secondtriangle with the third
  226.     (otherTri == finalTri);
  227.     cout << "/n" << endl;
  228.     finalTri.Set(1.5, 2, otherPoints);
  229.     cout << "Setting the 3rd triangle to same coords as 2nd.../n" << endl;
  230.  
  231.     //Checking for intersection between the first triangle with the second
  232.     (tri == otherTri);    
  233.     //Checking for intersection between the first triangle with the third
  234.     (tri == finalTri);    
  235.     //Checking for intersection between the secondtriangle with the third   
  236.     (otherTri == finalTri);
  237.     cin.get();
  238.     return 0;
  239. }
原创粉丝点击