0 Favourites

# Rotate-Fire-Rotate-Move AI

• 7 posts
• This is part how-to, part discussion and theorycrafting on AI.

The built-in pathfinding AI is excellent for finding a path towards a point, but I would like to delve a bit further into AI behaviors that can be created from this. In particular, I am aiming to construct an enemy that rotates towards the player, fires a bullet, then rotates to some other direction and moves, and repeats this behavior. This is more advanced than simply having pathfinding move the enemy towards the player.

The main issue to deal with is dynamically generating a 'move point' for the enemy that is not in the current path of the player. Not being in the current path is important, otherwise the enemy will be moving to be shot by the player (dumb AI). One implementation of this I can think of is drawing an invisible line in the direction of the current player angle and seeing if the proposed 'move point' intersects with it, but this could be costly in terms of performance.

For the other aspects of this behavior, pathfinding takes care of finding a path to the desired 'move point', and instance variables can handle the timings of the rotate-fire actions.

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Bump, anyone?

I'm currently thinking of pinning an invisible object at X distance and at some angle away from the ship's current angle when it turns to fire at the player. The ship would then move towards this invisible object when not firing.

Anyone else have ideas on how this would be implemented?

• I had typed up all my notes, but the 'Authorization Error' I seem to get every now and then on this forum happened, and I'd rather not type it all up again (why isn't this fixed?).

Here is a brief version of my notes on this.

Two States:

-Move (starting state)

-Fire

Fire:

-If in Fire, set variable PredictedFiringAngle = angle(Self.X, Self.Y, Player.X, Player.Y) + asin((Player.Speed * sin(angle(Self.X, Self.Y, Player.X, Player.Y) + 180 - Player.MovingAngle))/ SelfBullet.Speed)

-Update this variable before every fired shot

-If Player is within 50 pixels of self, do not end this state, instead just keep firing at the object's fire rate

-If Player's angle = angle(Self.X, Self.Y, Player.X, Player.Y) + or - 10 degrees for 1 second or more then Player is facing the AI and the AI should turn and fire back.

-If Player is beyond 50 pixels of self, set state to Move after several shots

Move:

-If Player's angle = angle(Self.X, Self.Y, Player.X, Player.Y) + or - 10 degrees for 1 second or more then Player is facing the AI and most likely firing, so enter Fire state.

-If Player is within 50 pixels of the AI, immediately enter Fire state and fire upon the player.

-AI should try to move behind the player and avoid flying into the path of the player (avoid dumb AI)

-???

Any thoughts on this?

• To further clarify, I have created images showing how the AI should behave according to certain conditions:

<img src="http://i.imgur.com/eNay9B6.png" border="0" />

In the situations above, even if the player turns, the AI should still end up positioned behind the player or head-on with the player (the latter of which causes the AI to enter 'fire' mode and try to just shoot down the player).

This implementation should be easy - just have pathfinding find an optimal path to the player's current position.

<img src="http://i.imgur.com/dj1TRrB.png" border="0" />

If the angle from the AI's position to the player is less than 45 degrees, the best course of action is to simply engage the player since the player is most likely going to be attacking the AI ship soon.

So the hard part now is determining the perpendicular position.

Once this is all set up, it shouldn't be too hard to set up a random counter to make the AI ship run away sometimes as well.

The question is: How do I determine the 'spot' mathematically?

• Perpendicular positions are easy. If you have a vector (2,1), you simply swap the two numbers and make one negative. For example, (-1, 2), and the opposite side is then (1,-2). ;)

• Another tip on the AI: You can fake it a bit with a trail behind the enemy as well. Use angles to avoid the enemy as you stated, but create an invisible trail of 3-5 invisible sprite dots behind the enemy. If spots are seen, the AI can go to them and follow each one towards the first, making it seems like it's following. Then, once the angle is right, the AI shoots. ;)

• Another tip on the AI: You can fake it a bit with a trail behind the enemy as well. Use angles to avoid the enemy as you stated, but create an invisible trail of 3-5 invisible sprite dots behind the enemy. If spots are seen, the AI can go to them and follow each one towards the first, making it seems like it's following. Then, once the angle is right, the AI shoots. ;)

This seems a bit inefficient. If the angle >= 90, just mark the player's current position as soon as you go into the Move state and have the AI pathfind to that spot.

• 7 posts