Я пытаюсь сделать игру похожую на slither.io. Я только на первых шагах пытаюсь заставить мою змею (которая построена из числа секций (кругов)) двигаться в направлении мыши. Мне удалось переместить секцию головы змеи в направлении мыши, и я хочу, чтобы остальные секции двигались по траектории головы, как в оригинальной игре, но расстояние между каждой секцией будет таким, как я хочу ( "sectionsdistance"). Любые идеи, как я могу сделать это? https://jsfiddle.net/nzemc3ws/2/
function Snake(x, y, r) {
this.headpos = createVector(x, y);
this.r = r;
this.body = [];
this.additionalsections = 0;
this.sectionsdistance = 2 * r * 0.5;
for (var i = 1; i < 10; i++) {
this.body.push([this.headpos.x, this.headpos.y + i * this.sectionsdistance]);
}
this.update = function() {
var prevheadpos = this.headpos.copy();
var velocity = createVector(mouseX - width / 2, mouseY - height / 2);
velocity.setMag(3);
this.headpos.add(velocity);
for (var i = this.body.length - 1; i >= 0; i--) {
if (i == 0) { // Second Section
this.body[i][0] = prevheadpos.x;
this.body[i][1] = prevheadpos.y;
}
else {
this.body[i][0] = this.body[i-1][0];
this.body[i][1] = this.body[i-1][1];
}
}
}
this.show = function() {
fill(212, 239, 255);
stroke(186, 229, 225);
strokeWeight(4);
for (var i = this.body.length - 1; i >= 0; i--) {
ellipse(this.body[i][0], this.body[i][1], this.r * 2, this.r * 2);
}
ellipse(this.headpos.x, this.headpos.y, this.r * 2, this.r * 2);
}
}