Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
  <div class="row">
  
    <canvas width=500 height=250 id='canvas'></canvas>
  
    <div id="legend">
      <p class="box">
        <span id="yellowLine"></span><span>- Nearest Point of Polygon</span> 
      </p>
      <p class="box">
        <span id="blueLine"/></span> - Nearest Point on all Polygons
      </p>
      <p class="box">
        <span id="orangeLine"/></span> - Direction to Center of Nearest Polygon
      </p>
    </div>
  
  </div>
    
</body>
              
            
!

CSS

              
                canvas {
  width: 500px;
  height: 250px;
}

#legend {
  position: relative;
  margin: auto;
  display: flex;
  flex-direction: column;
  background-color: gray;
  width: 400px;
}

.box {
  display: flex;
  flex-direction: row;
}

.row {
  display: flex;
  justify-content: center;
  align-items: flex-top;
}

#yellowLine {
  display: block;
  border-bottom: 3px solid yellow;
  width: 40px;
  margin: 0px 10px;
}

#blueLine {
  display: block;
  border-bottom: 3px solid blue;
  width: 40px;
  margin: 0px 10px;
}

#orangeLine {
  display: block;
  border-bottom: 3px solid orange;
  width: 40px;
  margin: 0px 10px;
}
              
            
!

JS

              
                /********************************************
  Utility Functions                       
********************************************/

// return the smallest vector of an array of vectors
function smallest(vectors) {
  let mag2s = vectors.map((v, i) => {
    return [v.pos.x * v.pos.x + v.pos.y * v.pos.y, i];
  }).sort((a, b) => {
    return a[0] - b[0];
  });
  return vectors[mag2s[0][1]];
}

function average(vectors) {
  let count = vectors.length;

  if (count === 0) {
    return new Vector({pos: {x: 0, y: 0}, unit: false});
  }
  let Xs = vectors.map(v => v.pos.x).reduce((pre, cur) => pre + cur),
      Ys = vectors.map(v => v.pos.y).reduce((pre, cur) => pre + cur);
  let sum = {
    x: vectors.reduce((prev, curr) => {
      return prev + curr.pos.x;
    }, 0),
    y: vectors.reduce((prev, curr) => {
      return prev + curr.pos.y;
    }, 0)
  }

  return new Vector({
    pos: {x: sum.x / count, y: sum.y / count},
    unit: false
  })
}

/********************************************
  Vector Class                            
********************************************/

class Vector {
  
  constructor({pos, unit = false}) {
    this.pos = pos;
    this.unit = unit;
  }
  
  copy() {
    return new Vector({pos: {x: this.pos.x, y: this.pos.y}});
  }
  
  getMag() {
    return Math.sqrt(this.pos.x * this.pos.x + this.pos.y * this.pos.y);
  }

  normalize() {
    let len = this.getMag();
    if (len === 0) {
      return new Vector({
        pos: { x: 0, y: 0 },
        unit: false
      })
    } else {
      return new Vector({
        pos: {
          x: this.pos.x / len,
          y: this.pos.y / len
        },
        unit: true
      })
    }
  }

  dot(vectorB) {
    return this.pos.x * vectorB.pos.x + this.pos.y * vectorB.pos.y;
  }
  
  project(vectorB) {
    let dp = this.dot(vectorB);
    if (vectorB.unit) {
      // simplification if b is unit vector
      return new Vector({pos: {x: dp * vectorB.pos.x, y: dp * vectorB.y}, unit: false});
    } else {
      return new Vector({
        pos: {
          x: (
            dp
            / (
              vectorB.pos.x 
              * vectorB.pos.x + vectorB.pos.y
              * vectorB.pos.y
            )
          ) * vectorB.pos.x,
          y: (
            dp
            / (
              vectorB.pos.x
              * vectorB.pos.x + vectorB.pos.y
              * vectorB.pos.y
            )
          ) * vectorB.pos.y
        },
        unit: false
      })
    }
  }

  leftNormal() {
    return new Vector({pos: {x: -this.pos.y, y: this.pos.x}, unit: false});
  }

  rightNormal() {
    return new Vector({pos: {x: this.pos.y, y: -this.pos.x}, unit: false});
  }

  perProduct(vectorB) {
    return this.dot(this.rightNormal(vectorB));
  }

  add(vectorB) {
    return new Vector({
      pos: {
        x: this.pos.x + vectorB.pos.x,
        y: this.pos.y + vectorB.pos.y
      }
    });
  }

  sub(vectorB) {
    return new Vector({
      pos: {
        x: this.pos.x - vectorB.pos.x,
        y: this.pos.y - vectorB.pos.y
      }
    });
  }
  
  scale(scalar) {
    return new Vector({
      pos: {
        x: this.pos.x * scalar,
        y: this.pos.y * scalar
      }
    });
  }
}

/********************************************
    Shape Classes                           
********************************************/

class Polygon {

  constructor({points}) {
    this.points = points;
    this.pos = average(this.points).pos;
    this.getNormals();
  }

  getPoints() {
  }
  
