HTML5 2D Game - 100% Accurate AI?

482 Views Asked by At

I've been recreating the classic Asteroids in JavaScript using canvas and am pretty new to game dev. I was wondering is it possible to create an AI that has a 100% success rate of hitting the player (assuming the enemy's bullet can travel indefinitely)? I want to be able to control how accurate the enemy AI can be from completely inaccurate to perfectly accurate - the shot it takes guarantees to hit the player no matter the relative speeds of the enemy and player object.

I have a rough (and simple) example of what I'm looking for on YouTube. As you can see it does a relatively good job of predicting where the player will be next but it is no way perfect (I'm also aware of some bullet speed bugs in that video too, and the UFO shoots in random directions if the player has been destroyed just FYI).

The function I used was this:

function angleBetween( p1, vel1, p2, vel2 ) {
    var relativePoint = {
        x: ( p2.x + vel2.x ) - ( p1.x + vel1.x ),
        y: ( p2.y + vel2.y ) - ( p1.y + vel1.y )
    };

    return Math.atan2( -relativePoint.y, -relativePoint.x );
}

p1 & vel1 being the x,y position and velocity of first object,

p2 & vel2 being the x,y position and velocity of the second object.

So basically is there a way to improve on what I've done so that it will only ever need to take one shot to hit the player taking into account the relative speeds of the objects in question?

Thanks in advance and apologies if I wasn't clear.

0

There are 0 best solutions below