SFML 2.5.1的问题:冲突检测不起作用

问题描述 投票:0回答:1

您能帮我解决我在这里做错的事情吗?

我在YouTube上关注了本教程:https://www.youtube.com/watch?v=l2iCYCLi6MU&t=605s用于进行碰撞检测,但是我的企鹅似乎穿过了我的物体。我非常沮丧。

我使用的sfml版本是2.5.1。我使用Visual Studio 2017。

我保证,在我链接到的文件中没有病毒。https://drive.google.com/drive/folders/16vl1Kkv-O5IRb8Svu71qLPXRkn3eINf1?usp=sharing

((更新-我通过替换解决了问题)>

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

所以我用加号更改了减号,但是现在推序列仅从底部和右侧开始起作用,但是当我尝试向左推时,对象立即站在角色的头部。我失败了什么?

有些人对病毒持怀疑态度,所以我只粘贴代码:

这是标题对撞机的代码

抓起视频,告诉我我做错了什么。

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;
    

您能帮我解决我在这里做错的事情吗?我在YT上遵循了该教程:https://www.youtube.com/watch?v=l2iCYCLi6MU&t=605s进行了碰撞检测,但我的企鹅似乎去了...

c++ collision-detection sfml collision
1个回答
0
投票

我发现了问题:

© www.soinside.com 2019 - 2024. All rights reserved.