Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在游戏中从可移动空间移除矩形_C++_Animation_Collision Detection - Fatal编程技术网

C++ 在游戏中从可移动空间移除矩形

C++ 在游戏中从可移动空间移除矩形,c++,animation,collision-detection,C++,Animation,Collision Detection,我有一个简单的设置,有一个球在屏幕上弹跳,我想能够给对象矩形,它不能在里面移动(他们将作为障碍物)。目前,我们有以下逻辑来尝试实现这一点 void AnimatableObject::ExcludeRects(AnimatableObject *obj) { for (int i = 0; i < obj->numberOfExclusionBounds; i++) { SDL_Rect bounds = obj->exclusionBo

我有一个简单的设置,有一个球在屏幕上弹跳,我想能够给对象矩形,它不能在里面移动(他们将作为障碍物)。目前,我们有以下逻辑来尝试实现这一点

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;
            }
        }
}
void AnimatableObject::excludects(AnimatableObject*obj){
对于(int i=0;inumberOfExclutionBounds;i++){
SDL_Rect bounds=obj->exclusionBounds[i];
/*触摸直肠两侧*/
bool touchingtopfrect=obj->m_dY+obj->m_dHeight>bounds.y&&
obj->m_dYm_dX>bounds.x&&
obj->m_dX+obj->m_dWidthm_dYm_dY>bounds.y&&
obj->m_dX>bounds.x&&
obj->m_dX+obj->m_dWidthm_dXm_dX>bounds.x&&
obj->m_dY>bounds.y&&
obj->m_dY+obj->m_dHeightm_dX+obj->m_dWidth>bounds.x&&
obj->m_dX+obj->m_dWidthm_dY>bounds.y&&
obj->m_dY+obj->m_dHeight>bounds.y+bounds.h;
//矩形的顶部。
如果(触摸触摸触摸屏){
obj->m_dY=bounds.y-obj->m_dHeight;
obj->bottomContact=true;
}
//直肠底部
if(接触CT底部){
obj->m_dY=bounds.y+bounds.h;
obj->topContact=true;
}
//直肠左侧。
如果(触碰左侧){
obj->m_dX=bounds.x-obj->m_dWidth;
obj->rightContact=true;
}
//直肠右侧
if(触碰权){
printf(“击中障碍物右侧%d\n”,bounds.w);
obj->m_dX=bounds.x+bounds.w;
obj->leftContact=true;
}
}
}
这就是所谓的每一帧,它将循环通过一个数组来存储我想要从对象可移动区域中排除的矩形


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

您应该能够使用
SDL\u 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 ...
每个碰撞器矩形的

if HasIntersection(边界、碰撞边界)
找到每个矩形的中心点(x+w/2,y+h/2)
bool touchingLeft=(边界_center.x<对撞机_center.x)
bool touchingRight=(边界>碰撞器>中心x)
... 等

编辑:另一个选项是跳过自己进行的任何碰撞检测,而是使用优秀的Box2D: