// a simple "springy follower" thingy class Tracker { float x, y, vx, vy; float lastx, lasty; float targetx, targety; float rate, friction; color clr; Tracker() { this(width/2, height/2); } Tracker(float _x, float _y) { setPosition(_x, _y); setTarget(_x, _y); lastx = x; lasty = y; rate = 0.01; friction = 0.01; clr = #000000; } float getAngle() { return atan2(vy,vx); } void setColor(color c) { clr = c; } void setFriction(float f) { friction = f; } // "all in one" routine to set ALL physics-related properties // (expand as necessary if properties are added) void setPhysics(float _rate, float _friction) { rate = _rate; friction = _friction; } void setPosition(float _x, float _y) { x = _x; y = _y; vx = vy = 0.0; } void setRate(float r) { rate = r; } void setTarget(float x, float y) { targetx = x; targety = y; } void setVelocity(float _vx, float _vy) { vx = _vx; vy = _vy; } void move(float dt) { lastx = x; lasty = y; float axdt = ((targetx-x) * rate) * dt; float aydt = ((targety-y) * rate) * dt; x += vx * dt + 0.5 * axdt * dt; y += vy * dt + 0.5 * aydt * dt; vx += axdt; vy += aydt; float frictiondt = friction * dt; vx -= vx * frictiondt; vy -= vy * frictiondt; } void draw() { stroke(clr); point(x, y); } }