Rotate-Fire-Rotate-Move AI

Get help using Construct 2

Post » Sat Apr 13, 2013 2:55 am

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.Excal2013-04-13 02:56:43
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194

Post » Sat Apr 13, 2013 11:14 pm

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?Excal2013-04-13 23:15:06
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194

Post » Thu Apr 18, 2013 7:14 am

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?Excal2013-04-18 09:54:11
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194

Post » Thu Apr 18, 2013 9:06 am

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



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.



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?Excal2013-04-18 09:55:40
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194

Post » Thu Apr 18, 2013 10:38 am

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). ;)JoyfulDreamer2013-04-18 10:39:28
Jack of all trades, and master of some.
B
29
S
9
G
7
Posts: 174
Reputation: 7,601

Post » Thu Apr 18, 2013 10:57 am

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. ;)
Jack of all trades, and master of some.
B
29
S
9
G
7
Posts: 174
Reputation: 7,601

Post » Thu Apr 18, 2013 12:13 pm

[QUOTE=JoyfulDreamer] 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. ;)[/QUOTE]

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.
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194


Return to How do I....?

Who is online

Users browsing this forum: HeinRich, Zakeru and 24 guests