Moving Polygon for Hit Detection
import java.awt.*;
int[] xs = {100,200,100,0};
int[] ys = {100, 200, 300, 200};
Polygon p = new Polygon(xs, ys, 4);
int x;
void setup()
{
size(500,500);
x = width;
frameRate(120);
}
void draw()
{
background(255);
if (p.contains(x, 150, 20, 20))
fill(255,0,0);
else if (p.contains(x, 170))
fill(0, 0, 255);
else
fill(0,255,0);
beginShape();
vertex(xs[0], 100);
vertex(xs[1], 200);
vertex(xs[2], 300);
vertex(xs[3],200);
endShape(CLOSE);
fill(0,0,255,50);
if (frameCount % 4 == 0)
{
for (int i = 0; i < xs.length; i++)
xs[i]++;
if (xs[3] > width)
{
xs[0] = 100;
xs[1] = 200;
xs[2] = 100;
xs[3] = 0;
}
p = new Polygon(xs,ys,4);
}
if (x < 0)
x = width;
rect(x--, 150, 20, 20);
}