-1

My problem is that I can not change the class members variable value, I tried everything that has come to my mind

So here is the following code that is involved:

if(player.getCollisionObject().isColliding(platform1))
{
    if(player.getCollisionObject().up)
        std::cout << "Colliding from top\n";
    if(player.getCollisionObject().down)
        std::cout << "Colliding from bottom\n";
    if(player.getCollisionObject().left)
        std::cout << "Colliding from left\n";
    if(player.getCollisionObject().right)
        std::cout << "Colliding from right\n";
}

bool Collision::isColliding(const Entity &other)
{
    resetCollisionDirection();

    sf::Vector2f otherCenterPosition = other.getCenterPosition();
    sf::Vector2f otherSize = other.getSize();

    float deltaX = m_CenterPosition.x - otherCenterPosition.x; // abs(deltaX) - (thisX + otherX)
    float deltaY = m_CenterPosition.y - otherCenterPosition.y;

    float resultX = abs(deltaX) - (m_Size.x / 2 + otherSize.x / 2); 
    float resultY = abs(deltaY) - (m_Size.y / 2 + otherSize.y / 2);

    if(resultX < 0 && resultY < 0) 
    {
        if(m_CenterPosition.x < otherCenterPosition.x)
            left = true;
        if(m_CenterPosition.x > otherCenterPosition.x)
            right = true;
        if(m_CenterPosition.y < otherCenterPosition.y)
            up = true;
        if(m_CenterPosition.y > otherCenterPosition.y)
            down = true;

        return true;
    }
    return false;
}

class Collision
{
public:
    Collision(const Entity &entity);

    void reset(const Entity &entity);
    bool isColliding(const Entity &other);

    bool up, down, left, right;

private:
    void resetCollisionDirection();

private:
    sf::Vector2f m_CenterPosition, m_Size;

};

So the problem is that I have a class member of Collision in my player class and with that I am accesing the Collision object from my player and checking if it is colliding with another object and if so it will return true and should also set the internal flag from which direction it is colliding from, but after it returns from the "isColliding()" function the flags from the Collision object hasn't been set

I do not really understand what may be the problem here, I tried debugging it and tried to follow along step by step. My observations were that it did in fact set the flags during the function call but as soon as it returned the information was lost

Any help would be appreciated!

EDIT: Here is the getCollisionObject() function:

Collision Player::getCollisionObject()
{
    return m_CollisionBody;
}

EDIT: Found the problem the function above returned a copy (I am an idiot sorry) changed it to

Collision& Player::getCollisionObject()
{
    return m_CollisionBody;
}
muhm
  • 1
  • 2
  • 1
    Use your debugger. Place breakpoints at suspicious locations in your code, reason about what should happen, and _then_ see what actually happens. – alter_igel Jul 19 '18 at 19:11
  • Show us the `getCollisionObject` function. – David G Jul 19 '18 at 19:13
  • @alterigel I actually did that and is why I am here, because I couldn't find where the problem may be! I checked step by step as I mentioned evertyhing seemed to work fine BUT as soon as I returned from the function call nothing has been actually set in the Collision variable! – muhm Jul 19 '18 at 19:13
  • @0x499602D2 I added it, could the problem be because it returns a copy that gets set? – muhm Jul 19 '18 at 19:15
  • 1
    @muhm That's exactly the problem. You have to return `Collision&`. – David G Jul 19 '18 at 19:16
  • @0x499602D2 should have made an actual answer thanks! can't believe how stupid I am searched for the wrong place... – muhm Jul 19 '18 at 19:18

1 Answers1

1

There's your problem. getCollisionObject() returns by value. Change it to

Collision &Player::getCollisionObject()
dgnuff
  • 3,195
  • 2
  • 18
  • 32