AI follow and avoid collision with obstacle

Viewed 307

I'm making an AI navigation system based on polar coordinates. The purpose is to move an actor to a position, while also moving away from a possible obstacle on its path.

The code works perfectly most of the time but after testing, I discovered this: when the player, obstacle and actor are all perfectly aligned in either the X or Y direction or diagonally, the actor gets stuck in the obstacle. It's mostly noticeable when the player is "hugging" a wall because the actor's movement vector is clipped by the walls, making them aligned.

Click the buttons in the snippet to see what I'm on about.

Is there a way to prevent this?

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

let playerX = 100;
let playerY = 200;

let obstacleX = 200;
let obstacleY = 200;

let actorX = 300;
let actorY = 201;


function loop() {
    // Wall clipping
    if (actorX > 490) {
        actorX = 490;
    } else if (actorX < 10) {
        actorX = 10;
    }

    if (actorY > 490) {
        actorY = 490;
    } else if (actorY < 10) {
        actorY = 10;
    }

    // Vector between player and actor
    let vectorPlayerX = playerX - actorX;
    let vectorPlayerY = playerY - actorY;

    // Vector between obstacle and actor
    let vectorObstacleX = obstacleX - actorX;
    let vectorObstacleY = obstacleY - actorY;

    // Where to move, defaults to player's position
    const anglePlayer = Math.atan2(vectorPlayerY, vectorPlayerX);
    let moveAngle = anglePlayer;

    // If near obstacle, adjust course and try to avoid it
    if (Math.sqrt(vectorObstacleX * vectorObstacleX + vectorObstacleY * vectorObstacleY) < 50) {
        const angleObstacle = Math.atan2(vectorObstacleY, vectorObstacleX);
        moveAngle += anglePlayer - angleObstacle;
    }

    // Move the vector to desired location
    actorX += Math.cos(moveAngle);
    actorY += Math.sin(moveAngle);

    //Drawing
    ctx.clearRect(0, 0, 500, 500);

    ctx.beginPath();
    ctx.fillStyle = "gray";
    ctx.arc(actorX, actorY, 10, 0, Math.PI * 2, true);
    ctx.fill();

    ctx.beginPath();
    ctx.fillStyle = "orange";
    ctx.arc(obstacleX, obstacleY, 10, 0, Math.PI * 2, true);
    ctx.fill();

    ctx.beginPath();
    ctx.fillStyle = "blue";
    ctx.arc(playerX, playerY, 10, 0, Math.PI * 2, true);
    ctx.fill();

    requestAnimationFrame(loop);
}

requestAnimationFrame(loop);


function nonAligned() {
    playerX = 100;
    playerY = 200;

    obstacleX = 200;
    obstacleY = 200;

    actorX = 300;
    actorY = 201;
}

function alignedY() {
    playerX = 100;
    playerY = 490;

    obstacleX = 200;
    obstacleY = 490;

    actorX = 300;
    actorY = 490;
}

function alignedBoth() {
    playerX = 200;
    playerY = 200;

    obstacleX = 300;
    obstacleY = 300;

    actorX = 400;
    actorY = 400;
}
#options {
    position: fixed;
    top: 5px;
    left: 5px;
}
<!DOCTYPE html>
<html>
<body>
    <canvas id="canvas" width="500" height="500"></canvas>
<div id="options">
    <button onclick="nonAligned()">Spawn non-aligned</button>
    <button onclick="alignedY()">Spawn Y aligned</button>
    <button onclick="alignedBoth()">Spawn diagonally aligned</button>
</div>
</body>
</html>

2 Answers

If the angle to the player and the obstacle are the same then we continue the course, as the variables cancel each other out.

    moveAngle += anglePlayer - angleObstacle;

If anglePlayer is 117 and angleObstacle is 117 and your moveAngle is 117 you get

   117 + 117 -117 = 117 ...

You might want something like this (pseudo code)

    moveAngle += anglePlayer + random(90)-45;

