• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [SOLVED] AI advanced aiming, no idea where to start

P

Pablo Reese

Guest
I have a top down shooter, where there is an enemy that will calculate where the player is going to be by the time his bullet reaches the player. He needs to aim in front of where the player is so that his bullets are harder to dodge. I want to use the player's move speed and direction, along with the enemy's position and bullet speed, to calculate where to aim.

The player moves at a speed of 3, and the enemy bullet moves at a speed of 5. Here is the code used in the player's movement:
Code:
//Movement
var xDirection, yDirection
yDirection = keyDown - keyUp;
xDirection = keyRight - keyLeft;

y += yDirection * moveSpeed;
x += xDirection * moveSpeed;
I need to know what calculations should be made to find the angle that the enemy aims at any given time. Thanks! I appreciate it! Let me know if any more info is needed.
 

Simon Gust

Member
When the enemy creates the bullet object you can define some information in them.

At the point the enemy shoots, the player is at his x and y. The bullet should be aimed towards a step ahead.
That means the positions x + hspd and y + vspd. Depending on the distance of the player to the enemy, the bullet may need more time than 1 frame to reach the player in which case, as long as the player moves, the bullet will still miss.

Code:
// first get the distance from the player to the enemy
var dist = point_distance(x, y, obj_player.x, obj_player.y);

// now calc how many steps in theory the bullet needs to reach the player
var steps = dist / bullet_speed;

// now get the position of the player that many steps ahead
var xspd = obj_player.xDirection * obj_player.moveSpeed;
var yspd = obj_player.yDirection * obj_player.moveSpeed;

var target_x = obj_player.x + xspd * steps;
var target_y = obj_player.y + yspd * steps;

// now shoot
var inst = instance_create(x, y, obj_bullet);
with (inst)
{
   xspd = lengthdir_x(5, point_direction(x, y, target_x, target_y));
   yspd = lengthdir_y(5, point_direction(x, y, target_x, target_y));
}
In the bullet step event
Code:
x += xspd;
y += yspd;
I haven’t tested this.
 
P

Pablo Reese

Guest
You are a legend, thank you! It worked, I was close, I just needed to stop over complicating things. Thanks! Your help is much appreciated.
 
Top