高级碰撞检测?

时间:2016-01-29 10:08:42

标签: c++ sfml

您好我正在制作类似于塞尔达传说的角色扮演游戏。我的游戏中有一个功能,当玩家用剑攻击敌人时,敌人会被击退n个单位。我有一个碰撞检测,有时按预期工作,有时敌人穿过墙壁并卡在另一边,然后敌人可以简单地穿过墙壁。如果可能的话,让敌人在与墙碰撞时向玩家移动将是我相信这个问题的一种可能的解决方案,但我不知道如何实现这一点。这是我目前的碰撞代码:

// Enemy Collides with Wall
        counter1 = 0;
        for (iter4 = enemyArray.begin(); iter4 != enemyArray.end(); iter4++)
        {
            counter2 = 0;
            for (iter15 = wallArray.begin(); iter15 != wallArray.end(); iter15++)
            {
                if (enemyArray[counter1].rect.getGlobalBounds().intersects(wallArray[counter2].rect.getGlobalBounds()))
                {
                    enemyArray[counter1].isCollided = true;
                        //Hit Wall
                    if ((enemyArray[counter1].direction == 1 || enemyArray[counter1].rect.getPosition().y >= wallArray[counter2].rect.getPosition().y)) //up
                        {
                            enemyArray[counter1].canMoveUp = false;
                            enemyArray[counter1].canMoveLeft = false;
                            enemyArray[counter1].canMoveRight = false;
                            enemyArray[counter1].rect.move(0, 7);
                        }
                    else if ((enemyArray[counter1].direction == 2 || enemyArray[counter1].rect.getPosition().y <= wallArray[counter2].rect.getPosition().y)) //Down
                        {
                            enemyArray[counter1].canMoveDown = false;
                            enemyArray[counter1].canMoveRight = false;
                            enemyArray[counter1].canMoveLeft = false;
                            enemyArray[counter1].rect.move(0, -7);
                        }
                    else if ((enemyArray[counter1].direction == 3 || enemyArray[counter1].rect.getPosition().x >= wallArray[counter2].rect.getPosition().x)) //Left
                        {
                            enemyArray[counter1].canMoveLeft = false;
                            enemyArray[counter1].canMoveUp = false;
                            enemyArray[counter1].canMoveDown = false;
                            enemyArray[counter1].rect.move(7, 0);
                        }
                    else if ((enemyArray[counter1].direction == 4 || enemyArray[counter1].rect.getPosition().x <= wallArray[counter2].rect.getPosition().x)) //Right
                        {
                            enemyArray[counter1].canMoveRight = false;
                            enemyArray[counter1].canMoveUp = false;
                            enemyArray[counter1].canMoveDown = false;
                            enemyArray[counter1].rect.move(-7, 0);
                        }
                }
                counter2++;
            }
            counter1++;
        }



//Knock Back enemy away from sword && sword2
        counterKnockBack++;
        counter2 = 0;
        for (iter4 = enemyArray.begin(); iter4 != enemyArray.end(); iter4++)
        {
                    if (enemyArray[counter2].knockback == true)
                    {
                        if (enemyArray[counter2].isCollided == false)
                        {
                            if ((Player1.rect.getPosition().y > enemyArray[counter2].rect.getPosition().y))
                            {
                                enemyArray[counter2].rect.move(0, -3);  //up
                            }
                            else if ((Player1.rect.getPosition().y < enemyArray[counter2].rect.getPosition().y))
                            {
                                enemyArray[counter2].rect.move(0, 3);    //down
                            }
                            if ((Player1.rect.getPosition().x > enemyArray[counter2].rect.getPosition().x))
                            {
                                enemyArray[counter2].rect.move(-3, 0);   //left
                            }
                            else if ((Player1.rect.getPosition().x < enemyArray[counter2].rect.getPosition().x))
                            {
                                enemyArray[counter2].rect.move(3, 0);    //right
                            }
                            if (counterKnockBack >= 20)
                            {
                                enemyArray[counter2].knockback = false;
                            }
                        }
                    }
            counter2++;
        }

        //turn off collided counter
        counterCollided++;
        counter2 = 0;
        for (iter4 = enemyArray.begin(); iter4 != enemyArray.end(); iter4++)
        {
            if (enemyArray[counter2].isCollided == true)
            {
                if (counterCollided >= 30)
                    enemyArray[counter2].isCollided = false;
            }
            counter2++;
        }

我不知道为什么敌人有时可以简单地穿过墙壁而不被撞倒。那我怎么能解决这个问题呢?

1 个答案:

答案 0 :(得分:1)

如果不完全阅读代码,我已经可以告诉你,你的碰撞检测代码是错误的。主要是因为您直接移动对象,可能没有检查rect::move函数中的碰撞。

rect::move函数可能会将对象移动到墙上而不会触发任何碰撞反应代码。请考虑以下情形:

第一帧:
enter image description here
enemyArray[counterX].rect.move(3, 0);

第二帧:
enter image description here

敌人物体在墙后移动,不会触发碰撞检测码。

我的建议是(尽管显而易见:阅读some books):对于每个敌人存储其先前的位置并检查敌人与墙壁的矩形之间但在墙与矩形之间的碰撞两个敌人阵地之间。这样的事情:
enter image description here

当然,这只是碰撞检测代码容易出错的可能情况之一。