Felanpro

Sloppy gravity with collision

Apr 18th, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <SFML/Graphics.hpp>
  4. #include <SFML/Window.hpp>
  5.  
  6. using namespace std;
  7.  
  8. int width = 800, height = 600;
  9.  
  10. int main()
  11. {
  12.     sf::RenderWindow window(sf::VideoMode(width, height), "In Development");
  13.     window.setFramerateLimit(70);
  14.     window.setVerticalSyncEnabled(true);
  15.  
  16.     sf::RectangleShape player;
  17.     player.setFillColor(sf::Color::Cyan);
  18.     player.setSize(sf::Vector2f(40, 40));
  19.     player.setPosition(width / 2, height / 2);
  20.  
  21.     sf::RectangleShape bigBlock;
  22.     bigBlock.setSize(sf::Vector2f(100, 100));
  23.     bigBlock.setFillColor(sf::Color::Black);
  24.     bigBlock.setPosition((width / 2) - 300, height / 2);
  25.  
  26.     int direction = 0;
  27.     float gravity = .3;
  28.     int previousyCoord = NULL;
  29.     sf::Vector2f velocity(0, 0);
  30.     bool onGround = NULL;
  31.  
  32.     sf::Event event;
  33.     while (window.isOpen())
  34.     {
  35.         while (window.pollEvent(event))
  36.         {
  37.             if (event.type == sf::Event::Closed)
  38.                 window.close();
  39.             else if (event.type == sf::Event::KeyPressed)
  40.             {
  41.                 if (event.key.code == sf::Keyboard::Right)
  42.                 {
  43.                     player.move(5, 0);
  44.                     direction = 1;
  45.                 }
  46.                 else if (event.key.code == sf::Keyboard::Left)
  47.                 {
  48.                     player.move(-5, 0);
  49.                     direction = 2;
  50.                 }
  51.                 else if(event.key.code == sf::Keyboard::Space)
  52.                 {
  53.                     velocity.y = -10;
  54.                 }
  55.             }
  56.             else
  57.             {
  58.                 direction = 4;
  59.             }
  60.         }
  61.  
  62.         if (player.getPosition().y > 500)
  63.         {
  64.             player.setPosition(player.getPosition().x, 500);
  65.             velocity.y = 0;
  66.         }
  67.         else
  68.         {
  69.             velocity.y += gravity;
  70.         }
  71.  
  72.         player.move(velocity);
  73.  
  74.         sf::FloatRect player_boundingBox = player.getGlobalBounds();
  75.         if (player_boundingBox.intersects(bigBlock.getGlobalBounds()))
  76.         {
  77.             if (direction == 1 && onGround == true)
  78.             {
  79.                 player.setPosition(player.getPosition().x, bigBlock.getPosition().y - 40);
  80.             }
  81.             else if (direction == 2 && onGround == true)
  82.             {
  83.                 player.setPosition(player.getPosition().x, bigBlock.getPosition().y - 40);
  84.             }
  85.             else if (direction == 1)
  86.                 player.setPosition(bigBlock.getPosition().x - 40, player.getPosition().y);
  87.             else if (direction == 2)
  88.                 player.setPosition(bigBlock.getPosition().x + 100, player.getPosition().y);
  89.             else if (direction == 4 && previousyCoord <= bigBlock.getPosition().y)
  90.             {
  91.                 player.setPosition(player.getPosition().x, bigBlock.getPosition().y - 40);
  92.                 onGround = true;
  93.                 velocity.y = 0;
  94.             }
  95.         }
  96.         else
  97.             onGround = false;
  98.  
  99.         window.clear(sf::Color::White); //Clear
  100.  
  101.         window.draw(bigBlock);
  102.         window.draw(player);
  103.  
  104.         window.display(); //Display
  105.  
  106.         cout << direction << " --- " << " OnGround: " << onGround << endl;
  107.         previousyCoord = player.getPosition().y;
  108.     }
  109.  
  110.     int pause; cin >> pause; //Pause the program
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment