Collision detection any number of sides


code

int y = 200,x= 200,tileHeight = 50,tileWidth = 50,sides = 10;
float theta = 360;

void setup(){
 size(400,400); 
 theta = radians(theta/sides);
};

void draw(){
  background(255);
strokeWeight(2);
stroke(0);
int c1 = 0;
for(int i=0;i<sides;i++){
         float thetab =  theta * i;
            float thetac =  theta * i+theta;
            PVector a = new PVector(x+tileWidth/2*sin(thetab),y+tileHeight/2*cos(thetab));
            PVector b = new PVector(x+tileWidth/2*sin(thetac),y+tileHeight/2*cos(thetac));
            
            float t1 = abs(atan2(mouseY - a.y,mouseX - a.x)+PI);
            float t2 = abs(atan2(mouseY - b.y,mouseX - b.x)+PI);
            if(abs(t2-t1)>=PI){
            c1++;
          }
         }
         beginShape();
for(int i=0;i<sides;i++){
         float thetab =  theta * i;
            float thetac =  theta * i+theta;
            PVector pv1 = new PVector(x+tileWidth/2*sin(thetab),y+tileHeight/2*cos(thetab));
            PVector pv2 = new PVector(x+tileWidth/2*sin(thetac),y+tileHeight/2*cos(thetac));
            //
            if(c1==1){fill(0, 0, 255, 100);
                      noStroke();
                      vertex(pv1.x,pv1.y);
                      vertex(pv2.x,pv2.y);
            }else line(pv1.x,pv1.y,pv2.x,pv2.y);
         }
         endShape();
}

and with complex shapes
press mouse to add points

Fixed for touch devices

1 Like