# GM:S 1.4 Need a help about making prediction shots

Discussion in 'Programming' started by hijong park, Mar 14, 2019.

1. ### hijong parkMember

Joined:
Dec 29, 2016
Posts:
131
I'm trying to implement prediction shot for the enemies to make it more challenging. I did some experiments with point_distance and lengthdir_x / lengthdir_y to make it, but It never worked well. So I searched for prediction shot examples and found this one:

Code:
```predir = point_direction(x,y,player.x,player.y)

alpha = object0.speed / shootspeed;
beta = alpha * sin(phi);
if (abs(beta) >= 1) {
return (-1);
}

This one works precisely, which is good. But the problem is that this method only gets the direction of the bullet to move for prediction shots, not the x and y coordinates the bullet would move to. I need to get the positions of the predict shooting bullets, so enemies won't end up firing their bullet at the wall when there's a wall in the predicted position.

Are there any possible ways to make the prediction shooting by getting x and y positions of it?

2. ### TheSlyMember

Joined:
Jan 16, 2017
Posts:
1,001
Once you have the direction, can't you use lengthdir_x and lengthdir_y to get the x and y, with len being the distance from the bullet to the coordinates of the player + his hsp/vsp.

3. ### JackTurboMember

Joined:
Oct 19, 2016
Posts:
827
Could you not just use a collision line check before firing?

4. ### hijong parkMember

Joined:
Dec 29, 2016
Posts:
131
but I need x and y value of the predicted shot's position to use collision line. You might know that just checking collision line between enemy.x,enemy.y and player.x, player.y would not work well for prediction shooting, as players would exploit it by moving around in the narrow corridors to make the enemy shooting at the walls.

Last edited: Mar 14, 2019
5. ### NightFrostMember

Joined:
Jun 24, 2016
Posts:
2,012
I recall you'll have to look at line intersection maths for that.

6. ### flyingsaucerinvasionMember

Joined:
Jun 20, 2016
Posts:
2,223
Using the law of sines:

distance_to_intercept = point_distance(x, y, target.x ,target.y) * dsin(phi) / dsin(phi - darcsin(beta));
intercept_x = x + dcos(shoot_dir) * distance_to_intercept;
intercept_y = y - dsin(shoot_dir) * distance_to_intercept;

shoot_dir is the direction in which you shoot the bullet.

7. ### curatoMember

Joined:
Jun 30, 2016
Posts:
424
Yeah it would be about the same idea and checking for the enemy x and y for collisions you just use the predicted x and y and see if the bullet can make to where you predict the player will be and if it is clear you can try to lead the shot to hit him.