gpt4 book ai didi

c++ - 水平碰撞不起作用 AABB C++

转载 作者:行者123 更新时间:2023-11-30 03:20:50 25 4
gpt4 key购买 nike

更新

再次更改了碰撞代码并为AABB制作了一个组件,现在看来问题仅在于水平碰撞,它没有将物体推到它认为足够的位置但是与Y轴的代码相同所以它不应该成为一个问题。

(它确实检测到水平碰撞分辨率是问题)

image

void Hermes_Player::Collision(GameObject * other)
{
if (other->GetTag() == "wall") {
AABB* myAABB = dynamic_cast<AABB*>(this->GetComponent("aabb"));
AABB* otherAABB = dynamic_cast<AABB*>(other->GetComponent("aabb"));
if (abs(myAABB->lastCenter.x - otherAABB->lastCenter.x) < myAABB->halfCenter.x + otherAABB->halfCenter.x) {
std::cout << "y" << std::endl;
if (myAABB->center.y < otherAABB->center.y) {
int distance = (myAABB->halfCenter.y + otherAABB->halfCenter.y) - (otherAABB->center.y - myAABB->center.y);
this->Position.y -= distance;
myAABB->center.y = (myAABB->center.y - distance);
}
if (myAABB->center.y > otherAABB->center.y) {
int distance = (myAABB->halfCenter.y + otherAABB->halfCenter.y) - (myAABB->center.y - otherAABB->center.y);
this->Position.y += distance;
myAABB->center.y = (myAABB->center.y + distance);
}
}
else
{
std::cout << "x" << std::endl;

int dist = myAABB->halfCenter.x + otherAABB->halfCenter.x;
int dif = (this->Size.x + other->Size.x) /2- abs(dist);
if (myAABB->center.x < otherAABB->center.x) {
int distance = (myAABB->halfCenter.x + otherAABB->halfCenter.x) - (otherAABB->center.x - myAABB->center.x);
this->Position.x -= distance;
myAABB->center.x = (myAABB->center.x - distance);
}
if (myAABB->center.x > otherAABB->center.x) {
int distance = (myAABB->halfCenter.x + otherAABB->halfCenter.x) - (myAABB->center.x - otherAABB->center.x);
this->Position.x += distance;
myAABB->center.x = (myAABB->center.x + distance);
}
std::cout << this->Position.x << std::endl;
}
}
}

最佳答案

这可能不是您想要的,但尝试同时解析 X 轴和 Y 轴可能会相当复杂。

一个解决方案可能是独立步进每个轴并分别解决它们的碰撞。

您必须执行两次碰撞检测,但它使解决更简单,IE 不会检查碰撞解决是否导致更多碰撞,您只需停止沿着第一个碰撞对象的边缘移动。

这篇文章在我开发自己的 2D 平台游戏时为我提供了帮助,它推荐了这种做法:

http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/

关于c++ - 水平碰撞不起作用 AABB C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52527612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com