Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- class Vector2D
- {
- public :
- // empty constructor
- Vector2D(){};
- // default constructor
- Vector2D(double p_x,double p_y):x(p_x),y(p_y)
- {
- // calculating the magnitude
- magnitude = sqrt((x*x) + (y*y));
- // calculating the normal of the vectors
- norm_x = x / magnitude;
- norm_y = y / magnitude;
- }
- // this is used to get the normal vector ( not normalized !! )
- Vector2D& perp()
- {
- Vector2D normal;
- // vector of (x,y) => (-y,x) or (y,-x) i'm using the first one
- normal = Vector2D(-(this->y),this->x);
- return normal;
- }
- // this is used to get the dot product ( for the projection )
- double dot(Vector2D& other)
- {
- double DotProduct;
- DotProduct = (this->x * other.x + this->y * other.y);
- return DotProduct;
- }
- // used to check if two vectors overlap
- bool overlap(Vector2D& other)
- {
- bool overlap = false;
- // getting this vector
- Vector2D offset = Vector2D(other.x - this->x,other.y - this->y);
- if (offset.magnitude < this->magnitude)
- overlap = true;
- return overlap;
- }
- // overloading assignment operators
- Vector2D& operator + (const Vector2D& other)
- {
- Vector2D resultant;
- resultant = Vector2D(other.x + this->x,other.y + this-> y);
- return resultant;
- }
- Vector2D& operator - (const Vector2D& other)
- {
- Vector2D resultant;
- resultant = Vector2D(other.x - this->x,other.y - this-> y);
- return resultant;
- }
- Vector2D& operator * (double factor)
- {
- Vector2D resultant;
- resultant = Vector2D(this->x * factor,this->y * factor);
- return resultant;
- }
- double magnitude;
- double x;
- double y;
- double norm_x;
- double norm_y;
- };
- class ConvexPolygon
- {
- public :
- // empty constructor
- ConvexPolygon(){};
- // default constructor
- ConvexPolygon(std::vector<Vector2D> list):vertices(list)
- {
- // calculating the edge vectors
- for (int i = 0; i < vertices.size(); i++)
- {
- // getting the current vertex :
- Vector2D p1 = vertices[i];
- // getting the next vertex :
- Vector2D p2 = vertices[i + 1 == vertices.size() ? 0 : i + 1];
- // getting the edge vector :
- Vector2D edge = p2 - p1;
- // getting the perpendicular :
- Vector2D normal = edge.perp();
- // pushing it back to the edges vector and the axes vector
- edges.push_back(edge);
- axes.push_back(normal);
- }
- }
- // used for projection, will return a reference to a Vector2D instance as projection
- Vector2D& proj(Vector2D& other)
- {
- // normalizing the vector for accurate precision
- Vector2D NormOther = Vector2D(other.norm_x,other.norm_y);
- // we get the projection with the min and max dot product
- double min = other.dot(axes[0]);
- double max = min;
- // looping through the vertices and doing the dot product
- for (int i = 0; i < vertices.size(); i++)
- {
- double p = NormOther.dot(vertices[i]);
- if (p < min)
- min = p;
- else if (p > max)
- max = p;
- }
- // creating the vector that will be then returned
- Vector2D proj = Vector2D(min,max);
- return proj;
- }
- std::vector<Vector2D> vertices;
- std::vector<Vector2D> edges;
- std::vector<Vector2D> axes;
- };
- bool SatCollisionCheck(ConvexPolygon& Poly1,ConvexPolygon& Poly2)
- {
- for (int i = 0; i < Poly1.axes.size(); i ++)
- {
- // projecting both shapes onto the axis
- Vector2D Proj1 = Poly1.proj(Poly1.axes[i]);
- Vector2D Proj2 = Poly2.proj(Poly1.axes[i]);
- // if the overlap will be returned false otherwise continue the loop
- std::cout << Proj1.x << "|" << Proj1.y << " : " << Proj2.x << "|" << Proj2.y << '\n';
- if (!Proj1.overlap(Proj2))
- return false;
- }
- return true;
- }
- /**
- APPLICATION ENTRY POINT
- **/
- int main()
- {
- // Polygon 1
- std::vector<Vector2D> Vertices1;
- Vertices1.push_back(Vector2D(0,0));
- Vertices1.push_back(Vector2D(10,0));
- Vertices1.push_back(Vector2D(10,20));
- Vertices1.push_back(Vector2D(0,10));
- // Polygon 2
- std::vector<Vector2D> Vertices2;
- Vertices2.push_back(Vector2D(9,19));
- Vertices2.push_back(Vector2D(27,576));
- Vertices2.push_back(Vector2D(400,500));
- Vertices2.push_back(Vector2D(9,150));
- // Creating two shapes from the std::vector of Vector2D
- ConvexPolygon Polygon1 = ConvexPolygon(Vertices1);
- ConvexPolygon Polygon2 = ConvexPolygon(Vertices2);
- // getting the collision
- bool collide = SatCollisionCheck(Polygon1,Polygon2);
- // printing it to the screen :
- std::cout << collide << std::endl;
- std::cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement