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

Author Topic: collision detection  (Read 1728 times)

0 Members and 2 Guests are viewing this topic.

lack

  • Newbie
  • *
  • Posts: 3
    • View Profile
collision detection
« on: October 16, 2015, 02:21:45 pm »
Hi, when i run my program, the controllable rectangle is always blue although i want it to be red when it collides with tiles with the value 1.

 player.Update();
    for(int i = 0; i < colMap.size(); i++)
{
        for(int j = 0; j < colMap[i].size(); j++)
        {
            if(colMap[i][j] == 1)
            {
            int bottom, top, left, right;
            bottom = i * 40 + 40;
            top = i * 40;
            right = j * 40 + 40;
            left = j * 40;

                if(player.right < left || player.left > right || player.top > bottom || player.bottom < top)
                {
                    player.rect.setFillColor(sf::Color::Blue);
                }
                else
                {
                    player.rect.setFillColor(sf::Color::Red);
                    break;
                }
            }

        }
}

Stuff that might help you further:
(click to show/hide)

(click to show/hide)

(click to show/hide)

(click to show/hide)

thanks

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: collision detection
« Reply #1 on: October 16, 2015, 02:45:59 pm »
You can use the built-in sf::Rect<type>::intersect( sf::Rect<type> )

See the docs:

http://www.sfml-dev.org/documentation/2.0/classsf_1_1Rect.php#a566740c8f58e01bb052266f47e7e1011

This is a good tutorial for general collision detection:

http://www.toptal.com/game/video-game-physics-part-ii-collision-detection-for-solid-objects
I would like a spanish/latin community...
Problems building for Android? Look here

lefreut

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: collision detection
« Reply #2 on: October 16, 2015, 02:47:56 pm »
Hello,

The break statement in C++ will break out of the for statement in which the break is directly placed. In your sample, the outer loop will continue and your code will reset the color to blue.

lack

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: collision detection
« Reply #3 on: October 16, 2015, 03:02:21 pm »
oh thanks for the notice, i fixed it but the problem seems to persist, any other clues?
I do get the idea behind collision detection but trying to do it with an array is difficult for me
« Last Edit: October 16, 2015, 03:08:06 pm by lack »

lack

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: collision detection
« Reply #4 on: October 16, 2015, 05:32:29 pm »
Fixed it, forgot to put the player updating function under the active event poll, too much coffee isn't good for my health