Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Collision help please  (Read 3295 times)

0 Members and 1 Guest are viewing this topic.

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Collision help please
« on: September 27, 2014, 09:09:14 pm »
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;
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision help please
« Reply #1 on: September 27, 2014, 09:10:25 pm »
What's the question?  ???
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Collision help please
« Reply #2 on: September 27, 2014, 09:23:51 pm »
I cannot get any type of collision working at all, I just cant seem to do it, collision is one of my biggest problems. If I can see a simple working example for some collision that blocks all four sides of a square then I can examine it and go from there, but so far i've found no useful examples that help me. The above code works and allows for character movement in all four directions, and here is the character sheet i used:

http://oi59.tinypic.com/2zxxs0y.jpg
« Last Edit: September 27, 2014, 09:28:25 pm by Chay Hawk »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision help please
« Reply #3 on: September 27, 2014, 09:34:47 pm »
...collision that blocks all four sides of a square...
Blocks from the inside or outside? e.g. a box that you can't enter, or a room that you can't exit.

I cannot get any type of collision working at all
Collision detection isn't particularly hard in general (is this thing overlapping that thing?); it's what you do when there is a collision that can be taxing.

If I can see a simple working example for some collision
Maybe it's worth looking at some open-sourced games. I don't know how "simple" these "examples" are, but there are some on the SFML wiki here.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Collision help please
« Reply #4 on: September 27, 2014, 09:46:02 pm »
From outside, I want to make a top down game. I know it must be simple but iI just cant figure it out but like i said If i see an example I can learn from it, because I can dissect it myself and figure it out, that helps me learn it better and understand too but I need to start with small code first. I took a look at some of their collisions and they are too difficult for me at the moment, some of them are 4 files big. I need something simpler so that I can actually learn from it and understand it before i move onto that.
« Last Edit: September 27, 2014, 09:53:37 pm by Chay Hawk »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision help please
« Reply #5 on: September 27, 2014, 10:12:48 pm »
I know it must be simple
Dealing with collisions is rarely simple, depending on its accuracy, of course.

Is it collision detection that you're having trouble with?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Collision help please
« Reply #6 on: September 27, 2014, 10:42:45 pm »
I'm having trouble actually making them stop from going through the object. I can make it so it detects the collision, I just cant figure out how to make it so the player will not go through the object, and when I did have something working a long time ago, when i was against the object and pressed a different key the player would teleport to a corner of the square no matter what side it was on. I just want the player to not go through the cube and not bounce and jitter when they hit the object or do weird stuff when I press different keys.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision help please
« Reply #7 on: September 27, 2014, 10:52:36 pm »
Ah, so dealing with a detected collision.
There are many variables (no pun intended) to how you deal with them.
Firstly, I'd recommend making sure you have a fixed time-step before continuing any further.
Then, decide whether it will be vector collision (is that point/line in/past that area/line etc.?) or pixel collision (is this pixel at the same position as that pixel?). If vector collision (most likely), there are a lot of routes to consider. I'm not expert on collision (or anything for that matter) but you will need to comfortable with trigonometry and stuff to be able to back-track a collision to where it should be.
Then, implement it!  :D

That said, test the collision, and if it's colliding, move it so that it isn't. Be sure, though, that you test all collisions before doing any moving, and that when you move it, it's no longer colliding.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Collision help please
« Reply #8 on: September 28, 2014, 12:27:52 am »
Ok, well I'm horrible at math, dont know even a little trigonometry. I'll have to study up on it. So I guess for now is there any libraries that can give me physics and collision that i can use commercially in my project? I'm looking at Box2D but that says physics, I assume that includes collision as well but I want to make sure, does anyone know if it does?
« Last Edit: September 28, 2014, 12:50:09 am by Chay Hawk »

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Collision help please
« Reply #9 on: September 28, 2014, 01:05:22 am »
Yes it does.