GameMaker Anticipate an instance's future coordonates

F

Flagolet

Guest
Hi there. Newby in coding here :cool:

I would like to know some ways to code an enemy shot so the enemy bullet acts this way : My player is in a linear motion, and when the enemy shoots, the bullets of the enemy will have a certain direction and a certain speed so the bullets will colide with the player if the player keeps going in the same direction at the same speed.

Sorry if it's confusing, I don't speak a pretty good english.

For those who played "Enter The Gungeon", I'm talking about how to mimic the bullet-veterans shots. (these shots aims the future location of the player)

Thanks for those who will take the time to respond, and cheers ;-)
 
The function you are looking for is
lerp(), which stands for linear extrapolation. You may need something NOT linear if you ever deal with exponential values, tho. Those are sometimes called TWEEN functions, but unless the update brought some tweening functions Im not aware of, you'll have to find them online and make your functions. I think there's something on the marketplace, too, might be free even, I havent checked it in a while.
 

kburkhart84

Firehammer Games
The function you are looking for is
lerp(), which stands for linear extrapolation. You may need something NOT linear if you ever deal with exponential values, tho. Those are sometimes called TWEEN functions, but unless the update brought some tweening functions Im not aware of, you'll have to find them online and make your functions. I think there's something on the marketplace, too, might be free even, I havent checked it in a while.
I have a basic Easing thing I released for free on Itch, has 30 equations and you can use 2.3s animation curves as a base as well. However, a simple LERP isn't what is needed in this case, because it isn't just moving to the position, rather predicting where the object would be based on its current speed and direction, and then moving to intercept its course based on the time it takes and the speed you go.
 
I have a basic Easing thing I released for free on Itch, has 30 equations and you can use 2.3s animation curves as a base as well. However, a simple LERP isn't what is needed in this case, because it isn't just moving to the position, rather predicting where the object would be based on its current speed and direction, and then moving to intercept its course based on the time it takes and the speed you go.
Well, yes, you can definetely use lerp for that, it's just less common than for, say, acceleration, but nonetheless.

Extrapolation is just returning the y value on a specified x on a graph. You can calculate future positions with it no problem, I guarantee you.
you have x, y, futurX, futurY and bullet speed, you can calculate the bullet angle needed with those.

Requires some maths, but it's part of the game!
 

kburkhart84

Firehammer Games
Well, yes, you can definetely use lerp for that, it's just less common than for, say, acceleration, but nonetheless.

Extrapolation is just returning the y value on a specified x on a graph. You can calculate future positions with it no problem, I guarantee you.
you have x, y, futurX, futurY and bullet speed, you can calculate the bullet angle needed with those.

Requires some maths, but it's part of the game!
I'm not disagreeing with that, I'm just saying it is more complicated than a simple LERP(). The way you worded it made it seem like lerp() was all you needed, but as you yourself just said, that's not all, though it is part of the equation.
 
I'm not disagreeing with that, I'm just saying it is more complicated than a simple LERP(). The way you worded it made it seem like lerp() was all you needed, but as you yourself just said, that's not all, though it is part of the equation.
Yeah, maybe I just gave the big picture without much details and some embelishment, but I still think that's going to solve itself when the OP gets that function under his fingers.
But yeah, it's going to be a quite cool and challenging function to build for a beginner! Lots of useful functions like lengthdir and lerp are going to be needed and they seem to be hard to grasp for many!
 

kburkhart84

Firehammer Games
Yeah, maybe I just gave the big picture without much details and some embelishment, but I still think that's going to solve itself when the OP gets that function under his fingers.
But yeah, it's going to be a quite cool and challenging function to build for a beginner! Lots of useful functions like lengthdir and lerp are going to be needed and they seem to be hard to grasp for many!
Or they could just use the function I linked to earlier. That function uses point_direction and a bit of trig to get it done easily enough. It depends on if their priority is to get the thing done or to really learn lower level things like this.
 
It depends on if their priority is to get the thing done or to really learn lower level things like this.
Call me overly optimistic, but I always seem to assume the latter, hahaha

And why didn't i learn about this site until today?!? Looks like a lot of useful stuff in there!
 

kburkhart84

Firehammer Games
Call me overly optimistic, but I always seem to assume the latter, hahaha

And why didn't i learn about this site until today?!? Looks like a lot of useful stuff in there!
Yeah, that site is really old actually, so since it is new it no longer gets that awareness boost from the new-ness.
 
F

Flagolet

Guest
Hi gentlemen, and thanks for your advices.

In facts, I'm quite interested in both of theses solutions.
It depends on if their priority is to get the thing done or to really learn lower level things like this.
I'm building a very simple breakthrough and I add some features, step by step. So I like to learn how to use simple basic functions, like lerp and lengthdir. But it's also cool to have a look on kburkhart84 script, to figure out how it's coded, and to get it quickly done if needed.

I tried this script but since I'm really a noob with coding, I could not make it work. :bash:
Still, I keep it in my pocket and I find it funny to play with maths to find out the logic of this script.
 
Hi gentlemen, and thanks for your advices.

In facts, I'm quite interested in both of theses solutions.


I'm building a very simple breakthrough and I add some features, step by step. So I like to learn how to use simple basic functions, like lerp and lengthdir. But it's also cool to have a look on kburkhart84 script, to figure out how it's coded, and to get it quickly done if needed.

