Help with code that uses vehicle associations and seek/flock functions

Hello! I’m new to this forum. I’m taking a class that looks autonomous agents like vehicles and their behaviors. And, I’m stuck with my final project :frowning:

Description of Project:

So for my final project, I want to make a code that uses association. The premise of my project is that I have a flock of vehicles and with every mouse click a new vehicle is added. This new vehicle can either be normal or sad, and depending on that the flock will seek the vehicle.
So, in my code, the flock of vehicle’s seek the sad vehicles.
The association in this code is mostly related to the story that I have created for it. Where dorsal orcas, are mostly considered sad, so the flock of orcas would associate sadness to dorsal orcas and approach them.

Problem with code:
The problem that I am facing in my code now, is that the vehicles are really jittery when they meet a vehicle they need to approach and seek.
And, I’m suspecting that this has to do with the multiple seek functions acting on the vehicles. But, I’m not sure really.

So, I would very much appreciate if someone could look at my code and help me fix this :slight_smile:

My code is on Github in this link:

In my README I have a journal of my project that describes the concept and everything that I have did up till now. So, if my description above makes no sense, I hope that my README will be helpful :slight_smile:

Thank you so much :slight_smile: <3

1 Like

Hi! Welcome to the forum!

I took a quick look but since it’s quite a big project I couldn’t spot the problem. Generally, 1) it’s good to iterate step by step and 2) normalize can add artifacts.

For 1, I would suggest like

  for (Orca o : orcas) {
  }  for (Orca o : orcas) {
  }  for (Orca o : orcas) {
  }  for (Orca o : orcas) {
  }  for (Orca o : orcas) {
  }  for (Orca o : orcas) {

because when the second orca’s position change, then that will affect the other ones too, including the first one. Ideally, not like above, but you should keep the “future” position separately from location, and only update to the future position in display. Therefore other orcas’ positions are not affected… but this seemed not to affect the result and maybe is a small detail.

2 is more critical. You seem to use normalize to limit the vector. However, the problem is that this could introduce artifacts. Imagine the vector is [0.00001, 0.0]. This is almost zero, but if you normalize it, it becomes [1.0, 0.0] which is not negligible. You may want to normalize only when the vector’s magnitude is more than 1, or use other functions like limit.

Also I found in some lines you replace sum with seek() result, but don’t you want to add and divide by the count instead?

I couldn’t completely get rid of the jitter but I hope this helps!