class Obstacle { Vec P1, P2; double x1, y1, x2, y2; double x2_x1, y2_y1; double dir; public double WALL_DRAG = 0.6; public boolean ACTIVE = true; Obstacle(Obstacle o) { P1 = new Vec(o.P1); P2 = new Vec(o.P2); x1 = P1.X; y1 = P1.Y; x2 = P2.X; y2 = P2.Y; x2_x1=o.x2_x1; y2_y1=o.y2_y1; dir=o.dir; WALL_DRAG=o.WALL_DRAG; ACTIVE=o.ACTIVE; } Obstacle(double _x1, double _y1, double _x2, double _y2) { P1 = new Vec(_x1,_y1); P2 = new Vec(_x2,_y2); x1 = P1.X; y1 = P1.Y; x2 = P2.X; y2 = P2.Y; x2_x1 = x2-x1; y2_y1 = y2-y1; dir = Math.atan2(y2_y1,x2_x1); } // http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/ public Vec getIntersectionPoint(Vec P3, Vec P4) { double x3 = P3.X, y3 = P3.Y; double x4 = P4.X, y4 = P4.Y; double denom = (y4-y3)*(x2_x1) - (x4-x3)*(y2_y1); if (denom==0) return null; double numA = (x4-x3)*(y1-y3) - (y4-y3)*(x1-x3); double numB = (x2_x1)*(y1-y3) - (y2_y1)*(x1-x3); double uA = numA/denom; double uB = numB/denom; if (uA>=0&&uA<=1&&uB>=0&&uB<=1) { // INTERSECTION! return new Vec(x1+uA*(x2_x1),y1+uA*(y2_y1)); } return null; } public void draw() { if (ACTIVE) line(P1,P2); } }