Guest User

Untitled

a guest
Jan 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/Audio.hpp>
  3. #include <SFML/System.hpp>
  4. #include <iostream>
  5.  
  6. struct Square
  7. {
  8. sf::Sprite Sprite;
  9. sf::Image Image;
  10. sf::String String;
  11. Square();
  12. };
  13.  
  14. Square::Square()
  15. {
  16. Image.Create(100, 100, sf::Color(255,255,255));
  17. Sprite.SetImage(Image);
  18. String.SetFont(sf::Font::GetDefaultFont());
  19. }
  20.  
  21. bool Intersects(sf::Sprite &One, sf::Sprite &Two);
  22. void Cycle_Bounding_Box(sf::Sprite &Host, sf::Sprite &Marker, int &Pos);
  23. int Pos = 0;
  24.  
  25. int Collision_Test()
  26. {
  27. sf::RenderWindow App(sf::VideoMode().GetDesktopMode(), "Monotone", sf::Style::Fullscreen);
  28. App.UseVerticalSync(true);
  29.  
  30. Square S1, S2, S3, S4, S5;
  31. S1.Image.Create(100, 100, sf::Color(255,0,0)); // Red Square
  32. S2.Image.Create(100, 100, sf::Color(0,255,0)); // Green Square
  33. S3.Image.Create(100, 100, sf::Color(0,0,255)); // Blue Square
  34. S4.Image.Create(200, 200, sf::Color(100,100,100)); // Grey Square
  35. S5.Image.Create(10, 10, sf::Color(255,255,255, 128)); // White Marker Square
  36. S1.Sprite.SetPosition(250, 250); // Left Bottom
  37. S2.Sprite.SetPosition(375, 250); // Right Bottom
  38. S3.Sprite.SetPosition(300, 300); // Center Top
  39. S4.Sprite.SetPosition(App.GetWidth()/2+200, App.GetHeight()/4); // Some-fucking-where
  40. S1.String.SetPosition(App.GetWidth()/2, App.GetHeight()/8);
  41. S2.String.SetPosition(App.GetWidth()/2, App.GetHeight()/6);
  42. bool Switched = false;
  43. while (App.IsOpened())
  44. {
  45. sf::Event Event;
  46. while (App.GetEvent(Event))
  47. {
  48. if (Event.Type == sf::Event::Closed)
  49. {
  50. App.Close();
  51. }
  52.  
  53. if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
  54. {
  55. App.Close();
  56. }
  57. }
  58.  
  59. const sf::Input& Input = App.GetInput();
  60. bool Space_Down = Input.IsKeyDown(sf::Key::Space);
  61. bool S_Down = Input.IsKeyDown(sf::Key::S);
  62. if (Space_Down)
  63. {
  64. if (Switched)
  65. {
  66. S1.Sprite.SetPosition(250, 250); // Left Bottom
  67. S2.Sprite.SetPosition(375, 250); // Right Bottom
  68. S3.Sprite.SetPosition(300, 300); // Center Top
  69. Switched = false;
  70. }
  71. else
  72. {
  73. S1.Sprite.SetPosition(100, 250); // Left Bottom
  74. S2.Sprite.SetPosition(500, 250); // Right Bottom
  75. S3.Sprite.SetPosition(300, 500); // Center Top
  76. Switched = true;
  77. }
  78. }
  79. if (S_Down)
  80. {
  81. Cycle_Bounding_Box(S4.Sprite, S5.Sprite, Pos);
  82. }
  83.  
  84. // Collision Logic
  85. if (Intersects(S1.Sprite, S2.Sprite))
  86. {
  87. S1.String.SetText("Red is currently touching Green.");
  88. }
  89. else
  90. {
  91. S1.String.SetText("Red is not touching Green.");
  92. }
  93. if (Intersects(S3.Sprite, S1.Sprite))
  94. {
  95. S2.String.SetText("Blue is currently touching Red.");
  96. }
  97. else
  98. {
  99. S2.String.SetText("Blue is not touching Red.");
  100. }
  101.  
  102. App.Clear();
  103. App.Draw(S1.Sprite);
  104. App.Draw(S2.Sprite);
  105. App.Draw(S3.Sprite);
  106. App.Draw(S4.Sprite);
  107. App.Draw(S5.Sprite);
  108. App.Draw(S1.String);
  109. App.Draw(S2.String);
  110. App.Display();
  111. }
  112. }
  113.  
  114. bool Intersects(sf::Sprite &One, sf::Sprite &Two)
  115. {
  116. // Right Collision - Check to see if the right side of One is greater than or equal to the left side of Two
  117. if((One.GetPosition().x + One.GetSize().x) >= Two.GetPosition().x)
  118. {
  119. if(One.GetPosition().y <= (Two.GetPosition().y + Two.GetSize().y))
  120. {
  121. return true;
  122. }
  123. else if((One.GetPosition().y + One.GetSize().y) >= Two.GetPosition().y)
  124. {
  125. return true;
  126. }
  127. }
  128.  
  129. // Left Collision - Check to see if the left side of One is lesser than or equal to the right side of Two
  130. else if(One.GetPosition().x <= (Two.GetPosition().x + Two.GetSize().x))
  131. {
  132. if(One.GetPosition().y <= (Two.GetPosition().y + Two.GetSize().y))
  133. {
  134. return true;
  135. }
  136. else if((One.GetPosition().y + One.GetSize().y) >= Two.GetPosition().y)
  137. {
  138. return true;
  139. }
  140. }
  141.  
  142. // Top Collision - Check to see if the top side of One is lesser than or equal to the bottome side of Two
  143. else if(One.GetPosition().y <= (Two.GetPosition().y + Two.GetSize().y))
  144. {
  145. if((One.GetPosition().x + One.GetSize().x) >= Two.GetPosition().x)
  146. {
  147. return true;
  148. }
  149. else if(One.GetPosition().x <= (Two.GetPosition().x + Two.GetSize().x))
  150. {
  151. return true;
  152. }
  153. }
  154.  
  155. // Bottom Collision - Check to see if the bottom side of One is greater than or equal to the top side of Two
  156. else if((One.GetPosition().y + One.GetSize().y) >= Two.GetPosition().y)
  157. {
  158. if((One.GetPosition().x + One.GetSize().x) >= Two.GetPosition().x)
  159. {
  160. return true;
  161. }
  162. else if(One.GetPosition().x <= (Two.GetPosition().x + Two.GetSize().x))
  163. {
  164. return true;
  165. }
  166. }
  167.  
  168. else return false;
  169. }
  170.  
  171. void Cycle_Bounding_Box(sf::Sprite &Host, sf::Sprite &Marker, int &Pos)
  172. {
  173. switch (Pos)
  174. {
  175. case 1:
  176. Pos++;
  177. Marker.SetPosition(Host.GetPosition().x+Host.GetSize().x, Host.GetPosition().y);
  178. break;
  179. case 2:
  180. Pos++;
  181. Marker.SetPosition(Host.GetPosition().x, Host.GetPosition().y+Host.GetSize().y);
  182. break;
  183. case 3:
  184. Pos++;
  185. Marker.SetPosition(Host.GetPosition().x+Host.GetSize().x, Host.GetPosition().y+Host.GetSize().y);
  186. break;
  187. case 4:
  188. Pos = 1;
  189. Marker.SetPosition(Host.GetPosition().x, Host.GetPosition().y);
  190. break;
  191. default:
  192. Pos = 1;
  193. Marker.SetPosition(Host.GetPosition().x+Host.GetSize().x, Host.GetPosition().y);
  194. break;
  195. }
  196. }
Add Comment
Please, Sign In to add comment