Homing in on a player in a way that looks nice?


(TaylorAnderson) #1

Hello!

So right now I have an entity that I want to follow another entity until it collides with it. Right now I’m just using moveTowards, which works, but it makes a straight line where I kinda want more of an arc or something more smooth. Is there an easy way to accomplish this? I know my question isn’t entirely clear, and thats just because I’m not really concerned with how the entity does the following, as long as it looks a bit nicer than a direct straight line.

Thanks, Taylor


(Ultima2876) #2

Maybe you could use a attraction force vector? This is just what I thought might work off the top of my head:

Basically, each frame, you get the target position (position of target entity) and take the current position (position of following entity) from it. This is your attraction vector. Normalize this vector (divide its x by its length and its y by its length) and multiply it by the amount of force; this is the key bit. If you use a constant force, it’ll basically be the same as MoveTowards, but you can vary the force. For example, your force could be inversely proportional to the distance between the two entities; this will make your following entity slow down as it approaches the player. Or you could simply accelerate your force (start at 1, increase it slowly till it hits some kind of maximum). You could tween your force between some values for some crazy effects :smile:

Also, make sure you ‘massage’ the force value; you want to make sure it doesn’t go beyond a certain maximum and minimum, and you could also pass it through a FlashPunk Ease function. There’s lots of ways you can play with it for different effects!

If you’re feeling lazy you could check this out, it has some movement behaviours included that might be the kind of thing you want: ImmaBoid - AI Steering Behaviors


(TaylorAnderson) #3

I tried out the handmade moveTowards thing, and it kind of messes up when it reaches its destination. I took a look at the movement behaviors and I really like how the followers kinda do a sorta slow turn around to readjust to their targets’ position. I think I might have a peek in the source code to see how that works.


(TaylorAnderson) #4

Is there a way to kinda do a little sine wave motion while moving towards a target? (I tried to copy the movement behavior code and it didn’t work for me)

EDIT: realized that sounds like I want an easing function. I mean like, have the particle kinda do waves up and down while moving towards target at a steady rate


(Jacob Albano) #5

(TaylorAnderson) #6

Oh geez, this is more complicated than I thought…I guess this topic is closed now, I’ll see if I can implement that solution


(Jacob Albano) #7

We can leave the topic open for now, in casy you want to try another approach.


(Zachary Lewis) #8

Can you draw a picture of what you want the behavior to look like?


(TaylorAnderson) #9

The post that @jacobalbano put here is basically what i wanted, but i’ve since removed that element from the game entirely for different reasons so i’m not worrying about it