To create a p5.js fish that follows the mouse pointer, we'll define the fish using a series of circles connected hierarchically. Each circle represents a part of the fish (nose, midsection, fins, tail), and they follow the parent circles with a certain elasticity parameter. We'll also ensure that the circles do not intersect by adjusting their positions.
Here's the complete code that accomplishes this:
let fish;
function setup() {
createCanvas(800, 600);
// Initial positions
let x = width / 2;
let y = height / 2;
// Create the nose circle (head of the fish)
let nose = new Circle(x, y, 10, null, 0.1);
let elasticity = 0.1;
// Create the midsection circle attached to the nose
let mid = new Circle(x, y, 20, nose, elasticity, 0, 30); // offsetDist is 30
nose.addChild(mid);
// Create fin circles attached to the midsection
let finLeft = new Circle(x, y, 5, mid, elasticity, PI / 2, 25); // Left fin
let finRight = new Circle(x, y, 5, mid, elasticity, -PI / 2, 25); // Right fin
mid.addChild(finLeft); // mid.children[0]
mid.addChild(finRight); // mid.children[1]
// Create tail narrowing circle attached to the midsection
let tailNarrow = new Circle(x, y, 10, mid, elasticity, 0, 30);
mid.addChild(tailNarrow); // mid.children[2]
// Create tail circles attached to the tail narrowing circle
let tailLeft = new Circle(x, y, 15, tailNarrow, elasticity, PI / 4, 25);
let tailRight = new Circle(x, y, 15, tailNarrow, elasticity, -PI / 4, 25);
tailNarrow.addChild(tailLeft); // tailNarrow.children[0]
tailNarrow.addChild(tailRight); // tailNarrow.children[1]
// Assign the nose as the root of the fish hierarchy
fish = nose;
}
function draw() {
background(255);
fish.update();
fish.display();
}
class Circle {
constructor(x, y, radius, parent = null, elasticity = 0.1, offsetAngle = 0, offsetDist = 0) {
this.pos = createVector(x, y);
this.radius = radius;
this.parent = parent;
this.children = [];
this.elasticity = elasticity;
this.vel = createVector(0, 0);
this.angle = 0;
// The angle and distance offset from the parent circle
this.offsetAngle = offsetAngle;
this.offsetDist = offsetDist || (parent ? parent.radius + this.radius : 0);
}
addChild(child) {
this.children.push(child);
}
update() {
if (this.parent == null) {
// If there is no parent (this is the nose), move towards the mouse
let desiredPos = createVector(mouseX, mouseY);
let force = p5.Vector.sub(desiredPos, this.pos).mult(this.elasticity);
this.vel.add(force);
} else {
// Follow the parent circle with elasticity
let targetAngle = this.parent.angle + this.offsetAngle;
let parentToChild = p5.Vector.fromAngle(targetAngle).mult(this.offsetDist);
let desiredPos = p5.Vector.add(this.parent.pos, parentToChild);
let force = p5.Vector.sub(desiredPos, this.pos).mult(this.elasticity);
this.vel.add(force);
// Prevent overlapping with the parent circle
let d = p5.Vector.dist(this.pos, this.parent.pos);
let minDist = this.radius + this.parent.radius;
if (d < minDist) {
let overlap = minDist - d;
let correction = p5.Vector.sub(this.pos, this.parent.pos).normalize().mult(overlap / 2);
this.pos.add(correction);
this.parent.pos.sub(correction);
}
}
// Update position and dampen velocity
this.pos.add(this.vel);
this.vel.mult(0.9);
// Update angle based on velocity
if (this.vel.mag() > 0.01) {
this.angle = this.vel.heading();
}
// Update the children circles
for (let child of this.children) {
child.update();
}
}
display() {
noFill();
stroke(0);
strokeWeight(2);
// Begin drawing the fish shape
beginShape();
// Get points on the left side
let noseLeft = p5.Vector.add(this.pos, p5.Vector.fromAngle(this.angle - PI / 2).mult(this.radius));
vertex(noseLeft.x, noseLeft.y);
let mid = this.children[0];
let midLeft = p5.Vector.add(mid.pos, p5.Vector.fromAngle(mid.angle - PI / 2).mult(mid.radius));
vertex(midLeft.x, midLeft.y);
let finLeft = mid.children[0];
let finTipLeft = p5.Vector.add(finLeft.pos, p5.Vector.fromAngle(finLeft.angle - PI / 2).mult(finLeft.radius));
vertex(finTipLeft.x, finTipLeft.y);
let tailNarrow = mid.children[2];
let tailNarrowLeft = p5.Vector.add(tailNarrow.pos, p5.Vector.fromAngle(tailNarrow.angle - PI / 2).mult(tailNarrow.radius));
vertex(tailNarrowLeft.x, tailNarrowLeft.y);
let tailLeft = tailNarrow.children[0];
let tailTipLeft = p5.Vector.add(tailLeft.pos, p5.Vector.fromAngle(tailLeft.angle - PI / 2).mult(tailLeft.radius));
vertex(tailTipLeft.x, tailTipLeft.y);
// Now get points on the right side (in reverse order)
let tailTipRight = p5.Vector.add(tailNarrow.children[1].pos, p5.Vector.fromAngle(tailNarrow.children[1].angle + PI / 2).mult(tailNarrow.children[1].radius));
vertex(tailTipRight.x, tailTipRight.y);
let tailNarrowRight = p5.Vector.add(tailNarrow.pos, p5.Vector.fromAngle(tailNarrow.angle + PI / 2).mult(tailNarrow.radius));
vertex(tailNarrowRight.x, tailNarrowRight.y);
let finRight = mid.children[1];
let finTipRight = p5.Vector.add(finRight.pos, p5.Vector.fromAngle(finRight.angle + PI / 2).mult(finRight.radius));
vertex(finTipRight.x, finTipRight.y);
let midRight = p5.Vector.add(mid.pos, p5.Vector.fromAngle(mid.angle + PI / 2).mult(mid.radius));
vertex(midRight.x, midRight.y);
let noseRight = p5.Vector.add(this.pos, p5.Vector.fromAngle(this.angle + PI / 2).mult(this.radius));
vertex(noseRight.x, noseRight.y);
// Close the shape to complete the fish outline
endShape(CLOSE);
// Optionally, draw the circles for debugging
// this.displayCircles();
}
// Optional method to display the circles (for debugging)
displayCircles() {
noStroke();
fill(0, 150, 255, 100);
ellipse(this.pos.x, this.pos.y, this.radius * 2);
for (let child of this.children) {
child.displayCircles();
}
}
}
Explanation:
-
Circle Class: Each Circle represents a part of the fish. The class handles the position, movement, and drawing of the circle. It also maintains a reference to its parent and any children.
-
Hierarchy Setup: In the setup() function, we create the fish's structure by instantiating circles for the nose, midsection, fins, tail narrowing, and tail. We specify the offset angles and distances to position them relative to their parent circles.
-
Movement and Elasticity: In the update() method of the Circle class, each circle moves towards its desired position relative to its parent. The movement is influenced by an elasticity parameter that controls how rigidly the circle follows its parent.
-
Collision Avoidance: To ensure that circles do not intersect, we check the distance between a circle and its parent and apply a correction if they overlap.
-
Drawing the Fish: In the display() method, we use the beginShape() and vertex() functions to draw a smooth outline around the external points of the circles, creating the appearance of a fish without visible joints.
-
Following the Mouse Pointer: The nose circle (the head of the fish) moves smoothly towards the mouse pointer, causing the rest of the fish to follow due to the hierarchical structure.
Usage:
- Include the p5.js library in your HTML file.
- Copy and paste the above code into your p5.js sketch.
- Run the sketch, and you should see a fish that smoothly follows your mouse pointer.
Note: This is a simplified implementation and can be further refined to improve the fish's appearance and movement dynamics. Collision handling between all circles and more sophisticated drawing techniques (like using Bezier curves) can enhance the smoothness and realism of the fish.