# GML Enemy ai?

Discussion in 'Programming' started by corwin22, Jun 15, 2018.

1. ### corwin22Member

Joined:
Nov 18, 2017
Posts:
73
I have been trying to figure out how to make an enemy be able to shoot towards the player and at the same time figure out how to get the bounce (bullets bounce once of walls once before disappearing) the enemies would work similar to the Wii tank game but I have no way of finding out how to get them to shoot the correct direction or get them to move the correct ways (they see player from start).

Joined:
Jul 12, 2016
Posts:
713
3. ### corwin22Member

Joined:
Nov 18, 2017
Posts:
73
i know how to make the bouncing my main issue is getting the enemy to use bouncing to its advantage.

Joined:
Jun 18, 2017
Posts:
823

5. ### BentleyMember

Joined:
Jun 18, 2017
Posts:
823
What do you mean by that?

6. ### corwin22Member

Joined:
Nov 18, 2017
Posts:
73
i want to try and find out how to have the enemy shoot towards the wall so that it will bounce and hit the player.

7. ### BentleyMember

Joined:
Jun 18, 2017
Posts:
823
Ah. Hmm. Well, you could "cheat" and actually change the direction of the bullet once it hits the wall. But if you want to plan it out so the trajectory remains after the bounce, than I don't know.

EvanSki likes this.
8. ### NightFrostMember

Joined:
Jun 24, 2016
Posts:
2,009
I suppose you could do the bounce check with trigonometry. The check is two triangles: hypotenuse of the first is the line from enemy to bounce point at the wall, and hypotenuse of the other is the line from bounce point to player. They share a common side that extends at right angle from the wall at the bounce point. For the triangle that represents the bullet coming from enemy to wall, you must solve the angle of the corner that touches the wall. Then do the same for the other triangle. (You can solve these since you know the lengths of all three sides.) If the angles match, the bullet will bounce to the player from that wall position. You must solve this for every wall pixel the enemy has a line of sight to, in order to find if any of them will bounce directly at the player. There's probably some optimization opportunities there, discarding wall positions that have no chance for a bounce, based on results of nearby calculations.

Edit: added a simple illustration for clarity.

Edit2: actually, looking at the image, it might be better to create the enemy triangle by imagining the enemy position at the same x- or y-coordinate as the player (for vertical and horizontal bounces respectively) and then place the shared side at their halfway point. Then you just check along the shared side both ways for walls, check that there's a line of sight at it from player and enemy, and if all checks pass, you have found a bounce point.

#### Attached Files:

• ###### illustration.jpg
File size:
60.4 KB
Views:
5
Last edited: Jun 15, 2018