Skip to content

Line/Rect collision fix #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CodeExamples/LineRect/LineRect.pde
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ void draw() {

// LINE/RECTANGLE
boolean lineRect(float x1, float y1, float x2, float y2, float rx, float ry, float rw, float rh) {

// is either end INSIDE the rectangle?
// if so, return true immediately
boolean inside1 = pointRect(x1,y1, rx,ry,rw,rh);
boolean inside2 = pointRect(x2,y2, rx,ry,rw,rh);
if (inside1 || inside2) return true;

// check if the line has hit any of the rectangle's sides
// uses the Line/Line function below
Expand All @@ -62,6 +68,20 @@ boolean lineRect(float x1, float y1, float x2, float y2, float rx, float ry, flo
}


// POINT/RECTANGLE
boolean pointRect(float px, float py, float rx, float ry, float rw, float rh) {

// is the point inside the rectangle's bounds?
if (px >= rx && // right of the left edge AND
px <= rx + rw && // left of the right edge AND
py >= ry && // below the top AND
py <= ry + rh) { // above the bottom
return true;
}
return false;
}


// LINE/LINE
boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {

Expand Down
21 changes: 20 additions & 1 deletion CodeExamples/LineRect/web-export/LineRect.pde
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ void draw() {

// LINE/RECTANGLE
boolean lineRect(float x1, float y1, float x2, float y2, float rx, float ry, float rw, float rh) {

// is either end INSIDE the rectangle?
// if so, return true immediately
boolean inside1 = pointRect(x1,y1, rx,ry,rw,rh);
boolean inside2 = pointRect(x2,y2, rx,ry,rw,rh);
if (inside1 || inside2) return true;

// check if the line has hit any of the rectangle's sides
// uses the Line/Line function below
Expand All @@ -64,6 +70,20 @@ boolean lineRect(float x1, float y1, float x2, float y2, float rx, float ry, flo
}


// POINT/RECTANGLE
boolean pointRect(float px, float py, float rx, float ry, float rw, float rh) {

// is the point inside the rectangle's bounds?
if (px >= rx && // right of the left edge AND
px <= rx + rw && // left of the right edge AND
py >= ry && // below the top AND
py <= ry + rh) { // above the bottom
return true;
}
return false;
}


// LINE/LINE
boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {

Expand All @@ -87,4 +107,3 @@ boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, flo
}



11 changes: 9 additions & 2 deletions Website/line-rect.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

<h1>LINE/RECTANGLE</h1>

<p>We've actually already covered how to check if a line has hit a rectangle: it's really just four <a href="line-line.php">Line/Line</a> collisions, one for each side!</p>
<p>We've actually already covered how to check if a line has hit a rectangle: it's really just two <a href="point-rect.php">Point/Rect</a> and four <a href="line-line.php">Line/Line</a> collisions, one for each side!</p>

<p>For example, the left edge of the square starts at <code>(rx,ry)</code> and extends down to <code>ry+rh</code>. We can treat that as a line, using the algorithm we made in the last section:</p>
<p>First, let's test if either of the ends of the line are inside the square. This is likely to happen if the line is much smaller than the square. To do this, we can use <a href="point-rect.php">Point/Rect</a> from the beginning of the book. If either end is inside, return <code>true</code> immediately and skip the rest.</p>

<pre>boolean inside1 = pointRect(x1,y1, rx,ry,rw,rh);
boolean inside2 = pointRect(x2,y2, rx,ry,rw,rh);
if (inside1 || inside2) return true;
</pre>

<p>Next, we check for collision between the line and any edge of the square. For example, the left edge of the square starts at <code>(rx,ry)</code> and extends down to <code>ry+rh</code>. We can treat that as a line, using the algorithm we made in the last section:</p>

<pre>boolean left = lineLine(x1,y1,x2,y2, rx,ry, rx,ry+rh);
</pre>
Expand Down