Randomize location every minute

hello! i am a beginner trying to create a project where a bee moves between 3 flowers. there are 3 set flower locations, and each minute, the bee will randomly choose one of the 3 flowers to fly to. if anyone could help me figure out how to write the code so that the bee chooses one of 3 coordinate sets every minute and glides to that spot, i would really appreciate it!

Hi welcome to the forum.

We can support you complete the project so show us what you tried so far and what particualr problems you are having.
:smile:

Hey, welcome to the forum!

Please try to divide your project into steps so you can work on each step separately.

  • Draw flowers, have an array of PVector of their positions (the bee flies to)
  • Draw the bee at x,y (you must be able to change x,y)
  • Every minute choose a different target from the array using random
  • Use lerp() to change the bee position (PVector bee.x, bee.y) from previous flower position to target flower position. Actually, curvePoint() may look better but is harder to get used to.
    Pro-Tip: Start with lerp() and then make a new version with curvePoint().
  • You need a timer. Use millis().
    In theory I guess, you can derive the lerp() parameter “amt”
    (which in my code is named “i_Lerp”, in curvePoint() reference “t”) from the difference of millis()-lastTargetChange using map() command. So it is maybe
    amt=map( millis()-lastTargetChange, 0,1000, 0,1); - not tested.
    Read this as: Calc the millis since the start of flight from previous flower (this number is between 0 and 1000), calc the number that has passed from 0 to 1000 and calc the same number on the scale between 0 and 1 (for example 0.67, for the range of “amt”) and put the resulting number into amt so we can use it for lerp(). It’s easier than it looks.

See reference for the commands.

Warm regards,

Chrisir

EDITED

Here is an example for the PVector Array



// array of PVector - see reference
PVector[] flowerPositions = {
  new PVector (random (39, 377), random (39, 377)),
  new PVector (random (239, 577), random (29, 577)),
  new PVector (random (139, 377), random (339, 377))
};
int indexFlower=0;

// Needed for lerp() command - see reference
float i_Lerp=0;

//---------------------------------------------------------------------

void setup() {
  fullScreen();

  background(0);
}

void draw() {
  background(0);

  text ( flowerPositions[indexFlower].x
    +" "
    + flowerPositions[indexFlower].y
    +"\n\n\nWait for the Pos to change.",
    100, 200);

  if (i_Lerp>=1) {
    // Start moving to NEXT flower
    indexFlower=int(random(3));//choose NEXT flower
    i_Lerp=0;//reset
  }

  i_Lerp+=0.061; // SPEED
}
//

I agree.

Also, it is more sensibel to set the flower positions in setup() using width and height. The internal variables width and height (of your window) are not initialized before size() command in setup(), so they won’t work where I init the flower positions now.

Tutorial:

Examples:

Try modifying the 0.5 in the example with values between 0 and 1.0:

middle = PVector.lerp(start, end, 0.5);

Once understood you can modify to increment amt in the draw loop as desired.
Constant speed is a challenge for later but that is for homework.

PVectors make this much easier!
It can be done without PVectors as well.

:)