• 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!

Accurate future location shot finder

Perg

Member
I was trying to buid something that predicts where it should shoot to hit the enemy, i have tried using a simple script that looked like this
GML:
var i = instance_nearest;
var di = point_direction(x,y,i.x,i.y);
var dist = point_distance(x,y,i.x,i.y);
time = dist/bulletspeed;
alpha = i.direction - di;
beta = darcsin(dsin(alpha));
final = di + beta;
(i'm not sure if this will work)
but it was innacure especially at long ranges and low bullet speeds
so i decided to make it myself and after some thinking i have got this beautiful paint cauculations
Hypersupamath.png
and after some more time and cauculations i present you my script :D
GML:
function scr_accurateshot(target,bulletspeed){
    var di = point_direction(x,y,target.x,target.y);
    var dist = point_distance(x,y,target.x,target.y);
    var alpha = target.direction - di;
   
    var a = sqr(target.speed) - sqr(bulletspeed);
    var b = -2*dist*target.speed*dcos(alpha);
    var c = sqr(dist);
   
    var falsedelta = sqr(b) - (4*a*c);
   
    if sign(falsedelta) != -1{
    var time = (-b-sqrt(falsedelta))/(2*a);}
   
    var delta = target.speed*time
    var beta = bulletspeed*time
    var delto = darcsin(dsin(alpha)*delta/beta);
    var final = di + delto;
    return final;
}
 
Top