Or if hitting an obstacle move left or right

    moveAngle += anglePlayer - 90;
    if (random(2)==1 moveAngle += 180

The issue is indeed that moveAngle is unchanged when it is pointed directly at the obstacle. A small modification checks whether the moveAngle is clockwise or counter-clockwise from the obstacle, and veers further away (note: my code breaks the wall hugging and behaves badly in the "aligned in Y" case for that reason, which is fixable but I don't care to):

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

let playerX = 100;
let playerY = 200;

let obstacleX = 200;
let obstacleY = 200;

let actorX = 300;
let actorY = 201;


function loop() {
    // Wall clipping
    if (actorX > 490) {
        actorX = 490;
    } else if (actorX < 10) {
        actorX = 10;
    }

    if (actorY > 490) {
        actorY = 490;
    } else if (actorY < 10) {
        actorY = 10;
    }

    // Vector between player and actor
    let vectorPlayerX = playerX - actorX;
    let vectorPlayerY = playerY - actorY;

    // Vector between obstacle and actor
    let vectorObstacleX = obstacleX - actorX;
    let vectorObstacleY = obstacleY - actorY;

    // Where to move, defaults to player's position
    const anglePlayer = Math.atan2(vectorPlayerY, vectorPlayerX);
    let moveAngle = anglePlayer;

    // If near obstacle, adjust course and try to avoid it
    obs_distance = Math.sqrt(vectorObstacleX * vectorObstacleX + vectorObstacleY * vectorObstacleY);
    if (obs_distance < 100) {
        const angleObstacle = Math.atan2(vectorObstacleY, vectorObstacleX);
        delta = Math.PI/2.0;
        if (obs_distance > 100.0/32.0) { delta = (100.0/32.0)*Math.PI/obs_distance; }
        cross = Math.sin(moveAngle-angleObstacle);
        if (cross>0) { moveAngle += delta; }
        if (cross<0) { moveAngle -= delta; }
        if (cross==0) {
           if (Math.random(2)==1) {
             moveAngle += delta;
           } else {
             moveAngle -= delta;
           }
        }
    }

    // Move the vector to desired location
    actorX += Math.cos(moveAngle);
    actorY += Math.sin(moveAngle);

    //Drawing
    ctx.clearRect(0, 0, 500, 500);

    ctx.beginPath();
    ctx.fillStyle = "gray";
    ctx.arc(actorX, actorY, 10, 0, Math.PI * 2, true);
    ctx.fill();

    ctx.beginPath();
    ctx.fillStyle = "orange";
    ctx.arc(obstacleX, obstacleY, 10, 0, Math.PI * 2, true);
    ctx.fill();

    ctx.beginPath();
    ctx.fillStyle = "blue";
    ctx.arc(playerX, playerY, 10, 0, Math.PI * 2, true);
    ctx.fill();

    requestAnimationFrame(loop);
}

requestAnimationFrame(loop);


function nonAligned() {
    playerX = 100;
    playerY = 200;

    obstacleX = 200;
    obstacleY = 200;

    actorX = 300;
    actorY = 201;
}

function alignedY() {
    playerX = 100;
    playerY = 490;

    obstacleX = 200;
    obstacleY = 490;

    actorX = 300;
    actorY = 490;
}

function alignedBoth() {
    playerX = 200;
    playerY = 200;

    obstacleX = 300;
    obstacleY = 300;

    actorX = 400;
    actorY = 400;
}
#options {
    position: fixed;
    top: 5px;
    left: 5px;
}
<!DOCTYPE html>
<html>
<body>
    <canvas id="canvas" width="500" height="500"></canvas>
<div id="options">
    <button onclick="nonAligned()">Spawn non-aligned</button>
    <button onclick="alignedY()">Spawn Y aligned</button>
    <button onclick="alignedBoth()">Spawn diagonally aligned</button>
</div>
</body>
</html>

Related