Natural Movement with Noise

Im creating a an object that randomly moves in a natural way using noise like this (works as intended):

    thisRabbit.x = _world.width * (noise(thisRabbit.t));
    thisRabbit.y = _world.height * (noise(thisRabbit.t+5));
    thisRabbit.t += 0.001;

The objects encounter a collision and their trajectory is manipulated, the movement path now changes to straight line (words as intended)

    let vx = this.acquiredFood[0] - this.x;
    let vy = this.acquiredFood[1] -  this.y;

    let f = (this.genes.speed + 10) / Math.sqrt(vx-vx+vy*vy);

    vx = vx * f;
    vy = vy * f;

    let newX = this.x + vx;
    let newY = this.y + vy;

The problem is after this movement , i want the object to start moving in a random direction again as it was initially. If i use the same function, the object jumps to the last location before the trajectory was modified.

So how do i get the object to move as before by giving a starting position

1 Like

Hi

You could make a position each for the noise and the straight line movement.
Add these two positions up before drawing the object.
Look into vectors: https://p5js.org/reference/#/p5.Vector

let straight = true;
let currentMovement  = createVector(0, 0);
let noisePos         = createVector(0, 0);
let directionalPos   = createVector(0, 0);
let outputPos        = createVector(0, 0);


// in draw 
if (straight) {
   // when moving straight
   currentMovement = createVector(1, 0)
   directionalPos.add(currentMovement);
} else {
   // when moving noisy
   currentMovement = createVector(noise(thisRabbit.t), noise(thisRabbit.t+5))
   noisePos.add(currentMovement);
}

// Add both positions.
outputPos = noisePos.add(directionalPos);


"Untested code :slight_smile: "

1 Like