  getCenter() {
    this.pos = average(this.points);
  }
  
  getNormals() {
    this.normals = this.points.map((point, i) => {
      let p1 = this.points[i],
          p2 = i === this.points.length - 1
            ? this.points[0]
            : this.points[i + 1],
          v = new Vector({
            pos: {x: p2.pos.x - p1.pos.x, y: p2.pos.y - p1.pos.y}
          })
      return v.leftNormal().normalize();
    })
  }
}

class Circle {
  
  constructor({pos, radius}) {
    this.pos = pos;
    this.radius = radius;
  }
  
  getPoints(objB, point) {
    this.points = [
      new Vector({
        pos: {
          x: point.pos.x - this.pos.x,
          y: point.pos.y - this.pos.y
        }
      }).normalize().scale(this.radius).add(this),
      new Vector({
        pos: {
          x: objB.pos.x - this.pos.x,
          y: objB.pos.y - this.pos.y
        }
      }).normalize().scale(this.radius).add(this)
    ]
  }
  
  getNormals(objB) {
    let v = new Vector({
      pos: {
        x: objB.pos.x - this.pos.x,
        y: objB.pos.y - this.pos.y
      }
    });
    this.normals = [v.leftNormal().normalize()];
  }
  
  nearestPoint(points) {
    return points.map((point, i) => {
      return [
        new Vector({
          pos: {
            x: this.pos.x - point.pos.x,
            y: this.pos.y - point.pos.y 
          }
        }),
        point,
        i
      ]
    }).reduce((prev, curr) => {
      return (curr[0].getMag() < prev[0].getMag()
        ? curr
        : prev)
    });
  }
}

class AABB {
  
  constructor({pos, size}) {
    this.pos = pos;
    this.size = size;
    this.getPoints();
    this.getNormals();
  }

  getPoints() {
    let halfW = this.size.w / 2,
        halfH = this.size.h / 2;
    this.points = [
      new Vector({pos: {x: this.pos.x - halfW, y: this.pos.y - halfH}}),
      new Vector({pos: {x: this.pos.x + halfW, y: this.pos.y - halfH}}),
      new Vector({pos: {x: this.pos.x + halfW, y: this.pos.y + halfH}}),
      new Vector({pos: {x: this.pos.x - halfW, y: this.pos.y + halfH}})
    ]
  }
  
  getNormals() {
    this.normals = this.points.map((point, i) => {
      let p1 = this.points[i],
          p2 = i === this.points.length - 1
            ? this.points[0]
            : this.points[i + 1],
          v = new Vector({
            pos: {x: p2.pos.x - p1.pos.x, y: p2.pos.y - p1.pos.y}
          })
      return v.leftNormal().normalize()
    })
  }
}

/********************************************/
/*  Collision Test Class                    */
/********************************************/

class Collider {
  
  constructor(bounce = 0.5, friction = 0.5) {
    // 0 = no bounce, 1 = max bounce
    this.bounce = bounce
    // 1 = no friction, 0 max friction
    this.friction = friction
  }

  Result(isIntersecting, willIntersect, correction) {
    return {
        isIntersecting,
        willIntersect,
        correction
    }
  }

  // project vectors on to normal and return min/max value
  minMaxDotProd(vectors, normal) {
    let min = null,
        max = null
    // For circles
    vectors.forEach(v => {
        let dp = v.dot(normal)
        if (min === null || dp < min) { min = dp }
        if (max === null || dp > max) { max = dp }
    })
    return { min, max }
  }

  checkNormal(objA, objB, normal) {
    // project points onto normal to find bounds of shadow on axis
    let aMinMax = this.minMaxDotProd(objA.points, normal),
        bMinMax = this.minMaxDotProd(objB.points, normal)
    // check for overlap of shadows on axis
    if (aMinMax.min <= bMinMax.max && aMinMax.max >= bMinMax.min) {
    // correction vector is in direction of normal x amount overlapping
        let newV = normal.copy(),
            correction = newV.scale(bMinMax.max - aMinMax.min)
        correction.surface = newV.rightNormal()
        return this.Result(true, false, correction)
    }
    return this.Result(false, false, new Vector({pos: {x: 0, y: 0}}))
  }

  checkCollision(objA, objB) {
    let correctionVectors = [];
    // For circles
    objA.getNormals(objB)
    if (!objB.normals) { objB.getNormals(objA) }
    // project points of objA and objB onto objA's normals
    // check for overlap
    for (let i = 0; i < objA.normals.length; i += 1) {
      let result = this.checkNormal(objA, objB, objA.normals[i]);
      if (result.isIntersecting) {
        correctionVectors.push(result.correction);
      } else {
        return false
      }
    }
    // project points of objA and objB onto objB's normals
    // check for overlap
    for (let i = 0; i < objB.normals.length; i += 1) {
      let result = this.checkNormal(objA, objB, objB.normals[i]);
      if (result.isIntersecting) {
        correctionVectors.push(result.correction);
      } else {
        return false
      }
    }
    // if all overlap, return smallest correction vector
    return this.Result(true, false, smallest(correctionVectors))
  }
}

