// BE02 Vec2D bounce, v2, v1, D_v2, D_v1, D_v, dir, obst, D_obst; float direction,speed, rad=20, dist; boolean check=true, mouse = false; void setup() { size(400,400); background(30); bounce = new Vec2D(random(width),random(height)); v1 = new Vec2D(random(width),random(height)); v2 = new Vec2D(random(width),random(height)); obst= new Vec2D(width/2, height/2); D_obst = new Vec2D(width/3, 0); D_v1 = new Vec2D(); D_v2 = new Vec2D(); D_v = new Vec2D(); dir = new Vec2D(); direction=random(PI*2); speed=5; ellipseMode(CENTER_DIAMETER); } void loop() { background(30); dir.set(speed,0); if (mouse) obst.set(mouseX, mouseY); if (bounce.x - rad/2 < 1) direction = PI-direction; if (bounce.x + rad/2 > width) direction = PI-direction; if (bounce.y - rad/2 < 1) direction = 2*PI-direction; if (bounce.y + rad/2 > height) direction = 2*PI-direction; dir.rotate(direction); bounce.add(dir); D_obst.rotate(PI/180); dist = sin(D_obst.angle())*(obst.x-bounce.x-tan(D_obst.angle()-PI/2)*(bounce.y-obst.y)); if (abs(dist) < rad/3 && obst.x-abs(D_obst.x) < bounce.x+rad/3 && obst.x+abs(D_obst.x) > bounce.x-rad/3 && obst.y-abs(D_obst.y) < bounce.y+rad/3 && obst.y+abs(D_obst.y) > bounce.y-rad && check) { direction = -direction+2*D_obst.angle()+4*PI; check = false; } else if(abs(dist) > rad/3) check = true; stroke(130); line(obst.x, obst.y, obst.x+D_obst.x, obst.y+D_obst.y); line(obst.x, obst.y, obst.x-D_obst.x, obst.y-D_obst.y); D_v1.set(bounce); D_v1.sub(v1); if(D_v1.length()>80) v1.add(D_v1.x/20, D_v1.y/20); else v1.add(D_v1.x/40,D_v1.y/40); D_v2.set(bounce); D_v2.sub(v2); if(D_v2.length()>80) v2.add(D_v2.x/20, D_v2.y/20); else v2.add(D_v2.x/40,D_v2.y/40); D_v.set(v1); D_v.sub(v2); if(D_v.length()<30) v2.sub(D_v.x/18, D_v.y/18); else v2.sub(D_v.x/20,D_v.y/20); noStroke(); fill(90); ellipse(bounce.x, bounce.y, rad, rad); fill(150,100,0); ellipse(v1.x, v1.y, 10,10); ellipse(v2.x, v2.y, 10,10); } void mousePressed() { mouse = true; } void mouseReleased() { mouse = false; } // General vector class for 2D vectors class Vec2D { float x,y; // Constructor with no parameters Vec2D() { x=0; y=0; } Vec2D(float _x,float _y) { x=_x; y=_y; } Vec2D(Vec2D v) { x=v.x; y=v.y; } void set(float _x,float _y) { x=_x; y=_y; } void set(Vec2D v) { x=v.x; y=v.y; } void add(float _x,float _y) { x+=_x; y+=_y; } void add(Vec2D v) { x+=v.x; y+=v.y; } void sub(float _x,float _y) { x-=_x; y-=_y; } void sub(Vec2D v) { x-=v.x; y-=v.y; } void mult(float m) { x*=m; y*=m; } void div(float m) { x/=m; y/=m; } float length() { return sqrt(x*x+y*y); } float angle() { return atan2(y,x); } void normalise() { float l=length(); if(l!=0) { x/=l; y/=l; } } Vec2D tangent() { return new Vec2D(-y,x); } void rotate(float val) { // Due to float not being precise enough, double is used for the calculations double cosval=Math.cos(val); double sinval=Math.sin(val); double tmpx=x*cosval - y*sinval; double tmpy=x*sinval + y*cosval; x=(float)tmpx; y=(float)tmpy; } }