Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <SFML/Audio.hpp>
- #include <SFML/System.hpp>
- #include <iostream>
- struct Square
- {
- sf::Sprite Sprite;
- sf::Image Image;
- sf::String String;
- Square();
- };
- Square::Square()
- {
- Image.Create(100, 100, sf::Color(255,255,255));
- Sprite.SetImage(Image);
- String.SetFont(sf::Font::GetDefaultFont());
- }
- bool Intersects(sf::Sprite &One, sf::Sprite &Two);
- void Cycle_Bounding_Box(sf::Sprite &Host, sf::Sprite &Marker, int &Pos);
- int Pos = 0;
- int Collision_Test()
- {
- sf::RenderWindow App(sf::VideoMode().GetDesktopMode(), "Monotone", sf::Style::Fullscreen);
- App.UseVerticalSync(true);
- Square S1, S2, S3, S4, S5;
- S1.Image.Create(100, 100, sf::Color(255,0,0)); // Red Square
- S2.Image.Create(100, 100, sf::Color(0,255,0)); // Green Square
- S3.Image.Create(100, 100, sf::Color(0,0,255)); // Blue Square
- S4.Image.Create(200, 200, sf::Color(100,100,100)); // Grey Square
- S5.Image.Create(10, 10, sf::Color(255,255,255, 128)); // White Marker Square
- S1.Sprite.SetPosition(250, 250); // Left Bottom
- S2.Sprite.SetPosition(375, 250); // Right Bottom
- S3.Sprite.SetPosition(300, 300); // Center Top
- S4.Sprite.SetPosition(App.GetWidth()/2+200, App.GetHeight()/4); // Some-fucking-where
- S1.String.SetPosition(App.GetWidth()/2, App.GetHeight()/8);
- S2.String.SetPosition(App.GetWidth()/2, App.GetHeight()/6);
- bool Switched = false;
- while (App.IsOpened())
- {
- sf::Event Event;
- while (App.GetEvent(Event))
- {
- if (Event.Type == sf::Event::Closed)
- {
- App.Close();
- }
- if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
- {
- App.Close();
- }
- }
- const sf::Input& Input = App.GetInput();
- bool Space_Down = Input.IsKeyDown(sf::Key::Space);
- bool S_Down = Input.IsKeyDown(sf::Key::S);
- if (Space_Down)
- {
- if (Switched)
- {
- S1.Sprite.SetPosition(250, 250); // Left Bottom
- S2.Sprite.SetPosition(375, 250); // Right Bottom
- S3.Sprite.SetPosition(300, 300); // Center Top
- Switched = false;
- }
- else
- {
- S1.Sprite.SetPosition(100, 250); // Left Bottom
- S2.Sprite.SetPosition(500, 250); // Right Bottom
- S3.Sprite.SetPosition(300, 500); // Center Top
- Switched = true;
- }
- }
- if (S_Down)
- {
- Cycle_Bounding_Box(S4.Sprite, S5.Sprite, Pos);
- }
- // Collision Logic
- if (Intersects(S1.Sprite, S2.Sprite))
- {
- S1.String.SetText("Red is currently touching Green.");
- }
- else
- {
- S1.String.SetText("Red is not touching Green.");
- }
- if (Intersects(S3.Sprite, S1.Sprite))
- {
- S2.String.SetText("Blue is currently touching Red.");
- }
- else
- {
- S2.String.SetText("Blue is not touching Red.");
- }
- App.Clear();
- App.Draw(S1.Sprite);
- App.Draw(S2.Sprite);
- App.Draw(S3.Sprite);
- App.Draw(S4.Sprite);
- App.Draw(S5.Sprite);
- App.Draw(S1.String);
- App.Draw(S2.String);
- App.Display();
- }
- }
- bool Intersects(sf::Sprite &One, sf::Sprite &Two)
- {
- // Right Collision - Check to see if the right side of One is greater than or equal to the left side of Two
- if((One.GetPosition().x + One.GetSize().x) >= Two.GetPosition().x)
- {
- if(One.GetPosition().y <= (Two.GetPosition().y + Two.GetSize().y))
- {
- return true;
- }
- else if((One.GetPosition().y + One.GetSize().y) >= Two.GetPosition().y)
- {
- return true;
- }
- }
- // Left Collision - Check to see if the left side of One is lesser than or equal to the right side of Two
- else if(One.GetPosition().x <= (Two.GetPosition().x + Two.GetSize().x))
- {
- if(One.GetPosition().y <= (Two.GetPosition().y + Two.GetSize().y))
- {
- return true;
- }
- else if((One.GetPosition().y + One.GetSize().y) >= Two.GetPosition().y)
- {
- return true;
- }
- }
- // Top Collision - Check to see if the top side of One is lesser than or equal to the bottome side of Two
- else if(One.GetPosition().y <= (Two.GetPosition().y + Two.GetSize().y))
- {
- if((One.GetPosition().x + One.GetSize().x) >= Two.GetPosition().x)
- {
- return true;
- }
- else if(One.GetPosition().x <= (Two.GetPosition().x + Two.GetSize().x))
- {
- return true;
- }
- }
- // Bottom Collision - Check to see if the bottom side of One is greater than or equal to the top side of Two
- else if((One.GetPosition().y + One.GetSize().y) >= Two.GetPosition().y)
- {
- if((One.GetPosition().x + One.GetSize().x) >= Two.GetPosition().x)
- {
- return true;
- }
- else if(One.GetPosition().x <= (Two.GetPosition().x + Two.GetSize().x))
- {
- return true;
- }
- }
- else return false;
- }
- void Cycle_Bounding_Box(sf::Sprite &Host, sf::Sprite &Marker, int &Pos)
- {
- switch (Pos)
- {
- case 1:
- Pos++;
- Marker.SetPosition(Host.GetPosition().x+Host.GetSize().x, Host.GetPosition().y);
- break;
- case 2:
- Pos++;
- Marker.SetPosition(Host.GetPosition().x, Host.GetPosition().y+Host.GetSize().y);
- break;
- case 3:
- Pos++;
- Marker.SetPosition(Host.GetPosition().x+Host.GetSize().x, Host.GetPosition().y+Host.GetSize().y);
- break;
- case 4:
- Pos = 1;
- Marker.SetPosition(Host.GetPosition().x, Host.GetPosition().y);
- break;
- default:
- Pos = 1;
- Marker.SetPosition(Host.GetPosition().x+Host.GetSize().x, Host.GetPosition().y);
- break;
- }
- }
Add Comment
Please, Sign In to add comment