Advertisement
Guest User

A simple Collision Detection Program in C++

a guest
Nov 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. using namespace std;
  4. #include <cmath>
  5.  
  6. class Vector2{
  7.     public:
  8.         float x, y;
  9.         Vector2();
  10.         Vector2(float a, float b);
  11.  
  12.         float getMagnitude();
  13.         Vector2 getNormalised();
  14.         void normalised();
  15.  
  16. };
  17.  
  18. Vector2::Vector2(){
  19.     x = 0;
  20.     y = 0;
  21. }
  22. Vector2::Vector2(float a, float b){
  23.     x = a;
  24.     y = b;
  25. }
  26. float Vector2::getMagnitude(){
  27.  
  28.     float m = (float)sqrtf(x*x+y*y);
  29.     return m;
  30.  
  31. }
  32.  
  33. void swapHeadings(Vector2 *a, Vector2 *b){
  34.  
  35.     Vector2 t = *a;
  36.     a->x = b->x;
  37.     a->y = b->y;
  38.  
  39.     b->x = t.x;
  40.     b->y = t.y;
  41. }
  42.  
  43. void doCollision(Vector2 *a, Vector2 *b, int r1, int r2, bool *collided){
  44.  
  45.     Vector2 dist(b->x-a->x, b->y-a->y);
  46.     if (dist.x < 0){
  47.         dist.x = dist.x*-1;
  48.  
  49.     }
  50.     if (dist.y < 0){
  51.         dist.y = dist.y*-1;
  52.     }
  53.  
  54.  
  55.     float magni = dist.getMagnitude();
  56.     if (r1+r2 >= magni){
  57.         *collided = true;
  58.     }
  59.     else if (r1+r2 < magni){
  60.         *collided = false;
  61.     }
  62.  
  63.  
  64. }
  65. int main(){
  66.  
  67.     sf::RenderWindow window{{640,480}, "Collision Test"};
  68.     window.setVerticalSyncEnabled(true);
  69.     //window.setFramerateLimit(60);
  70.     bool running = true;
  71.     bool collided = false;
  72.  
  73.     Vector2 v1(0, 100);
  74.     Vector2 v2(640, 100);
  75.  
  76.     Vector2 hV1(1,1);
  77.     Vector2 hV2(-1,1);
  78.     doCollision(&v1, &v2, 5, 5, &collided);
  79.     sf::Texture t;
  80.     if (!t.loadFromFile("icon.png")) return -1;
  81.     sf::Sprite spr{t};
  82.     spr.setPosition({100,100});
  83.     while (running){
  84.  
  85.         v1.x += hV1.x*2;
  86.         v1.y += hV1.y*2;
  87.  
  88.         v2.x += hV2.x*2;
  89.         v2.y += hV2.y*2;
  90.  
  91.         if (v1.x <= 0){
  92.             hV1.x = 1;
  93.  
  94.         }
  95.         if (v1.x >= 630){
  96.             hV1.x = -1;
  97.         }
  98.         if (v1.y <= 0){
  99.             hV1.y = 1;
  100.         }
  101.         if (v1.y >= 470){
  102.             hV1.y = -1;
  103.         }
  104.         if (v2.x <= 0){
  105.             hV2.x = 1;
  106.  
  107.         }
  108.         if (v2.x >= 630){
  109.             hV2.x = -1;
  110.         }
  111.         if (v2.y <= 0){
  112.             hV2.y = 1;
  113.         }
  114.         if (v2.y >= 470){
  115.             hV2.y = -1;
  116.         }
  117.         doCollision(&v2, &v1, 5, 5, &collided);
  118.  
  119.         cout<<"Collided: "<<collided<<endl;
  120.         if (collided){
  121.             swapHeadings(&hV1, &hV2);
  122.         }
  123.         sf::Event event;
  124.  
  125.         sf::RectangleShape rect1{{10,10}};
  126.         sf::RectangleShape rect2{{10,10}};
  127.  
  128.         rect1.setFillColor(sf::Color::White);
  129.         rect2.setFillColor(sf::Color::Red);
  130.  
  131.         rect1.setPosition({v1.x, v1.y});
  132.         rect2.setPosition({v2.x, v2.y});
  133.         while (window.pollEvent(event)){
  134.             if (event.type == sf::Event::Closed){
  135.                 running = false;
  136.             }
  137.         }
  138.  
  139.         window.clear();
  140.         window.draw(rect1);
  141.         window.draw(rect2);
  142.         //window.draw(spr);
  143.         window.display();
  144.     }
  145.     window.clear();
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement