GMS 2.3+ Leading Shots with Bouncing Projectiles

This is mainly something that has peaked my interest in the last couple months and I have been trying to think of a way to do it but hitting a brick wall. So any suggestions would be greatly appreciated if anyone has done this before or has any ideas...

So currently I have Enemies in my game that are able to predict player movement and (if the bullet will reach them without hitting a wall) shoot in that direction effectively letting them lead shots. This system is great but it has one flaw which is that my projectiles can ricochet off a wall once. Currently I simply use move_bounce_solid(0) to do this although switching to something more reliable may be needed whilst doing this. The issue with this is that the Enemies effectively can't use the ricochet since they can only predict the player's with the projectile movements before the bounce. What I would like to do is combine these, so have the AI be able to predict where the Player will go and if it can, bounce a projectile to that position effectively to intercept them. If this doesn't make sense please let me know and I'll try clarify further since it is kind of hard to explain.

I currently see two ways of doing it currently, the first is a kind of brute force which is to simulate lines coming out of the enemy in all directions (kind of like rays) and if these rays bounce and are close to the player's future position in the time it would take the bullet to travel there, feedback that direction to the AI and tell it to shoot. This method would work but due to all the collision checking, I feel like this will really drain performance and since I'm already using lots of pathfinding functions, I need all the optimisation I can get.

The other option would be to just draw one line which would be the future position of the player and try to connect that to the Enemy's current position. The main issue is to get the Player's predicted path, I need to know how long the travel time would be and to know that, I need to know where the end-point is and to know that I need to know the travel time etc. As far as I can tell there is no solution but I could be thinking about it wrong.

All I need to do is supply my Enemies with a direction, once they have that, they can take care of the rest, it's just getting that direction that's hard. Any suggestions would be appreciated. Thanks for reading
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
I currently see two ways of doing it currently, the first is a kind of brute force which is to simulate lines coming out of the enemy in all directions (kind of like rays) and if these rays bounce and are close to the player's future position in the time it would take the bullet to travel there, feedback that direction to the AI and tell it to shoot. This method would work but due to all the collision checking, I feel like this will really drain performance and since I'm already using lots of pathfinding functions, I need all the optimisation I can get.
This is called "ray-casting" and is how I would personally do it. You can cut down on collision checking using an optimised script like the collision_line_first script I've posted elsewhere on this this forum: https://forum.yoyogames.com/index.p...d-with-rotating-laser-beam.84351/#post-504911 Also note that you can limit this by not checking every direction. Do an approximation first... Is the player on the left or the right of the enemy? Left, then only check 90 - 270º... This could probably be further reduced by checking if the player is above or below the enemy... Above? And on the left? Only check 90 - 180º! You then simply subdivide this angle into the rays you want to check that make sense... Maybe 10? Maybe 15? Maybe 6? Whatever gives decent results and the lowest number of checks is the best.

One tip to help with performance, btw... Use alarms if you're not already! For example, if you have AI, does it need to calculate it's movement EVERY step? If not then move the AI calculations into an alarm and run it every 5 steps or whatever works (and don't forget to set the alarm to a random value in the create event to start with so that all the enemies don't run the alarm event at the same time... this spreads the load out more evenly). The same for the shooting. I'm assuming enemies can't shoot every step, so only do the calculations when they can shoot, and again, stagger this over multiple frames so the enemies perform the calculations in an alarm at different times to spread out the computations. :)
 

NightFrost

Member
The problem is essentially the same as light in 3D environment having to bounce several times before reaching the camera. Trying to map out it all is highly expensive so I don't think any software tries brute force it all but uses shortcuts. The problem is since you have bounces it can't be immediately said which routes lead from x1,y1 to x2,y2. You're just shooting lightrays in the dark (badabum-tshhh) until you chance on a valid route.

You'll have to limit the problem, which your game already does by allowing only one bounce. Now since we're concerned with hitting the player, limit the problem further by mapping out all the potential bounces out of the player. You also should jigger the intersect math to solve: with current player speed and enemy bullet speed, what is the distance the bullet needs to travel to intersect? Then shoot out rays from a position leading the player and follow them through at most for one bounce, end on second wall hit detection. If the ray hits an enemy, calculate distance to that enemy. If it is in the ballpark of intersect distance calculated earlier, the enemy may have an intersecting shot to player. Flag that enemy to shoot to the incoming ray's direction when its turn on the Step event comes.

And all that is for one leading position. You may want to calculate for several, either a static amount spread across a distance that depends on player speed, or for an amount varying by player speed at fixed steps.
 
Top