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

Author Topic: Some Bounding Box Help  (Read 2294 times)

0 Members and 1 Guest are viewing this topic.

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Some Bounding Box Help
« on: July 29, 2013, 11:48:26 pm »
Hello again everybody!

So right now I'm just trying to add some collision.  I have a tiled map ( a .txt file with different values for different textures) I'm just going to try for some bounding box collision at the moment.  This is what I have so far, but when I move the player into the enemy box, it just gets stuck.  How can I fix that, or is there a better way to implement collision with a tiled map?  Actually, a better system of implementing collision would be greatly appreciated! :D

#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <vector>

const int TILE_SIZE = 32;

class Player
{
public:
        sf::RectangleShape rect;
        float top, bottom, left, right;

        Player(sf::Vector2f position, sf::Vector2f size, sf::Color color)
        {
                rect.setPosition(position);
                rect.setSize(size);
                rect.setFillColor(color);
        }

        void Update(void)
        {
                top = rect.getPosition().y;
                bottom = rect.getPosition().y + rect.getSize().y;
                left = rect.getPosition().x;
                right = rect.getPosition().x + rect.getSize().x;
        }

        bool VertCollision(Player p)
        {
                if (top > p.bottom || bottom < p.top || left > p.right || right < p.left)
                        return false;

                return true;
        }

        bool HorzCollision(Player p)
        {
                if (top > p.bottom || bottom < p.top || left > p.right || right < p.left)
                        return false;

                return true;
        }
};

int main()
{
        sf::RenderWindow window(sf::VideoMode(640, 480, 32), "Bounding Box Collision");

        Player p1(Player(sf::Vector2f(10, 10), sf::Vector2f(20, 20), sf::Color::Green)),
                p2(Player(sf::Vector2f(100, 100), sf::Vector2f(20, 20), sf::Color::Red));

        while (window.isOpen())
        {
                sf::Event event;

                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case::sf::Event::Closed:
                                window.close();
                                break;
                        default:
                                break;
                        }
                }

                p1.Update();
                p2.Update();

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        p1.rect.move(0, -1.0f);
                        if (p1.VertCollision(p2))
                                p1.rect.move(0, 1.0f);
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        p1.rect.move(0, 1.0f);
                        if (p1.VertCollision(p2))
                                p1.rect.move(0, -1.0f);
                }      
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        p1.rect.move(-1.0f, 0);
                        if (p1.HorzCollision(p2))
                                p1.rect.move(1.0f, 0);
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        p1.rect.move(1.0f, 0);
                        if (p1.HorzCollision(p2))
                                p1.rect.move(-1.0f, 0);
                }

                window.clear();

                window.draw(p1.rect);
                window.draw(p2.rect);

                window.display();
        }

        return 0;
}

Thanks!

gostron

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Some Bounding Box Help
« Reply #1 on: July 30, 2013, 12:30:43 pm »
Hi there,

First I think you already know it, but both of your boolean collision functions verify collisions in all directions.

That being said, I think it has to do with the way it is handled. You do not actualize the variables you use for collision (top, bottom, left, right) after your first move, so position checking is on the previous position, meaning you can intersect your players. Once you are in an intersection, you cannot move at all since you cancel every move.

Hope this helps.

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Some Bounding Box Help
« Reply #2 on: July 30, 2013, 07:45:10 pm »
Yeah, the two collision functions were an earlier test that I then realized were exactly the same.  Unfortunately I can't seem to get it to work though, should I call p1.update() in multiple places?  If so, where?

Well, as a side note.  What can I do to the player movement?  Like, when the player collides with the box, what should I do the the player's movement?  Is what I'm using currently ok?  Because it seems ... awkward. 
« Last Edit: July 31, 2013, 12:05:46 am by Clockwork »

gostron

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Some Bounding Box Help
« Reply #3 on: July 31, 2013, 10:59:17 am »
I think you should use directly getPosition() inside your getCollision() methods because you only want to use your current position at all times right now.

I for myself like to proceed differently. For each tick, I modify the speed of my object (so pressing a button sets or adds speed to your player) and then I call a collision checker which will modify my speed according to collisions and speed. If my next position is inside player 2 for example, I modify the speed so that it does not collide, which enables me to process lots of different things like bouncing for example.

However I believe you can find some really nice tutorials on the internet concerning collision management and physics.