繁体   English   中英

C ++ DirectX11 2d游戏:阻止敌方精灵相互移动吗?

[英]C++ DirectX11 2d Game: Stopping enemy sprites moving over each other?

我正在使用IntersectsWith(this->boundingBox))方法来检测精灵与播放器之间的碰撞。 我希望能够以某种方式使用这种方法来检测我的敌方精灵相互碰撞,并在它们确实这样做时确保它们不会彼此移动。

所有敌人的精灵都会跟随玩家。

MainGame.cpp

循环遍历向量中的每个敌人并执行更新循环:

for (auto &enemyMobsObj : this->enemyMobs)
        {

            enemyMobsObj->Update(tickTotal, tickDelta, timeTotal, timeDelta, windowBounds, this->ship, this->firstBoss,
                this->enemyMobs, this->bullets, this->missiles, NULL, "NULL", "NULL");
        }

这是我之前尝试停止每个精灵彼此移动的方法:

EnemyMobOne ::更新:

int nextEnemy;

    for (int i = 0; i < enemyMobOne.size(); i++)
    {
        nextEnemy = i + 1;
        if (nextEnemy < enemyMobOne.size())
        {
            //Deal with mobs collision
            if (enemyMobOne[i].boundingBox.IntersectsWith(enemyMobOne[nextEnemy].boundingBox))
            {
                enemyMobOne[i].position.x = enemyMobOne[nextEnemy].position.x - enemyMobOne[i].boundingBox.Width;
            }
        }
    }   

但是,这使每个敌人的精灵显然彼此粘在一起,看起来不正确,也使它们瞬移。

有人知道正确的代码以阻止它们相互移动吗? 谢谢。

当您检测到两个碰撞对象之间的相交时,您需要决定如何抵消重叠(如我所确定的那样)。 但是,与仅将它们“推”到一侧(就像您在代码中所做的那样)相比,这样做的技巧有些棘手。 可以这么说,您可能想要做的是对所施加的力施加反作用力。 基本上,您要计算最小平移量或所需最少移动量的方向,以获取至少一个框来移出另一个框。

这比简单地“让我在另一个人的右边(或者,取决于您如何设置坐标,我想)在我的另一边”(这差不多就是您的代码现在所做的)要复杂得多。

对于一个简单的解决方案,只需检查一个对撞机是否更靠近另一个对撞机的左,右,上或下。 为此,您可以简单地获取碰撞相交位置,并检查该点与相对于一个对撞机的最小和最大x和y坐标之间的相对距离,然后相应地移动一个或两个精灵。

例如:[编辑]在回顾了我以前的回答后,我意识到您需要计算方框的重叠量,这将使完成这些操作变得更加容易,如下所示:

float minX = min(sprite0.boundingBox.maxX, sprite1.boundingBox.maxX);// Minimum of the boxes' right-side points (top right and bottom right) x coordinates
float minY = min(sprite0.boundingBox.maxY, sprite1.boundingBox.maxY);// Minimum of the boxes' top-side points (top left and top right) y coordinates

float maxX = max(sprite0.boundingBox.minX, sprite1.boundingBox.minX);// Maximum of the boxes' left-side points (top left and bottom left) x coordinates
float maxY = max(sprite0.boundingBox.minY, sprite1.boundingBox.minY);// Maximum of the boxes' bottom-side points (bottom left and bottom right) y coordinates

float distHoriz = minX - maxX;// The horizontal intersection distance
float distVert = minY - maxY;// The vertical instersection distance

// If the boxes are overlapping less on the horizontal axis than the vertical axis,
// move one of the sprites (in this case, sprite0) in the opposite direction of the
// x-axis overlap
if(abs(distHoriz) < abs(distVert))
{
    sprite0.x -= distHoriz;
}
// Else, move one of the sprites (again, I just decided to use sprite0 here,
// arbitrarily) in the opposite direction of the y-axis overlap
else
{
    sprite0.y -= distVert;
}

为了进一步澄清(除了注释之外),我们在这里基本上要做的是检查重叠线之间的距离。 例如:

Box 0 x-axis  xmin0|------------------|xmax0
Box 1 x-axis                xmin1|----------------------|xmax1
                                 |----|<-- Overlap (xmax0 - xmin1)

请注意,来自两个边界框的用于重叠的最小值是两个最小值(xmin0和xmin1)中的最大值,用于重叠的最大值是两个最大值(xmax0和xmax1)中的最小值。

y轴计算的工作方式完全相同。 一旦有了两个轴,我们只需检查一下哪个轴的绝对值较低(哪个距离更短),然后沿该距离移动以抵消相交。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM