Give randomly generated objects a destination rather than a velocity?

I’m new to coding so this might sound very stupid but any help would be great.

I know how to increment x and y values to create object movement, and am in the process of learning how to do this using vectors, however I am in need of an alternative method for my latest project. I plan on randomly generating “enemies” and I want each of these to travel towards the centre of the screen, all at the same speed (arriving at different times however since I’ve set a time delay on their creation). Since they will be randomly generated each time I am struggling to work out how to give them set values for x and y speed. Is there a method in p5 in which I can give them a final destination to travel to, and the computer works out what to increment x and y by to get them there?


Could you share some code so we know how you’re going to implement this?

It’s been a few weeks since I worked directly on this so I’m slightly lost as to where I got my figures from, but this code works so that each enemy is generated at x and y (random values somewhere on / slightly off the screen) and travels towards the centre, however rather than travelling at different speeds they all arrive at the centre of the page at the exact same time.

1 Like

Hi wavyboneee,

Why the restriction on not using vectors? Some basic vector math would help you enormously with 2D movement Daniel Shiffman’s Nature of Code or Keith Peters’ Coding Math will tell you all about it.

Long story short:

  • Subtract Enemy’s position from Target Position and you have the vector of direction and magnitude (distance) to move in. From A to B is B - A. (Mnemonic: It’s that band from Sweden, ABBA!)
  • Make that vector into a unit vector. It defines your direction in which to move
  • Every draw() you add the product of x of that vector and the proposed speed to the x of the enemy
  • Every draw() you add the product of y of that vector and the proposed speed to the y of the enemy

This way you have easy control over both speed and direction.

Kind regards,


Like I said - still very new to programming in general so my knowledge is very basic. Information like this is super helpful, recommendations etc are just what I need!

I just took the nature of code out from the library so will give it a read and try and apply all of this to my work. Thanks so much for the help :slight_smile:

1 Like


Good to hear and glad to be of help :slightly_smiling_face:.

Have not looked into it very much, but Daniel Shiffman started a new series of the Nature of code. Old playlist was in Processing (the Java Version). The new Nature of Code videos are in p5js but currently only unedited live streams (and not on vector math yet).

It may be something to wrap your head around, but for 2D movement, once you go vectors, you never go back. And even without a library to do some of the math for you, a large part is still very manageable to implement yourself.

Good luck and happy coding!

1 Like