I tried this script but since I'm really a noob with coding, I could not make it work. :bash:
Still, I keep it in my pocket and I find it funny to play with maths to find out the logic of this script.
That's a good attitude! Both are super important: getting the basic stuff under your fingers, AND picking people's brain as on how they use it.
As for @kburkhart84 script, I just tried it and it looks like it works just fine (I rewrote it for 2.3, tho, but it's supposed to be the exact same outcome).
Are you working your angles in Deg or Rad? The function clearly returns in degrees, so you'll either have to change the last radtodeg() line in the script, or reconvert your returned value with degtorad()
 
F

Flagolet

Guest
That's a good attitude! Both are super important: getting the basic stuff under your fingers, AND picking people's brain as on how they use it.
As for @kburkhart84 script, I just tried it and it looks like it works just fine (I rewrote it for 2.3, tho, but it's supposed to be the exact same outcome).
Are you working your angles in Deg or Rad? The function clearly returns in degrees, so you'll either have to change the last radtodeg() line in the script, or reconvert your returned value with degtorad()
Thanks, it's encouraging 😄

Well, I have'nt being working with angles for a while (altough I'm more familiar with degrees), so I jumped into my old trigonometry lessons to figure out how this function worked and I think I somehow understood the process.

Finally I managed to make it work as a line of code directly in my Objet's create event.

var dir,alpha,phi,beta;
dir = point_direction(x,y,O_Balle.x,O_Balle.y);
alpha = O_Balle.speed/speed;
phi = degtorad(O_Balle.direction - dir);
beta = alpha * sin(phi);
if (abs(beta) >= 1) {
return (-1);
}
dir += radtodeg(arcsin(beta));
direction = dir;

I could not code it as a script. I never did any so I'm gonna start slowly with easy scripts to get familiar with them and try again later. 🙃

Thanks again for your help. It feels good to get support when you jump into something new with zero experience 🙂
 

Yal

🐧 *penguin noises*
GMC Elder
Hi there. Newby in coding here :cool:

I would like to know some ways to code an enemy shot so the enemy bullet acts this way : My player is in a linear motion, and when the enemy shoots, the bullets of the enemy will have a certain direction and a certain speed so the bullets will colide with the player if the player keeps going in the same direction at the same speed.

Sorry if it's confusing, I don't speak a pretty good english.

For those who played "Enter The Gungeon", I'm talking about how to mimic the bullet-veterans shots. (these shots aims the future location of the player)

Thanks for those who will take the time to respond, and cheers ;-)
A super easy way to do this is to just assume the target moves at a constant speed:
GML:
var future_x = x + hspeed * howManyStepsToLookAhead;
var future_y = y + vspeed * howManyStepsToLookAhead;
Especially when the target is a player, this is way more accurate than it has any rights to be.

You can get the howManyStepsToLookAhead number by just dividing the distance to the target (where they are now) with the bullet's speed, too:
GML:
var howManyStepsToLookAhead = point_distance(x,y,target.x,target.y) / bullet_speed;
(or you can use a static value like 30, depending on how smart you want the AI to be)

Doesn't need to be any more complicated than this... it works surprisingly well since players usually move in the shortest possible way to their goal, and enemies that are TOO good at killing you aren't fun to deal with anyway.
 
Last edited:
F

Flagolet

Guest
A super easy way to do this is to just assume the target moves at a constant speed:
GML:
var future_x = x + hspeed * howManyStepsToLookAhead;
var future_y = y + vspeed * howManyStepsToLookAhead;
Especially when the target is a player, this is way more accurate than it has any rights to be.

You can get the howManyStepsToLookAhead number by just dividing the distance to the target (where they are now) with the bullet's speed, too:
GML:
var howManyStepsToLookAhead = point_distance(x,y,target.x,target.y) / bullet_speed;
(or you can use a static value like 30, depending on how smart you want the AI to be)

Doesn't need to be any more complicated than this... it works surprisingly well since players usually move in the shortest possible way to their goal, and enemies that are TOO good at killing you aren't fun to deal with anyway.

Thanks Yal for this function, I just tested it and it works almost every time. (I guess it sometimes miss the target for a few pixels.)

I'll keep it in my bag for when I want to have different IA :cool:
 

TheouAegis

Member
Does it miss out of the blue, or or only when the player recently changed directions? I would guess it's just missing when the player changes directions, which is logical.

You could try to put in some very rudimentary behavioral analytics. Keep an array of the player's speeds every step or couple of steps. Count how many times the sign(floor(hspd)) and sign(floor(vspd)) change. Reduce the number of steps to look ahead accordingly. Still far from perfect, but it's probably similar to how a lot of gunners would think.
 
F

Flagolet

Guest
Does it miss out of the blue, or or only when the player recently changed directions? I would guess it's just missing when the player changes directions, which is logical.

You could try to put in some very rudimentary behavioral analytics. Keep an array of the player's speeds every step or couple of steps. Count how many times the sign(floor(hspd)) and sign(floor(vspd)) change. Reduce the number of steps to look ahead accordingly. Still far from perfect, but it's probably similar to how a lot of gunners would think.
I think it missed out of the blue but maybe something occured. Something that I did'nt noticed 🧐
I'm gonna have a look at arrays pretty soon I guess. For now I'm still trying to get used to easier things 😄
 
Top