MagneticPole (constructor)
A class constructor runs once when you write 'new MagneticPole(x, y)'. It's the perfect place to set up random starting properties like strength and polarity so every pole behaves a little differently.
constructor(x, y) {
this.x = x;
this.y = y;
this.strength = random(5000, 15000); // Magnitude of the pole's influence
this.polarity = random([-1, 1]); // -1 for 'south' (field towards), 1 for 'north' (field away)
// Perlin noise offsets for a natural, non-repeating drift
this.noiseOffsetX = random(1000);
this.noiseOffsetY = random(1000);
this.noiseStep = 0.005; // How quickly the pole's noise offset changes
this.driftMagnitude = 1; // Maximum amount the pole drifts per frame
}
Line-by-line explanation (4 lines)
this.strength = random(5000, 15000);- Gives this pole a random 'power level' between 5000 and 15000 - stronger poles create more intense fields nearby.
this.polarity = random([-1, 1]);- Randomly picks north (1) or south (-1) - this flips whether the field points toward or away from the pole.
this.noiseOffsetX = random(1000);- Picks a random starting point in Perlin noise's infinite space so every pole drifts differently and unpredictably.
this.driftMagnitude = 1; // Maximum amount the pole drifts per frame- Limits how far the pole can move each frame, keeping its drift subtle rather than jumping around wildly.