class InCircle extends UnaryForce{ Vector3D position; float diameter; float SQRT_2 = sqrt(2); InCircle(ParticleSystem s, Vector3D p, float d){ super(s,1); position = p; diameter = d; } void apply(){ float maxDist = (diameter - a.size())*0.5; float bounded = diameter/(2*SQRT_2); float maxDistSq = bounded - a.size()*0.5; //test if bounding box of point is outside of bounded box of circle if( Math.abs( a.position().x() - position.x() ) < maxDistSq && Math.abs( a.position().y() - position.y() ) < maxDistSq ) return; Vector3D a2b = a.position().copy().subtract(position); float a2bDistance = a2b.length(); if( a2bDistance < maxDist ) return; if( a2bDistance == 0 ){ a2b.set( (new Vector3D((float)Math.random(), (float)Math.random(), 0)).unit() ); }else{ a2b.divide(a2bDistance); } float force = (maxDist - a2bDistance); a2b.multiply(force); // add on the forces addForce( a2b ); } }