【发布时间】:2020-06-13 18:41:00
【问题描述】:
你能帮我解决我在这里做错了什么吗?
我在 YT 上关注了这个教程: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;
【问题讨论】:
-
之后我感觉很不确定:
I promise, in the files I linked here there's no virus。你能提供给我们你的实际代码吗? -
是的,但是很长
标签: c++ collision-detection sfml collision