/********************************************/
/*  Render                                  */
/********************************************/

let canvas,
    ctx,
    collider = new Collider(),
    polys = [
      new Polygon({
        points: [
          new Vector({pos: {x: 50, y: 70}}),
          new Vector({pos: {x: 70, y: 20}}),
          new Vector({pos: {x: 20, y: 20}})
        ]
      }),
      new Polygon({
        points: [
          new Vector({pos: {x: 140, y: 180}}),
          new Vector({pos: {x: 210, y: 180}}),
          new Vector({pos: {x: 250, y: 70}}),
          new Vector({pos: {x: 200, y: 30}}),
          new Vector({pos: {x: 170, y: 30}}),
          new Vector({pos: {x: 120, y: 80}})
        ]
      }),
      new Polygon({
        points: [
          new Vector({pos: {x: 270, y: 220}}),
          new Vector({pos: {x: 370, y: 250}}),
          new Vector({pos: {x: 320, y: 260}}),
          new Vector({pos: {x: 250, y: 270}})
        ]
      }),
      new Polygon({
        points: [
          new Vector({pos: {x: 450, y: 70}}),
          new Vector({pos: {x: 370, y: 20}}),
          new Vector({pos: {x: 320, y: 20}}),
          new Vector({pos: {x: 270, y: 150}}),
          new Vector({pos: {x: 420, y: 200}})
        ]
      }),
      new AABB({
        pos: {x: 100, y: 200},
        size: {w: 20, h: 20}
      })
    ],
    a = new Circle({pos: {x: 20, y: 200},  radius: 10});

function moveMouse(e) {
  a.pos.x = e.clientX - 10;
  a.pos.y = e.clientY - 10;
}

function drawCircle(circle, color, border) {
  ctx.fillStyle = color;
  if (border) {
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 1;
  }
  ctx.beginPath();
  ctx.moveTo(circle.pos.x + circle.radius, circle.pos.y);
  ctx.arc(circle.pos.x, circle.pos.y, circle.radius, 0, 2 * Math.PI);
  ctx.closePath();
  ctx.fill();
  if (border) { ctx.stroke(); }
}

function drawPolygon(points, color) {
  ctx.fillStyle = color;
  ctx.lineWidth = 1;
  ctx.beginPath();
  ctx.moveTo(points[0].pos.x, points[0].pos.y);
  points.forEach(p => ctx.lineTo(p.pos.x, p.pos.y) );
  ctx.lineTo(points[0].pos.x, points[0].pos.y);
  ctx.closePath();
  ctx.fill();
}

function drawLine(circle, point, color, weight) {
  ctx.strokeStyle = color;
  ctx.lineWidth = weight
  ctx.beginPath();
  ctx.moveTo(circle.pos.x, circle.pos.y);
  ctx.lineTo(point.pos.x, point.pos.y);
  ctx.stroke();
}

function drawNormals(obj) {
  ctx.strokeStyle = "red";
  ctx.lineWidth = 3;
  ctx.beginPath();
  obj.normals.forEach((normal, i) => {
    ctx.moveTo(obj.points[i].pos.x, obj.points[i].pos.y);
    let extend = normal.copy().scale(20);
    ctx.lineTo(extend.pos.x + obj.points[i].pos.x, extend.pos.y + obj.points[i].pos.y);
  })
  ctx.stroke();
}

function render() {
  ctx.fillStyle = 'lightgray';
  ctx.fillRect(0, 0, 500, 250);

  let block = polys[polys.length - 1],
      closePoints = polys.map(b => a.nearestPoint(b.points)),
      closest = a.nearestPoint(closePoints.map(point => point[1]));

  a.getPoints(polys[closest[2]], closest[1]);
  let checkClosest = collider.checkCollision(a, polys[closest[2]]);

  polys.forEach((b, i) => {
    let polyColor = (checkClosest && (i === closest[2]))
      ? 'orange'
      : '#aaa';
    drawPolygon(b.points, polyColor);

    // collision check
    let closestPoint = a.nearestPoint(b.points),
        blockHit
    if (i < polys.length - 1) {
      blockHit = collider.checkCollision(block, b);
    }

    drawLine(a, closestPoint[1], 'blue', 1);

    if (blockHit) {
      block.pos.x += blockHit.correction.pos.x;
      block.pos.y += blockHit.correction.pos.y;
      block.getPoints();
    }
  });
  drawPolygon(block.points, 'blue');
  drawLine(a, closest[1], 'yellow', 3);
  drawLine(a, polys[closest[2]], 'orange', 3);
  drawCircle(a, 'red');
  
  let displace = collider.checkCollision(a, block);

  if (displace) {
    block.pos.x -= displace.correction.pos.x;
    block.pos.y -= displace.correction.pos.y;
    block.getPoints();
  }
}

window.onload = function() {
  canvas = document.getElementById('canvas');
  canvas.onmousemove = moveMouse;
  ctx = canvas.getContext('2d');
  
  setInterval(render, 10);
};










              
            
!
999px

Console