# Oscillation

This week I designed a creature with oscillating legs and body, using the angular motion example as a template. The way sea creatures move under the water has always amazed me so I tried to emulate that motion using trigonometry. Some other exercises where I tried to create underwater behavior can be seen as a swarm of fish in Forces + Attractors (NoC) and an octopus in Arrays (ICM).
Processing code: Monster monster; float timer; void setup() { size(1000,500); monster = new Monster(100, 3); } void draw() { background(50,0,50); timer = (float) millis() / 1000; monster.update(); monster.checkEdges(); monster.display(); } class Monster { PVector location; PVector velocity; PVector acceleration; float topspeed; float size; Monster(float _size, float _topspeed) { location = new PVector(width/2, height/2); velocity = new PVector(0, 0); size = _size; topspeed = _topspeed; } void update() { PVector mouse = new PVector(mouseX, mouseY); PVector dir = PVector.sub(mouse, location); dir.normalize(); dir.mult(0.05); acceleration = dir; velocity.add(acceleration); velocity.limit(topspeed); location.add(velocity); } void display() { float theta = velocity.heading(); pushMatrix(); translate(location.x, location.y); rotate(theta); // Legs stroke(255, 50, 0, 200); strokeWeight(15); displayLeg(size * 0.75, 0.75, 0.3); displayLeg(size * 0.75, 0.25, 0.1); displayLeg(size * 0.75, -0.25, -0.1); displayLeg(size * 0.75, -0.75, -0.3); // Body noStroke(); fill(255, 50, 0, 200); ellipse(0, 0, size + 5 * sin(5 * timer), size + 10 * cos(5 * timer)); ellipse(0, 0, 0.7 * size + 10 * sin(5 * timer), 0.7 * size + 5 * cos(5 * timer)); popMatrix(); } void displayLeg(float length, float angle, float delay) { float aSp = 0.5 * sin(3 * (timer - delay)); float leftAngle = angle + aSp; float rightAngle = PI - angle - aSp; line(0, 0, length * sin(leftAngle), length * cos(leftAngle)); line(0, 0, length * sin(rightAngle), length * cos(rightAngle)); } void checkEdges() { if ((location.x > width + size) || (location.x < -size)) { velocity.x = velocity.x * -1; } if ((location.y > height + size) || (location.y < -size)) { velocity.y = velocity.y * -1; } } } |