SFML community forums

Help => General => Topic started by: AzkaIsHere on April 19, 2018, 02:41:33 pm

Title: intersection between sprites from different classes
Post by: AzkaIsHere on April 19, 2018, 02:41:33 pm
Hello guys I am struggling while trying to make this intersection between my tree and rock sprite but they are in different classes and I don't know how to make the command in this situation since I just started using classes with sprites.

this is the tree class:

class Tree
{
public:
        Tree(string treerectory)
        {
                t.loadFromFile(treerectory);
        }
       
        void treeTexture()
        {
                for (size_t i = 0; i < 1000; i++)
                {
                        tree[i].setTexture(t);
                        tree[i].setScale(1, 2);
                }
        }

        void treePosition()
        {
                for (size_t i = 0; i < 1000; i++)
                {
                        tree[i].setPosition(rand() % 10560 + 0.f, rand() % 10560 + 0.f);
                }
        }

        void treeDraw(RenderWindow &app)
        {
                for (size_t i = 0; i < 1000; i++)
                {
                        app.draw(tree[i]);
                }
        }

        void treeSelfCollision()
        {
                srand(time(0));

                for (size_t i = 0; i < 1000; i++)
                {
                        if (tree[i].getGlobalBounds().intersects(tree[1000].getGlobalBounds()))
                        {
                                tree[i].move(rand() % 15 - 15, rand() % 15 - 15);
                        }
                }
        }

private:
        Texture t;
        Sprite tree[1000];
};
 

and this is the rock class:

class Rock
{
public:
        Rock(string rockrectory)
        {
                r.loadFromFile(rockrectory);
        }

        void rockTexture()
        {
                for (size_t i = 0; i < 1000; i++)
                {
                        rock[i].setTexture(r);
                }
        }

        void rockPosition()
        {
                for (size_t i = 0; i < 1000; i++)
                {
                        rock[i].setPosition(rand() % 10560 + 0, rand() % 10560 + 0);
                }
        }

        void rockDraw(RenderWindow &app)
        {
                for (size_t i = 0; i < 1000; i++)
                {
                        app.draw(rock[i]);
                }
        }

        void rockSelfCollision()
        {
                for (size_t i = 0; i < 1000; i++)
                {
                        if (rock[i].getGlobalBounds().intersects(rock[1000].getGlobalBounds()))
                        {
                                rock[i].move(rand() % 15 - 15, rand() % 15 - 15);
                        }
                }
        }

private:
        Texture r;
        Sprite rock[1000];
};
 

I want to make a function to check if the tree and the rock are intersected, how do I do?
Title: Re: intersection between sprites from different classes
Post by: sjaustirni on April 23, 2018, 02:00:19 pm
Hi!

As it is very often the case, the answer to the question is - it depends. More specifically, it depends on the collision body you want to use.

If rectangles are enough for you, it is easy. You can call getGlobalBounds() (https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Sprite.php#a203d2d8087bfdca2ebc3c0485cdb7409) on your Sprite objects, which returns a rectangle. Then you have two rectangles with which you can call intersects() (https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1Rect.php#a566740c8f58e01bb052266f47e7e1011) to check for collisions.

There are other approaches (SAT, pixel-perfect, ...) but this ought to be the easiest one (and usually is enough for axis aligned box collisions).

Also, as a note aside, it is a good programming practice to call your methods with a verb, not noun(s). And you don't have to put rock in front of method names in Rock class, they already are in its namespace. This is not C.

Happy coding :)