SWBug from TNT, is a SketchWave (SW) class for creating animated "bug" objects that move around the canvas with dynamic behavior, within a p5js application. Their 'mode' controls the movement: mode 'r' means that they move erratically, in a 'random walk' type of behavior, while mode 'p' means they move more smoothly using Perlin noise, a softened version of randomness.
Inheritance: SWBug IS-A SWPoint through inheritance, meaning it inherits all properties and methods from the SWPoint class while adding bug-specific behavior.
- Constructor:
new SWBug(x, y, mode = 'r', speed = 1, strokeColor = undefined, options = {})
- options - Optional configuration object with the following properties:
noiseAmount (default: 0.01) - Controls the smoothness of Perlin noise movement; smaller values = smoother paths, larger values = more erratic motion
strokeWeight (default: 5) - Sets the thickness of the bug's point when drawn
trailPersistence (default: 50) - Number of previous positions to keep in the trail history (set to 0 to disable trail rendering)
shouldWrapAround (default: true) - If true, bug wraps to opposite edge when leaving grid bounds; if false, bug can move beyond grid
- Inherited from SWPoint:
x (x-coordinate), y (y-coordinate), plus all SWPoint methods:
draw() - Draws the point in screen coordinates
drawOnGrid(grid) - Draws the point using user/grid coordinates (overridden in SWBug to include trail drawing)
drawTrail() - Draws the pen trail in screen coordinates
drawTrailOnGrid(grid) - Draws the pen trail in user/grid coordinates
move(dx, dy, dz) - Moves the point by delta values
setPen(on) - Enable/disable the pen trail
setMaxTrailLength(n) - Set maximum trail length
clearTrail() - Clears the pen trail history
setStrokeColor(swColor) - Sets the stroke color
setStrokeWeight(w) - Sets the stroke weight
distanceTo(otherSWPt) - Returns Euclidean distance to another SWPoint
toString() - Returns string representation (overridden in SWBug)
- Additional Properties:
mode ('r' for random walk, 'p' for Perlin noise), speed (movement velocity), strokeColor (SWColor object), options (object with optional properties: noiseAmount, strokeWeight, trailPersistence, shouldWrapAround)
- Methods:
move() - Updates position based on speed and direction
display() - Renders the bug on the canvas
checkProximity(targetX, targetY, threshold) - Detects nearby objects
changeMode(newMode) - Switches behavior mode
adjustNoise(factor) - Modifies randomness in movement
bounceOffEdges() - Handles canvas boundary collisions
- Usage: Use SWBug for animated sprites, autonomous agents, interactive creatures, and dynamic visual effects in p5.js sketches.
For more details and examples, visit: SWBug Reference Page | Perlin Noise Tutorial.
SWBug Demo Usage: Watch the bug objects move autonomously around the canvas after you 'Start'. The demo showcases proximity detection, mode changes, and noise-adjusted movement patterns. Click on the canvas to add SWPoints: when a bug gets 'too close' to a SWPoint instance, it 'toggles' its mode. If it had been moving 'smoothly' with 'Perlin Noise,' it starts moving completely erratically (random walk). If it had been moving 'randomly' then it's movements become more quasi-predictable: Perlin based. The 'Clear Points' button removes all added points. Use Control Panel options to adjust SWBug behavior.