p5.collide2D

A 2d collision detection library for p5.js
p5.collide2D provides tools for calculating collision detection for 2D geometry with p5.js.
p5.collide2D contains some versions of, and references to, the functions in Jeffrey Thompson’s Collision Detection Book. His code is CC BY-NC-SA 4.0, so, this is too! I highly, highly, reccomend reading his book to better understand all of the details involved in collision detection. Implementing this library into your code will be much easier and more efficent after reading it!
It’s an incredible resource for this kind of work! – http://www.jeffreythompson.org/collision-detection/

Get Started!
p5.collide2D assumes the default p5.js rectMode(CORNER) and ellipseMode(CENTER).
All p5.collide2D functions return true if the specified geometry is colliding and false if they are not.
Adding p5.collide2D to your project
Download the latest release
How to add a library to your p5.js sketch
CDN Links
How to use a CDN hosted version of the p5.collide2D library
To include the library via a CDN, add the library’s CDN link using a <script> tag inside the index.html file within your project.
<script defer src="https://unpkg.com/p5"></script>
<script defer src="https://unpkg.com/p5.collide2d"></script>
Live Examples in the p5.js editor
p5.collide2D supports vector version of all functions. Use the function names below with Vector added on to the name to utilize the vector version of the function. The function’s arguments will then take in vectors instead of x/y values. Each of the examples below has a commented example to demonstrate vector usage. We will be updating the documentation and examples in the future to make this distinction more clear. This in no way affects the original functionality of the library.
const p1 = createVector(100, 100);
const mouse = createVector(mouseX, mouseY);
const hit = collidePointPointVector(p1, mouse, 10);
Documentation Table of Contents
Utility
2D Collision Detection
p5.collide2D Examples & Documentation
collideDebug()
collideDebug(debugMode, size, color)
Enables collision debug mode. Draws an ellipse at the collision point between objects on screen where applicable and calculable.
function setup() {
collideDebug(true);
}
collidePointPoint()
collidePointPoint(x, y, x2, y2, [buffer])
Point to point collision with an optional buffer zone.
Live example
var hit = false;
function draw() {
background(255);
circle(100, 100, 1);
circle(mouseX, mouseY, 1);
hit = collidePointPoint(100, 100, mouseX, mouseY);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collidePointCircle()
collidePointCircle(pointX, pointY, circleX, circleY, diameter)
Point to circle collision in 2D. Assumes ellipseMode(CENTER);
Live example
var hit = false;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(255);
circle(200, 200, 100);
point(mouseX, mouseY);
hit = collidePointCircle(mouseX, mouseY, 200, 200, 100);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collidePointEllipse()
collidePointEllipse(pointX, pointY, ellipseX, ellipseY, ellipseWidth, ellipseHeight )
Point to ellipse collision.
It takes the point, the centre of the ellipse, the major and the minor axes (diameters).

Live example
var hit = false;
function draw() {
background(255);
ellipse(200, 200, 50, 150);
point(mouseX, mouseY);
hit = collidePointEllipse(mouseX, mouseY, 200, 200, 50, 150);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collidePointRect()
collidePointRect(pointX, pointY, x, y, width, height)
Point to rect collision in 2D. Assumes rectMode(CORNER);
Live example
var hit = false;
function draw() {
background(255);
rect(200, 200, 100, 150);
hit = collidePointRect(mouseX, mouseY, 200, 200, 100, 150);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collidePointLine()
collidePointLine(pointX, pointY, x, y, x2, y2, [buffer])
Point to line collision in 2D. Includes and optional buffer which expands the hit zone on the line (default buffer is 0.1).
Live example
var hit = false;
function draw() {
background(255);
line(200, 300, 100, 150);
point(mouseX, mouseY);
hit = collidePointLine(mouseX, mouseY, 200, 300, 100, 150, .5);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collidePointArc()
collidePointArc(pointX, pointY, arcCenterX, arcCenterY, arcRadius, arcRotationAngle, arcAngle, [buffer])
Point to arc collision in 2D.

Live example
const ARC_RADIUS = 100;
const ARC_ANGLE = Math.PI / 3;
const ROTATION_ANGLE = -Math.PI / 4;
var hit = false;
function draw() {
background(220);
push();
translate(width / 2, height / 2);
rotate(ROTATION_ANGLE);
fill(180, 220, 210);
stroke(10);
arc(0, 0, 2 * ARC_RADIUS, 2 * ARC_RADIUS, -ARC_ANGLE / 2, ARC_ANGLE / 2, PIE);
pop();
point(mouseX, mouseY);
hit = collidePointArc(mouseX, mouseY, width / 2, height / 2, ARC_RADIUS, ROTATION_ANGLE, ARC_ANGLE);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collideCircleCircle()
collideCircleCircle(circleX, circleY, circleDiameter, circleX2, circleY2, circleDiameter2)
Circle to circle collision in 2D. Assumes ellipseMode(CENTER);
Live example
var hit = false;
function draw() {
background(255);
circle(200, 200, 100);
circle(mouseX, mouseY, 150);
hit = collideCircleCircle(mouseX, mouseY, 150, 200, 200, 100);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collideRectRect()
collideRectRect(x, y, width, height, x2, y2, width2, height2 )
Rect to rect collision in 2D. Assumes rectMode(CORNER);
Live example
var hit = false;
function draw() {
background(255);
rect(200, 200, 100, 150);
rect(mouseX, mouseY, 50, 75);
hit = collideRectRect(200, 200, 100, 150, mouseX, mouseY, 50, 75);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collideRectCircle()
collideRectCircle(x1, y1, width1, height1, cx, cy, diameter)
Rect to circle collision in 2D. Assumes rectMode(CORNER) && ellipseMode(CENTER);
Live example
var hit = false;
function draw() {
background(255);
rect(200, 200, 100, 150);
circle(mouseX, mouseY, 100);
hit = collideRectCircle(200, 200, 100, 150, mouseX, mouseY, 100);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collideLineLine()
collideLineLine(x1, y1, x2, y2, x3, y3, x4, y4, [calcIntersection])
Line to line collision in 2D. Takes an optional boolean parameter which calculates the intersection point.
If enabled it will return an object containing the x,y position of the collision intersection.
If no intersection occurs, it will return an object containing x,y values as false. Has a debug mode.
Live example
var hit = false;
function draw() {
background(255);
line(200, 300, 100, 150);
line(mouseX, mouseY, 350, 50);
hit = collideLineLine(200, 300, 100, 150, mouseX, mouseY, 350, 50);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
Live example
var hit = false;
function draw() {
background(255);
line(200, 300, 100, 150);
line(mouseX, mouseY, 350, 50);
hit = collideLineLine(200, 300, 100, 150, mouseX, mouseY, 350, 50, true);
stroke(hit ? color('red') : 0);
print('X-intersection:', hit.x);
print('Y-intersection:', hit.y);
}
collideLineCircle()
collideLineCircle(x1, y1, x2, y2, cx, cy, diameter)
Point to circle collision in 2D. Has a debug mode.
Live example
var hit = false;
function draw() {
background(255);
line(200, 300, 100, 150);
circle(mouseX, mouseY, 50);
hit = collideLineCircle(200, 300, 100, 150, mouseX, mouseY, 50);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collideLineRect()
collideLineRect(x1, y1, x2, y2, rx, ry, rw, rh, [calcIntersection])
Line to rectangle collision in 2d. Takes and optional boolean parameter which calculates the intersection points.
If enables it will return an object containing objects of the top,left,bottom,right X,Y intersection positions.
If no intersection occurs, it will return an object containing x,y values as false. Has a debug mode.
Live example
var hit = false;
function draw() {
background(255);
rect(200, 300, 100, 150);
line(mouseX, mouseY, 350, 50);
hit = collideLineRect(mouseX, mouseY, 350, 50, 200, 300, 100, 150);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
Live example
var hit = false;
function draw() {
background(255);
rect(200, 300, 100, 150);
line(mouseX, mouseY, 350, 50);
hit = collideLineRect(mouseX, mouseY, 350, 50, 200, 300, 100, 150, true);
stroke(
hit.bottom.x || hit.bottom.y ||
hit.top.x || hit.top.y ||
hit.left.x || hit.left.y ||
hit.right.x || hit.right.y ? color('red') : 0
);
print('bottomX:', hit.bottom.x);
print('bottomY:', hit.bottom.y);
print('topX:', hit.top.x);
print('topY:', hit.top.y);
print('leftX:', hit.left.x);
print('leftY:', hit.left.y);
print('rightX:', hit.right.x);
print('rightY:', hit.right.y);
}
collidePointPoly()
collidePointPoly(pointX,pointY,vectorArray)
Point to poly collision in 2D.
Takes a point x,y and an array of p5.Vector points which contain the x,y positions of the polygon.
This function works with x-sided polygons, and “collapsed” polygons where a single polygon shape overlaps itself.
Live example
var hit = false;
const poly = [];
function setup() {
createCanvas(500, 500);
poly[0] = createVector(123, 231);
poly[1] = createVector(10, 111);
poly[2] = createVector(20, 23);
poly[3] = createVector(390, 33);
}
function draw() {
background(255);
beginShape();
for (const { x, y } of poly) vertex(x, y);
endShape(CLOSE);
circle(mouseX, mouseY, 10);
hit = collidePointPoly(mouseX, mouseY, poly);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collideCirclePoly()
collideCirclePoly(x,y,diameter,vectorArray, [interiorCollision])
Circle to poly collision in 2D. Takes a circle x,y,diameter and an array of p5.Vector points
which contain the x,y positions of the polygon. This function works with x-sided polygons, and “collapsed” polygons
where a single polygon shape overlaps itself. Takes an optional 5th ‘true’ boolean parameter which enables the collision detection
if the circle is wholly inside the polygon. The interior detection is off by default
to save evaluating all of the edges of the polygon a second time. Has a debug mode.
Live example
var hit = false;
const poly = [];
function setup() {
createCanvas(500, 500);
collideDebug(true);
poly[0] = createVector(123, 231);
poly[1] = createVector(10, 111);
poly[2] = createVector(20, 23);
poly[3] = createVector(390, 33);
}
function draw() {
background(255);
beginShape();
for (const { x, y } of poly) vertex(x, y);
endShape(CLOSE);
circle(mouseX, mouseY, 45);
hit = collideCirclePoly(mouseX, mouseY, 45, poly);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collideRectPoly()
collideRectPoly(x,y,width,height,vectorArray, [interiorCollision])
Rect to poly collision in 2D. Takes a rect x,y,width,height and an array of p5.Vector points
which contain the x,y positions of the polygon. This function works with x-sided polygons,
and “collapsed” polygons where a single polygon shape overlaps itself. Takes an optional 6th ‘true’ boolean parameter
which enables the collision detection if the rect is wholly inside the polygon.
The interior detection is off by default to save evaluating all of the edges of the polygon a second time. Has a debug mode.
Live example
var hit = false;
const poly = [];
function setup() {
createCanvas(800, 600);
collideDebug(true);
poly[0] = createVector(323, 431);
poly[1] = createVector(210, 311);
poly[2] = createVector(220, 223);
poly[3] = createVector(590, 233);
}
function draw() {
background(255);
beginShape();
for (const { x, y } of poly) vertex(x, y);
endShape(CLOSE);
rect(mouseX, mouseY, 45, 100);
hit = collideRectPoly(mouseX, mouseY, 45, 100, poly);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collideLinePoly()
collideLinePoly(x1, y1, x2, y2, vertices)
Line to poly collision in 2D. Takes a line x,y,x2,y2 and an array of p5.Vector points
which contain the x,y positions of the polygon. This function works with x-sided polygons and “collapsed” polygons
where a single polygon shape overlaps itself. Has a debug mode.
Live example
var hit = false;
const poly = Array(16).fill();
function setup() {
createCanvas(500, 400);
collideDebug(true);
const angle = TAU / poly.length;
for (var i = 0; i < poly.length; ++i) {
const a = angle * i;
const x = cos(a) * 100 + 300;
const y = sin(a) * 100 + 200;
poly[i] = createVector(x, y);
}
}
function draw() {
background(255);
beginShape();
for (const { x, y } of poly) vertex(x, y);
endShape(CLOSE);
line(10, 10, mouseX, mouseY);
hit = collideLinePoly(mouseX, mouseY, 45, 100, poly);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collidePolyPoly()
collidePolyPoly(polygon1, polygon2, [interiorCollision])
Polygon to polygon collision in 2D. Takes a 2 arrays of p5.Vector points
which contain the x,y positions of the polygons. This function works with x-sided polygons,
and “collapsed” polygons where a single polygon shape overlaps itself. Takes an optional 3rd ‘true’ boolean parameter
which enables the collision detection if the polygon is wholly inside the other polygon.
The interior detection is off by default to save evaluating all of the edges of the polygon a second time. Has a debug mode.
Live example
var mouseDiff, hit = false;
const poly = Array(8).fill();
const randomPoly = [];
function setup() {
createCanvas(500, 400);
collideDebug(true);
mouseDiff = createVector();
const angle = TAU / poly.length;
for (var i = 0; i < poly.length; ++i) {
const a = angle * i;
const x = cos(a) * 100 + 300;
const y = sin(a) * 100 + 200;
poly[i] = createVector(x, y);
}
for (var a = 0; a < 360; a += random(15, 40)) {
const t = radians(a);
const x = cos(t) * random(30, 50);
const y = sin(t) * random(30, 50);
randomPoly.push(createVector(x, y));
}
}
function draw() {
background(255);
beginShape();
for (const { x, y } of poly) vertex(x, y);
endShape(CLOSE);
mouseDiff.set(mouseX, mouseY).sub(randomPoly[0]);
for (const vec of randomPoly) vec.add(mouseDiff);
beginShape();
for (const { x, y } of randomPoly) vertex(x, y);
endShape(CLOSE);
hit = collidePolyPoly(poly, randomPoly, true);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collidePointTriangle()
collidePointTriangle(px, py, x1, y1, x2, y2, x3, y3)
Point to triangle collision in 2D. You could use collidePointPoly() to do this as well, but this is more efficient.
live example
var hit = false;
function draw() {
background(255);
triangle(300, 200, 350, 300, 250, 300);
circle(mouseX, mouseY, 10);
hit = collidePointTriangle(mouseX, mouseY, 300, 200, 350, 300, 250, 300);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
collideCirclePoly()
collideCirclePoly(cx, cy, diameter, vertices, interior)
collide-2d-primitive-triangle
To collide any primitive shape into a triangle, use the corresponding primitive shape with a 3 sided-polygon as your triangle.
Note: you will have to define your triangle using p5.Vector, see example below.
Live example
var hit = false;
const triPoly = [];
function setup() {
createCanvas(500, 400);
collideDebug(true);
triPoly[0] = createVector(300, 200);
triPoly[1] = createVector(350, 300);
triPoly[2] = createVector(250, 300);
}
function draw() {
background(255);
triangle(300, 200, 350, 300, 250, 300);
circle(mouseX, mouseY, 45);
hit = collideCirclePoly(mouseX, mouseY, 45, triPoly);
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
评论