Antonio Riverso
Antonio Riverso

Reputation: 33

Problem with SFML 2.5.1: Collision Detection not working

can you pls help me on what I did wrong here?

I followed this tutorial on YT: https://www.youtube.com/watch?v=l2iCYCLi6MU&t=605s for making a collision detection, but my penguin seems to go through my objects. I'm very frustrated.

The version of sfml i use is the 2.5.1. I use Visual Studio 2017.

I promise, in the files I linked here there's no virus. https://drive.google.com/drive/folders/16vl1Kkv-O5IRb8Svu71qLPXRkn3eINf1?usp=sharing

(update - I solved the issue by replacing

float intersectX = abs(deltax) - (otherHalfSize.x + thisHalfSize.x); float intersectY = abs(deltay) - (otherHalfSize.y + thisHalfSize.y);

so I changed minus with the plus, but now the pushing sequence works only from the bottom and right, but when i try to push to the left, the object immediately stands over the head of the character. What did I failed?

Some people are sceptical about viruses so I'm gonna just paste the code:

this is the code for the header Collider

Whatch the video and tell me what I could have done wrong.

sf::Vector2f otherPosition = other.getPosition();
sf::Vector2f otherHalfSize = other.gethalfsize();
sf::Vector2f thisPosition = getPosition();
sf::Vector2f thisHalfSize = gethalfsize();

float deltax = otherPosition.x - thisPosition.x;
float deltay = otherPosition.y - thisPosition.y;

float intersectX = abs(deltax) - (otherHalfSize.x + thisHalfSize.x);
float intersectY = abs(deltay) - (otherHalfSize.y + thisHalfSize.y);

if (intersectX < 0.0f and intersectY < 0.0f)
{
    push = std::min(std::max(push, 0.0f), 1.0f);
    if (intersectX > intersectY) // ricordo che adesso stiamo utilizzando numeri negativi
    {
        if (deltax > 0.0f)
        {
            Move(intersectX * (1.0f - push), 0.0f);
            other.Move(-intersectX * push, 0.0f);
        }
        else
        {
            Move(intersectX * (1.0f - push), 0.0f);
            other.Move(intersectX * push, 0.0f);
        }

    }
    else
    {
        if (deltay > 0.0f)
        {
            Move(0.0f, intersectY * (1.0f - push));
            other.Move(0.0f, -intersectY * push);
        }
        else
        {
            Move(0.0f, intersectY * (1.0f - push));
            other.Move(0.0f, intersectY * push);
        }
    }
    return true;
}
return false;

Upvotes: 0

Views: 212

Answers (1)

Antonio Riverso
Antonio Riverso

Reputation: 33

I found the problem:

I need to change this:

Move(intersectX * (1.0f - push), 0.0f);

to this:

Move(-intersectX * (1.0f - push), 0.0f);

the same on the Y axis. I spent all the night finding this stupid error.

Upvotes: 1

Related Questions