1
\$\begingroup\$

I have a simple little game with one character as the player and four enemy characters. I created a very basic AI, that moves towards the player when the player is nearby. That all worked fine, but when I added AI-AI collision (using a bounding box model), it slowed it down tremendously, and exponentially the more AI there are. With four AI, they move quite slowly. 3 AI is a bit better. 2 is just perfect, and 1 is too fast.

I have the following function to calculate the AI movement when near a player.

function updateAI() {
    for (i = 0; i < aiCount; i++) {
        if (aiCounterAI >= aiCount) {
            aiCounterAI = 0;
        }
        checkArmyAIcol = armyAI[aiCounterAI][1];
        checkArmyAIrow = armyAI[aiCounterAI][2];
        enemySpeed = enemies[armyAI[aiCounterAI][0][0]][3];
        enemyPlayerCollision = false;
        if (playerBattle.xCoord - 6 <= checkArmyAIcol && playerBattle.xCoord + 6 >= checkArmyAIcol) {
            if (playerBattle.yCoord - 6 <= checkArmyAIrow && playerBattle.yCoord + 6 >= checkArmyAIrow) {
                if (playerBattle.x < armyAI[aiCounterAI][3] - 48) {
                    armyAI[aiCounterAI][3] = armyAI[aiCounterAI][3] - enemySpeed;
                    aiDirection = 'left';
                }
                if (playerBattle.x > armyAI[aiCounterAI][3] + 48) {
                    armyAI[aiCounterAI][3] = armyAI[aiCounterAI][3] + enemySpeed;
                    aiDirection = 'right';
                }
                if (playerBattle.y < armyAI[aiCounterAI][4] - 48) {
                    armyAI[aiCounterAI][4] = armyAI[aiCounterAI][4] - enemySpeed;
                    aiDirection = 'up'; 
                }
                if (playerBattle.y > armyAI[aiCounterAI][4] + 48) {
                    armyAI[aiCounterAI][4] = armyAI[aiCounterAI][4] + enemySpeed;
                    aiDirection = 'down'; 
                }
                checkBattleCollision('ai',aiCounterAI);
                armyAI[aiCounterAI][1] = Math.ceil(armyAI[aiCounterAI][3] / 48);
                armyAI[aiCounterAI][2] = Math.ceil(armyAI[aiCounterAI][4] / 48);
            }
        }
        aiCounterAI++;
    }
}

And finally I have this function to calculate the AI collision.

if (type == 'ai') {
    enemyEnemyCollision = false;
    if (aiCount > 1) {
    checkArmyAIcol1 = armyAI[ai][1];
    checkArmyAIrow1 = armyAI[ai][2];
    checkArmyAIx1 = armyAI[ai][3];
    checkArmyAIy1 = armyAI[ai][4];
    var aiCounter2 = 0;
    for (i = 0; i < aiCount; i++) {
        if (aiCounter2 != ai) {
            checkArmyAIcol2 = armyAI[aiCounter2][1];
            checkArmyAIrow2 = armyAI[aiCounter2][2];
            checkArmyAIx2 = armyAI[aiCounter2][3];
            checkArmyAIy2 = armyAI[aiCounter2][4];
            // Check if the AI is near the other AI before checking if collision is true
            if (checkArmyAIcol1 - 1 <= checkArmyAIcol2 && checkArmyAIcol1 + 1 >= checkArmyAIcol2) {
                if (checkArmyAIrow1 - 1 <= checkArmyAIrow2 && checkArmyAIrow1 + 1 >= checkArmyAIrow2) {
                    if (checkArmyAIx1 < checkArmyAIx2 + 48 &&
                        checkArmyAIx1 + 48 > checkArmyAIx2 &&
                        checkArmyAIy1 < checkArmyAIy2 + 48 &&
                        checkArmyAIy1 + 48 > checkArmyAIy2) {
                        enemyEnemyCollision = true;
                        checkEnemyEnemyCollision(ai,aiCounter2);
                    }
                }
            }
        }
        aiCounter2++;
    }
}
}

function checkEnemyEnemyCollision(enemy1,enemy2) {
enemySpeed = enemies[armyAI[enemy1][0][0]][3];
    if (enemyEnemyCollision == true) {
        if (aiDirection == 'left') {
            armyAI[enemy1][3] = armyAI[enemy1][3] + enemySpeed;}
        if (aiDirection == 'right') {
            armyAI[enemy1][3] = armyAI[enemy1][3] - enemySpeed;}
        if (aiDirection == 'up') {
            armyAI[enemy1][4] = armyAI[enemy1][4] + enemySpeed;}
        if (aiDirection == 'down') {
            armyAI[enemy1][4]= armyAI[enemy1][4] - enemySpeed;}
        console.log("ya'll collided ya clumsy potatoes");
    }
}

The updateAI function is fast and runs great. Adding the collision (using a bounding box model) as said before slows it down a lot. These functions are called sixty times a second, through requestAnimationFrame in my gameloop. My guess is that it can't keep up fast enough in the collision with the frame rate so they just can't move much as they could otherwise. Yet, I don't know how to fix this. Does anyone have any suggestions? Bounding box collision and multiple moving items is new territory for me so I wouldn't mind suggestions on improving my code.

\$\endgroup\$
  • 1
    \$\begingroup\$ Start by measuring bottlenecks using the built-in devtools profiler and timeline panels. Otherwise, one can only guess. \$\endgroup\$ – wOxxOm Oct 16 '16 at 15:13
  • 1
    \$\begingroup\$ It is basically impossible to review this code. Your armyAI variable seems key to this whole thing and it is not clear what is in this data structure as we don't see where it is defined nor have any idea what is stashed away in all those numeric indexes . You REALLY need to think about using more meaningful variable names, and think about defining things like meaningful coordinate classes and such to give better structure to this code. \$\endgroup\$ – Mike Brant Oct 17 '16 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.