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;
}