Seeker s;//http://kirkjerk.com/java/artseroids/js/ void setup() { size(500, 500); s = new Seeker(); } void draw() { s.seek(mouseX, mouseY); s.move(); s.draw(); } class Seeker { float x, y, a, s; float MAXTURN = .1; Seeker() { x = width/2; y = height/2; a = -PI/2; s = 2; } void move() { x += s * cos(a); y += s * sin(a); while (x < 0) x += width; while (x > width) x -= width; while (y < 0) y += height; while (y > height) y -= height; } void draw() { pushMatrix(); translate(x, y); rotate(a); triangle(20, 0, -10, -10, -10, +10); popMatrix(); } void seek(float rtx, float rty) { float dx = (rtx - x); float dy = ( rty - y); //this trick seems to work to deal with the overlap.. //they consider the shortest path even if other side of screen if (abs(dx) > width/2) { dx *= -1.0; } if (abs(dy) > height/2) { dy *= -1.0; } float wantangle = atan2(dy, dx); float anglediff = (a - wantangle); anglediff /= PI; //this next bit catches the "runaround" if (anglediff > 1) { anglediff -= 2; } if (anglediff < -1) { anglediff += 2; } if (abs(anglediff) > .1) { if (anglediff > 0) { a -= smaller(abs(anglediff*PI),MAXTURN); } else { a += smaller(abs(anglediff*PI),MAXTURN);; } //could just be MAXTURN if you don't mind oversteer } } } float smaller(float a, float b){ if(a < b) return a; return b; }