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

Author Topic: Simple Collision Help  (Read 2350 times)

0 Members and 1 Guest are viewing this topic.

guitarmatt99

  • Newbie
  • *
  • Posts: 19
    • View Profile
Simple Collision Help
« on: November 10, 2013, 03:35:15 pm »
Hello! I have been recently experiment with collision between two shapes but I can't quite get it to work. It only worked with two opposite sides.

How would I do collision correctly?

Here's some minimal code that detects the collision but doesn't stop movement. Movement it controlled by holding the mouse button down and dragging.

I have removed code that isn't affecting anything.

int main()
{
    //Player
    sf::RectangleShape player(sf::Vector2f(80.f, 80.f));
    player.setOrigin(40.f, 40.f);
    player.setPosition(400.f, 40.f);

    //Enemy
    sf::RectangleShape enemy(sf::Vector2f(80.f, 80.f));
    enemy.setOrigin(40.f, 40.f);
    enemy.setPosition(600.f, 350.f);
    enemy.setFillColor(sf::Color(232,74,63));

    while(window.isOpen())
    {
        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
            player.setPosition(sf::Vector2f(sf::Mouse::getPosition(window)));

        //Collision
        if(enemy.getGlobalBounds().intersects(player.getGlobalBounds()))
            player.setFillColor(sf::Color(40,39,39));
        else
            player.setFillColor(sf::Color(170,227,210));

        //Render
        window.clear(sf::Color(240, 240, 240));
        window.draw(player);
        window.draw(enemy);
        window.display();
    }
}
 
« Last Edit: November 10, 2013, 05:17:13 pm by guitarmatt99 »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Simple Collision Help
« Reply #1 on: November 10, 2013, 04:27:09 pm »
well, this code does what its expected to do: identify collision. do you want to avoid one square to "enter" another? if so, i first suggest putting the line which changes the player position just after you get the mouse click, like this
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
    player.setPosition(sf::Vector2f(sf::Mouse::getPosition(window)));
    [...]
}

AND, you'll need a little function to set the if the player bounds outside the enemy bounds.
something like
if(player.getPosition().x >= enemy.getPosition().x){
    player.setPosition(enemy.getPosition().x - enemy.getGlobalBounds().width, player.getPosition().y);
}

of course, you'll also need to make function for checking the other side of the enemy, as well as the Y axis.

i hope that answers your question.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

guitarmatt99

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Simple Collision Help
« Reply #2 on: November 10, 2013, 05:10:07 pm »
Thanks for answering but when I try this something goes wrong when positioning.

Did you mean do this?

if(player.getPosition().y + player.getOrigin().y >= enemy.getPosition().y - enemy.getOrigin().y){
    player.setPosition(player.getPosition().x, enemy.getPosition().y - player.getSize().y);
}
else if(player.getPosition().y - player.getOrigin().y <= enemy.getPosition().y + enemy.getOrigin().y){
    player.setPosition(player.getPosition().x, enemy.getPosition().y + player.getSize().y);
}
else if(player.getPosition().x - player.getOrigin().x <= enemy.getPosition().x + enemy.getOrigin().x){
    player.setPosition(enemy.getPosition().x + player.getSize().x, player.getPosition().y);
}
else if(player.getPosition().x + player.getOrigin().x >= enemy.getPosition().x - enemy.getOrigin().x){
    player.setPosition(enemy.getPosition().x - player.getSize().x, player.getPosition().y);
}
 

This is the way I tried before, if you can get it working could you post your code? This has been bothering me for 2 days! Haha!
« Last Edit: November 10, 2013, 05:16:40 pm by guitarmatt99 »

Eulavvalue

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Simple Collision Help
« Reply #3 on: November 10, 2013, 08:10:54 pm »
I think you may want to take a look at the thread I posted yesterday. I solved it already (to my knowledge), but it'll get you going. Note that this is only good for boxes, though. If you want to tackle circles vs boxes then you would have to find a different method.
http://en.sfml-dev.org/forums/index.php?topic=13510.0

guitarmatt99

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Simple Collision Help
« Reply #4 on: November 10, 2013, 09:16:21 pm »
That looks complicated, am I under estimating simple collision? I know what I did works when you comment the other 3 statements but when you include them all it breaks, I just want to know why it breaks.