Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<cstdlib>
- #include<cstddef>
- #include<algorithm>
- // gets the size of an array
- template<typename T, size_t ARRAY_S>
- size_t size_(T(&)[ARRAY_S]){
- return ARRAY_S;
- }
- class Vector{
- public:
- double x,y;
- Vector(double x,double y){
- this->x=x;
- this->y=y;
- }
- // return a vector p orthogonal to q
- Vector ortho(){
- return Vector(y,-x);
- }
- };
- Vector *polyEdges(Vector[],int);
- Vector project(Vector[],Vector,int);
- bool isOverlap(Vector,Vector);
- // sat algorithm function
- bool isColiding(Vector poly1[],Vector poly2[],int poly1Size,int poly2Size){
- int axesSize=poly1Size+poly2Size;
- //edges of the 1st poly
- Vector *edge1=polyEdges(poly1,poly1Size);
- //edges of 2nd poly
- Vector *edge2= polyEdges(poly2,poly2Size);
- //holds all the edges of poly1&poly2
- Vector *edges= (Vector*)malloc((axesSize)*sizeof(Vector));
- Vector *Axis;
- // copy the value of the edges of poly one
- for(int i=0;i<poly1Size;i++){
- edges[i]=edge1[i];
- }
- // copy the values of the edges of poly 2
- for(int i =poly1Size;i<axesSize;i++){
- edges[i]=edge2[i];
- }
- Axis=(Vector*)malloc((axesSize)*sizeof(Vector));
- // get the orthogonal value of the current vector
- for(int i=0;i<axesSize;i++){
- Axis[i]=edges->ortho();
- }
- // are the axis colliding or not
- for(int i=0;i<axesSize;i++){
- if(isOverlap(project(poly1,Axis[i],poly1Size),project(poly2,Axis[i],poly2Size))){
- // return true if its overlapping
- return true;
- }}
- return false;
- }
- // function that returns a vector from a1 to a2
- Vector edgeVec(Vector a1, Vector a2){
- double x=a2.x-a1.x;
- double y=a2.y-a1.y;
- return Vector(x,y);
- }
- //function that return edges of a polygon as list vector
- Vector *polyEdges(Vector polygon[],int len){
- Vector *edges= (Vector*)malloc(len*sizeof(Vector));
- for(int i=0;i<len;i++){
- edges[i]=edgeVec(polygon[i],polygon[(i+1)%len]);
- }
- return edges;
- }
- // return the dot product of a1 and a2
- double dotPoduct(Vector a1, Vector a2){
- return (a1.x*a2.x)+(a1.y*a2.y);
- }
- //projection
- Vector project(Vector p[],Vector axis,int len){
- double dotP[len];
- double max_=INT_MAX;
- double min_=INT_MIN;
- // get the dot product
- for(int i=0;i<len;i++){
- dotP[i]=dotPoduct(p[i],axis);
- }
- //get the min and max value of the dotproduct
- max_=dotP[0];
- min_=dotP[0];
- for(int i=0;i<len;i++){
- if(dotP[i]<min_){
- min_=dotP[i];
- }
- if(dotP[i]>max_){
- max_=dotP[i];
- }}
- return Vector(min_,max_);
- }
- // return true if is the 2 vectors are overlapping
- bool isOverlap(Vector p1, Vector p2){
- return((p1.x<=p2.y)&&(p2.x<=p1.y));
- }
- int main()
- {
- double w=30;
- double h=30;
- double x=40;
- double y=40;
- double x2=90;
- double y2=90;
- Vector test1[]={Vector(x,y), Vector(x+w,y),Vector(x,y+h),Vector(x+w,y+h),Vector(x+w/2,y+h/2)};
- Vector test2[]={ Vector(x2,y2), Vector(x2+w,y2),Vector(x2,y2+h),Vector(x2+w,y2+h),Vector(x2+w,y2+h)};
- if(isColiding(test1,test2,size_(test1),size_(test2))){
- std::cout<<"Collision";
- }else{
- std::cout<<"No colision";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement