c++ - 从游戏中的可移动空间中删除矩形

标签 c++ animation collision-detection

我有一个简单的设置,其中有一个球在屏幕上弹跳,我希望能够为对象提供它无法在内部移动的矩形(它们将充当障碍物)。目前有以下逻辑来尝试完成这个。

void AnimatableObject::ExcludeRects(AnimatableObject *obj) {
        for (int i = 0; i < obj->numberOfExclusionBounds; i++) {
            SDL_Rect bounds = obj->exclusionBounds[i];

            /* TOUCHING SIDES OF THE RECT*/
            bool touchingTopOfRect = obj->m_dY + obj->m_dHeight > bounds.y &&
                obj->m_dY < bounds.y + bounds.h &&
                obj->m_dX > bounds.x &&
                obj->m_dX + obj->m_dWidth < bounds.x + bounds.w;

            bool touchingBottomOfRect = obj->m_dY < bounds.y + bounds.h &&
                obj->m_dY > bounds.y &&
                obj->m_dX > bounds.x &&
                obj->m_dX + obj->m_dWidth < bounds.x + bounds.w;

            bool touchingRightOfRect = obj->m_dX < bounds.x + bounds.w &&
                obj->m_dX > bounds.x &&
                obj->m_dY > bounds.y &&
                obj->m_dY + obj->m_dHeight < bounds.y + bounds.h;

            bool touchingLeftOfRect = obj->m_dX + obj->m_dWidth > bounds.x &&
                obj->m_dX + obj->m_dWidth < bounds.x + bounds.w &&
                obj->m_dY > bounds.y &&
                obj->m_dY + obj->m_dHeight > bounds.y + bounds.h;


            // Top of a rect.
            if (touchingTopOfRect) {
                obj->m_dY = bounds.y - obj->m_dHeight;
                obj->bottomContact = true;
            }

            // Bottom of Rect
            if (touchingBottomOfRect) {
                obj->m_dY = bounds.y + bounds.h;
                obj->topContact = true;
            }

            // Left Side of rect.
            if (touchingLeftOfRect) {
                obj->m_dX = bounds.x - obj->m_dWidth;
                obj->rightContact = true;
            }

            // Right Side of rect
            if (touchingRightOfRect) {
                printf("HIT RIGHT OF A OBSTACLE %d\n", bounds.w);

                obj->m_dX = bounds.x + bounds.w;
                obj->leftContact = true;
            }
        }
}

所以这会在每一帧调用,并将循环遍历一个数组,该数组存储我想从对象可移动区域中排除的矩形。

使用这种方法开始工作变得非常棘手,很多时候条件句在它们不应该触发的时候触发。但是我的主要问题是这对我来说似乎不必要地复杂。有没有更好的方法来解决这个问题?

最佳答案

您应该能够使用 SDL_HasIntersection

大大简化此操作

基本的伪代码将遵循

for each collider rect
  if HasIntersection( bounds, colliderBounds )
    find the centre points of each rect (x+w/2, y+h/2)
    bool touchingLeft = ( bounds_centre.x < collider_centre.x )
    bool touchingRight = ( bounds_cenre.x > collider_centre.x )
    ... etc ...

http://wiki.libsdl.org/SDL_HasIntersection

编辑:另一种选择是自己跳过任何碰撞检测,而是使用出色的 Box2D:

http://box2d.org/

关于c++ - 从游戏中的可移动空间中删除矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22324511/

相关文章:

c++ - 如何为 editline 应用程序的提示着色

c++ - 如何在类中使用结构的 std::unique_ptr?

c++ - 调用未知 child 的构造函数

c++ - 如何在每个 ';' 处换行,保存在 char* 中

Java Swing : how to smoothly animate/move component

java - 构建音乐可视化工具

javascript - 拉斐尔动画

swift - 在将节点之一从屏幕上删除之前,如何要求其发生两次碰撞?

java - 平台碰撞和跳跃的奇怪行为

iphone - 花栗鼠碰撞检测