# Making the Snake Wiggle

**URL:** <https://discourse.processing.org/t/making-the-snake-wiggle/2094>\
**Category:** p5.js\
**Created:** [July 26, 2018, 3:54am UTC](https://discourse.processing.org/t/making-the-snake-wiggle/2094 "2018-07-26T03:54:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BlitzBane](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@BlitzBane](https://discourse.processing.org/u/BlitzBane)\
**Post date:** [July 26, 2018, 3:54am UTC](https://discourse.processing.org/t/making-the-snake-wiggle/2094/1 "2018-07-26T03:54:59Z")

</div>

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/af268d0801182fb387f243b59592afe91d3f80a8.png)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/a91915d9afbe764d8fe5a3ad447d42d3e02248f1.png)

Is there any way to wiggle the snake while going to the food(the white dot)? Here is my code as well. Please help me out.

Thank you 😃

---

<div class="post-metadata">

**Author:** ![BlitzBane](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@BlitzBane](https://discourse.processing.org/u/BlitzBane)\
**Post date:** [July 26, 2018, 4:12am UTC](https://discourse.processing.org/t/making-the-snake-wiggle/2094/3 "2018-07-26T04:12:26Z")

</div>

Lol. Hahaha… My codes are actually long. Im just asking the algorithn or a logic on this scenario.

---

<div class="post-metadata">

**Author:** ![BlitzBane](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@BlitzBane](https://discourse.processing.org/u/BlitzBane)\
**Post date:** [July 26, 2018, 4:16am UTC](https://discourse.processing.org/t/making-the-snake-wiggle/2094/4 "2018-07-26T04:16:18Z")

</div>

function Worm() {  
this.acceleration = createVector(0, 0);  
this.velocity = createVector();  
this.x = random(width);  
this.y = random(height)  
this.pos = createVector(this.x, this.y);  
this.r = 10;  
this.maxspeed = 5;  
this.maxforce = 0.1;  
this.angle = atan2(this.pos.x, this.pos.y);  
this.life = random(100);

this.update = function() {  
this.velocity.add(this.acceleration);  
this.velocity.limit(this.maxspeed);  
this.pos.add(this.velocity);  
[//this.acceleration.mult](https://this.acceleration.mult)(0);  
}

this.applyForce = function(force) { this.acceleration.add(force); }

this.seek = function(target) {  
var desired = p5.Vector.sub(target, this.pos);  
var d = desired.mag();  
var speed = this.maxspeed;  
desired.setMag(speed);  
var steer = p5.Vector.sub(desired, this.velocity);  
steer.limit(this.maxforce);  
this.applyForce(steer);

}

}

Here is my complete code for the Worm. 😃  
the target serves as the position of the Food.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [July 26, 2018, 4:34am UTC](https://discourse.processing.org/t/making-the-snake-wiggle/2094/5 "2018-07-26T04:34:02Z")

</div>

Wiggle like this? [https://processing.org/examples/sinewave.html](https://processing.org/examples/sinewave.html)

If you apply this to your line, then you can reposition your actual snake using transformation operations: translation, rotation.

Kf
