# Natural Movement with Noise

**URL:** <https://discourse.processing.org/t/natural-movement-with-noise/13684>\
**Category:** Coding Questions\
**Created:** [August 29, 2019, 1:08pm UTC](https://discourse.processing.org/t/natural-movement-with-noise/13684 "2019-08-29T13:08:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vchinner](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@vchinner](https://discourse.processing.org/u/vchinner)\
**Post date:** [August 29, 2019, 1:08pm UTC](https://discourse.processing.org/t/natural-movement-with-noise/13684/1 "2019-08-29T13:08:11Z")

</div>

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

```auto
    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)

```auto
    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

---

<div class="post-metadata">

**Author:** ![philipplehmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/philipplehmann/32/4111_2.png) [@philipplehmann](https://discourse.processing.org/u/philipplehmann)\
**Post date:** [August 29, 2019, 1:43pm UTC](https://discourse.processing.org/t/natural-movement-with-noise/13684/2 "2019-08-29T13:43:02Z")

</div>

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](https://p5js.org/reference/#/p5.Vector)

```auto
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 🙂 "
