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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - XxLanternexX

Pages: [1]
1
General / Strange problem with sprite collision
« on: June 04, 2021, 10:48:56 am »
Hello everyone,

I try to do a simple thing: I have a vector of Heroes and I want to change the color's sprite when one of the hero collides another one. The sprite's color change to red when there is a collision and return to normal/original color when there is no collision.

Hero is just a simple struct with a sprite and a texture:
struct Hero
{
    sf::Sprite s;
    sf::Texture t;

    Hero(sf::Vector2f pos)
    {
        if(!t.loadFromFile("sprite_example.png"))
                throw std::runtime_error(...);
       
        s.setTexture(t);
        s.setTextureRect(sf::IntRect(0,0,64,64));
        s.setPosition(pos);

       
    }
};
So I do this:
std::vector< std::unique_ptr<Hero> > vectorHeroes;
vectorHeroes.push_back( std::make_unique<Hero>(sf::Vector2f(10.f,100.f)) ); // create hero at position 10,100
// create some other heroes...
 

Simple function that test if there is collision between two sprites:
bool isCollision(const sf::Sprite &s1, const sf::Sprite &s2) // check if there's collision between s1 and s2. Return true if it's the case.
{
    if(s1.getGlobalBounds().intersects(s2.getGlobalBounds())) return true;

    return false;
}

Then I make a double loop to test if a hero(in vector) collides another one(in vector too) :


void checkCollision()
{
    int i = 0, j = 0;

    while(i < vectorHeroes.size())
    {

        while(j < vectorHeroes.size())
        {
            if(i != j)  // makes sure the two tested sprites are different
            {
                if( isCollision( vectorHeroes[i]->s, vectorHeroes[j]->s ) )  // if there's a collision between one of the sprites with another one
                {
                    std::cout << "COLLISION WITH " << i << " AND " << j << "\n==============" << std::endl;
                    vectorHeroes[i]->s.setColor(sf::Color::Red);  // set the sprite's color to red
                }
                else
                {
                    vectorHeroes[i]->s.setColor(sf::Color(-1,-1,-1));  // set the sprite's color to the original one
                }
            }
            ++j;
        }
        ++i;
     }
}

Then I move my heroes and call checkCollision() in the game loop.

But the problem is :
1. when there's a collision: ONE sprite becomes red and not the other colliding sprites, sometimes the sprite's color does not change at all(setColor(red) is ignored).

2. the collision between certain sprites is ignored(WHY ???). It seems there is a bug in the program.

I don't know WHY there's this strange problem. What I want to do is pretty simple though.

I think it's a problem relative to the index of the vector when I browse it with while loop but not sure.

Have you a solution, please ?
PS: sorry if there's grammar mistakes


Pages: [1]