class Ball{ float x,y,vx,vy,radius,diameter; Environment env; color ballColor; //-------------- Ball(float x, float y){ this.x = x; this.y = y; diameter = 10; radius = diameter/2; env = new Environment(); } //-------------- void update(){ vx *= env.drag; vy *= env.drag; wallBounce(); x += vx; y += vy; } //-------------- void wallBounce(){ if(xwidth-radius){ vx = -vx; x = width-radius; } if(yheight-radius){ vy = -vy; y = height-radius; } } //-------------- void visualize(){ stroke(255); strokeWeight(2); fill(255,128,128); ellipse(x,y,diameter,diameter); } //-------------- void push(float dx, float dy){ vx += dx * env.pushFactor; vy += dy * env.pushFactor; } //-------------- void collisionDetect(float cx, float cy, float cradius){ float distance = dist(x,y,cx,cy); if( distance < radius+cradius ){ x = cx + (radius+cradius)*(x-cx)/distance; y = cy + (radius+cradius)*(y-cy)/distance; vx = (x-cx)/distance; vy = (y-cy)/distance; } } } //================================= class Environment{ float drag = 0.98; float elasticity = 0.01; float pushFactor = 0.05; }