# Collision detection any number of sides

**URL:** <https://discourse.processing.org/t/collision-detection-any-number-of-sides/40497>\
**Category:** Gallery\
**Created:** [January 7, 2023, 2:37pm UTC](https://discourse.processing.org/t/collision-detection-any-number-of-sides/40497 "2023-01-07T14:37:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [January 7, 2023, 2:37pm UTC](https://discourse.processing.org/t/collision-detection-any-number-of-sides/40497/1 "2023-01-07T14:37:23Z")

</div>

https://openprocessing.org/sketch/1786718/embed/

```auto

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

> **[points on grid with collision detection - paul goux - OpenProcessing](https://openprocessing.org/sketch/1786696)**
>
> uses winding number to identify collision

Fixed for touch devices

https://openprocessing.org/sketch/1786696/embed/
