I am seriously struggling with collision, I have been reading articles and looking at examples but I just cannot seem to figure it out at all, I have been trying to figure out some collision for over a month or more, can someone please help me?
I deleted all my collision code that I had, which wasnt very much but still. It was too embarrassing to post.
/*
//
*/
#include <SFML/Graphics.hpp>
#include <SFGUI/SFGUI.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
enum Direction{Down, Left, Right, Up};
class Game
{
public:
void Program();
void CreateCharacters();
void LoadCharacters();
void CropSprite();
void UserInput();
void Collision();
private:
vector<sf::Vector2f> player_pos;
vector<sf::Sprite> load_sprites;
vector<sf::Texture> load_textures;
sf::Sprite sprite;
sf::Texture texture;
sf::Vector2i source;
sf::RectangleShape rect;
};
/*
class GUI : public Game
{
public:
void CreateMenu();
void CloseMenu();
void MenuButtons();
private:
sfg::SFGUI sfgui;
sfg::Desktop dtop;
sfg::Window::Ptr menu_window;
sfg::Button::Ptr menu_exit;
sfg::Box::Ptr box_HOR;
sfg::Box::Ptr box_VER;
};
void GUI::CreateMenu()
{
box_HOR = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL);
box_HOR->Pack(menu_exit, false, true);
}
void GUI::MenuButtons()
{
menu_exit = sfg::Button::Create("Exit");
}
*/
//Set characters to a sprite.
void Game::CreateCharacters()
{
sprite.setTexture(texture);
CropSprite();
}
//This will be a vector later and it will load all
//characters individually.
void Game::LoadCharacters()
{
if(!texture.loadFromFile("Resources/Characters/Player.png"))
{
cout << "Error loading texture" << endl;
}
CreateCharacters();
}
//We dont want our character to be the entire sprite sheet
//so we crop out the character and its position that we want.
void Game::CropSprite()
{
sprite.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
}
void Game::UserInput()
{
CropSprite();
Collision();
GUI gui;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
source.y = Up;
sprite.move(0, -1.3);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
source.y = Left;
sprite.move(-1.3, 0);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
source.y = Down;
sprite.move(0, 1.3);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
source.y = Right;
sprite.move(1.3, 0);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
//gui.menu_exit;
}
}
void Game::Collision()
{
}
void Game::Program()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "App");
window.setFramerateLimit(60);
window.resetGLStates();
rect.setSize(sf::Vector2f(32, 32));
rect.setFillColor(sf::Color::Blue);
rect.setPosition(80, 80);
LoadCharacters();
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::Resized:
sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(viewArea));
}
}
window.clear(sf::Color::White);
UserInput();
window.draw(rect);
window.draw(sprite);
window.display();
}
}
int main()
{
Game game;
game.Program();
return 0;
}