Making the Snake Wiggle

image

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 :smiley:

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

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(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. :smiley:
the target serves as the position of the Food.

Wiggle like this? 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

1 Like