更新
再次更改了碰撞代码并为 AABB 制作了一个组件,现在看来问题仅在于水平碰撞,它并没有将物体推到它认为的足够但与 Y 轴相同的代码,所以它不应该是一个问题.
(它确实检测到水平碰撞,分辨率是问题)
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;
}
}
}