Enemy moving towards player with low friction

So, I’ve just started working on my AI in my game and I just got stuck with one simple thing, making a enemy move towards me with barely any friction.

To explain it a little bit more I want the enemy move towards the player. If the player moves away from the “attack” of the moving enemy, the enemy will slowly speed down and change direction and aim for the player again.

Kinda similar to the Demon Eye in Terraria.

I do know how to make the enemy move towards me without taking the friction into account.

Hope you understood the question and thanks for answers.

If you need further information or code, just ask.

Off the top of my head, you could have ddx and ddy values and then change the dx and dy values accordingly.

What I have right now is kinda like that I think. I have a position (x,y) and two speeds (speedX,speedY). But the problem isn’t to implement the friction, it is how to calculate the speed in x and y that the enemy is going to slow down with each frame.

That’s less a friction thing and more just acceleration. Simply give your enemy an acceleration variable (the rate it slows down/speeds up) and when your player “moves” just decrease the enemy’s velocity by that acceleration value each frame. Velocity being the distance you move the enemy each frame. So to illustrate:

a = 2
v = 10 v = 8 v = 6 v = 8 v = 10 v = 12
---------->-------->------>-------->---------->------------>

You see, for 3 frames, v is lowered by 2, and then it speeds back up. (Of course you’ll want to use much smaller numbers)

Yes, sorry, I wasn’t very clear I assumed the notation would make sense. When I say ddx I mean the rate of change of the x velocity (ie the rate of change of x displacement from the origin (0,0))

Another name for “the rate of change of velocity” is acceleration.

okay, thanks. Just one more thing. How can I calculate the acceleration in x and y if I have a set acceleration for the diagonal movement?

There is no “calculating acceleration”. It’s a constant value of your choosing. To change the acceleration individually, just use a separate variable for x acceleration and y acceleration. Then it’s as simple as

enemy.xVelocity += xAcceleration
enemy.yVelocity += yAcceleration

But how can I get the xAcceleration and yAcceleration so that the enemy will get to the player from both axis at the same time?

yes there is calculating it.

for example if you want to be on the new course in 2 seconds and you want to have a finaldx of 6, and your startdx is 2

then ddx = (6-2)/2 = 2

then its just
enemy.xVelocity += xAcceleration
enemy.yVelocity += yAcceleration

for 2 seconds.

The reason I phrased ddx how I did was so that you would realize the above on your own, if you view acceleration as the rate of change of velocity then this is very obvious.

which 2 is which? Also (6-2)*2 = 8 right?

I’m sure he meant (6-2) / 2 = 2 as he’s calculating the distance (6 - 2) and then he’s dividing by how many seconds he wants to take to travel that distance. So, it comes out to 2 pixels per second…then again, I feel I’m wrong about what he meant, because that’s velocity, not acceleration.

In that case, yes you need to calculate the acceleration, but the original question was nothing more than how to decelerate his enemy, which would simply require choosing an acceleration value that slows down the enemy to get the effect he wants.

okay, thanks for the help :smiley:

its acceleration.

(m/s-m/s)/s = m/s^2 aka acceleration

yeah, sorry it wasn’t very clear witht he multiple 2’s… But I hope it all works for you.