Skip to content

Commit

Permalink
Merge pull request #4 from conradoqg/master
Browse files Browse the repository at this point in the history
makes collide2d work with instantiated sketches.
  • Loading branch information
bmoren authored Mar 26, 2017
2 parents df23629 + 1ea2d22 commit cfb0530
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
74 changes: 37 additions & 37 deletions p5.collide2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ p5.prototype.collideRectCircle = function (rx, ry, rw, rh, cx, cy, diameter) {
}else if (cy > ry+rh){ testY = ry+rh } // bottom edge

// // get distance from closest edges
var distance = dist(cx,cy,testX,testY)
var distance = this.dist(cx,cy,testX,testY)

// if the distance is less than the radius, collision!
if (distance <= diameter/2) {
Expand All @@ -51,15 +51,15 @@ p5.prototype.collideRectCircle = function (rx, ry, rw, rh, cx, cy, diameter) {

p5.prototype.collideCircleCircle = function (x, y,d, x2, y2, d2) {
//2d
if( dist(x,y,x2,y2) <= (d/2)+(d2/2) ){
if( this.dist(x,y,x2,y2) <= (d/2)+(d2/2) ){
return true;
}
return false;
};

p5.prototype.collidePointCircle = function (x, y, cx, cy, d) {
//2d
if( dist(x,y,cx,cy) <= d/2 ){
if( this.dist(x,y,cx,cy) <= d/2 ){
return true;
}
return false;
Expand All @@ -78,11 +78,11 @@ return false;

p5.prototype.collidePointLine = function(px,py,x1,y1,x2,y2, buffer){
// get distance from the point to the two ends of the line
var d1 = dist(px,py, x1,y1);
var d2 = dist(px,py, x2,y2);
var d1 = this.dist(px,py, x1,y1);
var d2 = this.dist(px,py, x2,y2);

// get the length of the line
var lineLen = dist(x1,y1, x2,y2);
var lineLen = this.dist(x1,y1, x2,y2);

// since floats are so minutely accurate, add a little buffer zone that will give collision
if (buffer === undefined){ buffer = 0.1; } // higher # = less accurate
Expand All @@ -98,36 +98,36 @@ return false;
p5.prototype.collideLineCircle = function( x1, y1, x2, y2, cx, cy, diameter) {
// is either end INSIDE the circle?
// if so, return true immediately
var inside1 = collidePointCircle(x1,y1, cx,cy,diameter);
var inside2 = collidePointCircle(x2,y2, cx,cy,diameter);
var inside1 = this.collidePointCircle(x1,y1, cx,cy,diameter);
var inside2 = this.collidePointCircle(x2,y2, cx,cy,diameter);
if (inside1 || inside2) return true;

// get length of the line
var distX = x1 - x2;
var distY = y1 - y2;
var len = sqrt( (distX*distX) + (distY*distY) );
var len = this.sqrt( (distX*distX) + (distY*distY) );

// get dot product of the line and circle
var dot = ( ((cx-x1)*(x2-x1)) + ((cy-y1)*(y2-y1)) ) / pow(len,2);
var dot = ( ((cx-x1)*(x2-x1)) + ((cy-y1)*(y2-y1)) ) / this.pow(len,2);

// find the closest point on the line
var closestX = x1 + (dot * (x2-x1));
var closestY = y1 + (dot * (y2-y1));

// is this point actually on the line segment?
// if so keep going, but if not, return false
var onSegment = collidePointLine(closestX,closestY,x1,y1,x2,y2);
var onSegment = this.collidePointLine(closestX,closestY,x1,y1,x2,y2);
if (!onSegment) return false;

// draw a debug circle at the closest point on the line
if(_collideDebug){
ellipse(closestX, closestY,10,10);
if(this._collideDebug){
this.ellipse(closestX, closestY,10,10);
}

// get distance to closest point
distX = closestX - cx;
distY = closestY - cy;
var distance = sqrt( (distX*distX) + (distY*distY) );
var distance = this.sqrt( (distX*distX) + (distY*distY) );

if (distance <= diameter/2) {
return true;
Expand All @@ -146,14 +146,14 @@ p5.prototype.collideLineLine = function(x1, y1, x2, y2, x3, y3, x4, y4,calcInter
// if uA and uB are between 0-1, lines are colliding
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {

if(_collideDebug || calcIntersection){
if(this._collideDebug || calcIntersection){
// calc the point where the lines meet
var intersectionX = x1 + (uA * (x2-x1));
var intersectionY = y1 + (uA * (y2-y1));
}

if(_collideDebug){
ellipse(intersectionX,intersectionY,10,10);
if(this._collideDebug){
this.ellipse(intersectionX,intersectionY,10,10);
}

if(calcIntersection){
Expand Down Expand Up @@ -182,10 +182,10 @@ p5.prototype.collideLineRect = function(x1, y1, x2, y2, rx, ry, rw, rh, calcInte
var left, right, top, bottom, intersection;

if(calcIntersection){
left = collideLineLine(x1,y1,x2,y2, rx,ry,rx, ry+rh,true);
right = collideLineLine(x1,y1,x2,y2, rx+rw,ry, rx+rw,ry+rh,true);
top = collideLineLine(x1,y1,x2,y2, rx,ry, rx+rw,ry,true);
bottom = collideLineLine(x1,y1,x2,y2, rx,ry+rh, rx+rw,ry+rh,true);
left = this.collideLineLine(x1,y1,x2,y2, rx,ry,rx, ry+rh,true);
right = this.collideLineLine(x1,y1,x2,y2, rx+rw,ry, rx+rw,ry+rh,true);
top = this.collideLineLine(x1,y1,x2,y2, rx,ry, rx+rw,ry,true);
bottom = this.collideLineLine(x1,y1,x2,y2, rx,ry+rh, rx+rw,ry+rh,true);
intersection = {
"left" : left,
"right" : right,
Expand All @@ -194,10 +194,10 @@ p5.prototype.collideLineRect = function(x1, y1, x2, y2, rx, ry, rw, rh, calcInte
}
}else{
//return booleans
left = collideLineLine(x1,y1,x2,y2, rx,ry,rx, ry+rh);
right = collideLineLine(x1,y1,x2,y2, rx+rw,ry, rx+rw,ry+rh);
top = collideLineLine(x1,y1,x2,y2, rx,ry, rx+rw,ry);
bottom = collideLineLine(x1,y1,x2,y2, rx,ry+rh, rx+rw,ry+rh);
left = this.collideLineLine(x1,y1,x2,y2, rx,ry,rx, ry+rh);
right = this.collideLineLine(x1,y1,x2,y2, rx+rw,ry, rx+rw,ry+rh);
top = this.collideLineLine(x1,y1,x2,y2, rx,ry, rx+rw,ry);
bottom = this.collideLineLine(x1,y1,x2,y2, rx,ry+rh, rx+rw,ry+rh);
}

// if ANY of the above are true, the line has hit the rectangle
Expand Down Expand Up @@ -255,13 +255,13 @@ p5.prototype.collideCirclePoly = function(cx, cy, diameter, vertices, interior)
var vn = vertices[next]; // n for "next"

// check for collision between the circle and a line formed between the two vertices
var collision = collideLineCircle(vc.x,vc.y, vn.x,vn.y, cx,cy,diameter);
var collision = this.collideLineCircle(vc.x,vc.y, vn.x,vn.y, cx,cy,diameter);
if (collision) return true;
}

// test if the center of the circle is inside the polygon
if(interior == true){
var centerInside = collidePointPoly(cx,cy, vertices);
var centerInside = this.collidePointPoly(cx,cy, vertices);
if (centerInside) return true;
}

Expand All @@ -287,12 +287,12 @@ p5.prototype.collideRectPoly = function( rx, ry, rw, rh, vertices, interior) {
var vn = vertices[next]; // n for "next"

// check against all four sides of the rectangle
var collision = collideLineRect(vc.x,vc.y,vn.x,vn.y, rx,ry,rw,rh);
var collision = this.collideLineRect(vc.x,vc.y,vn.x,vn.y, rx,ry,rw,rh);
if (collision) return true;

// optional: test if the rectangle is INSIDE the polygon note that this iterates all sides of the polygon again, so only use this if you need to
if(interior == true){
var inside = collidePointPoly(rx,ry, vertices);
var inside = this.collidePointPoly(rx,ry, vertices);
if (inside) return true;
}
}
Expand All @@ -317,7 +317,7 @@ p5.prototype.collideLinePoly = function(x1, y1, x2, y2, vertices) {
var y4 = vertices[next].y;

// do a Line/Line comparison if true, return 'true' immediately and stop testing (faster)
var hit = collideLineLine(x1, y1, x2, y2, x3, y3, x4, y4);
var hit = this.collideLineLine(x1, y1, x2, y2, x3, y3, x4, y4);
if (hit) {
return true;
}
Expand All @@ -344,12 +344,12 @@ p5.prototype.collidePolyPoly = function(p1, p2, interior) {
var vn = p1[next]; // n for "next"

//use these two points (a line) to compare to the other polygon's vertices using polyLine()
var collision = collideLinePoly(vc.x,vc.y,vn.x,vn.y,p2);
var collision = this.collideLinePoly(vc.x,vc.y,vn.x,vn.y,p2);
if (collision) return true;

//check if the 2nd polygon is INSIDE the first
if(interior == true){
collision = collidePointPoly(p2[0].x, p2[0].y, p1);
collision = this.collidePointPoly(p2[0].x, p2[0].y, p1);
if (collision) return true;
}
}
Expand All @@ -360,12 +360,12 @@ p5.prototype.collidePolyPoly = function(p1, p2, interior) {
p5.prototype.collidePointTriangle = function(px, py, x1, y1, x2, y2, x3, y3) {

// get the area of the triangle
var areaOrig = abs( (x2-x1)*(y3-y1) - (x3-x1)*(y2-y1) );
var areaOrig = this.abs( (x2-x1)*(y3-y1) - (x3-x1)*(y2-y1) );

// get the area of 3 triangles made between the point and the corners of the triangle
var area1 = abs( (x1-px)*(y2-py) - (x2-px)*(y1-py) );
var area2 = abs( (x2-px)*(y3-py) - (x3-px)*(y2-py) );
var area3 = abs( (x3-px)*(y1-py) - (x1-px)*(y3-py) );
var area1 = this.abs( (x1-px)*(y2-py) - (x2-px)*(y1-py) );
var area2 = this.abs( (x2-px)*(y3-py) - (x3-px)*(y2-py) );
var area3 = this.abs( (x3-px)*(y1-py) - (x1-px)*(y3-py) );

// if the sum of the three areas equals the original, we're inside the triangle!
if (area1 + area2 + area3 == areaOrig) {
Expand All @@ -379,7 +379,7 @@ p5.prototype.collidePointPoint = function (x,y,x2,y2, buffer) {
buffer = 0;
}

if(dist(x,y,x2,y2) <= buffer){
if(this.dist(x,y,x2,y2) <= buffer){
return true;
}

Expand Down
1 change: 1 addition & 0 deletions p5.collide2d.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cfb0530

Please sign in to comment.