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

Author Topic: intersection between sprites from different classes  (Read 1664 times)

0 Members and 1 Guest are viewing this topic.

AzkaIsHere

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
intersection between sprites from different classes
« 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?
« Last Edit: April 19, 2018, 02:46:47 pm by AzkaIsHere »

sjaustirni

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
Re: intersection between sprites from different classes
« Reply #1 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() on your Sprite objects, which returns a rectangle. Then you have two rectangles with which you can call intersects() 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 :)

 

anything