rect line intersection problem
in
Programming Questions
•
4 months ago
hi i nicked this code from
http://www.openprocessing.org/sketch/8010
but adapting it shows some unexpected behaviour
if lineX1 and lineX2 parameters are the same the collision detection don't work anymore
changing a bit one of the parameters solves the problem
second, i want something to happen just once when collision happens not every frame
using boolean for this again breaks the collision detection
i know this is a mess but if someone can help with this, i would really appreciate it, thank you
but adapting it shows some unexpected behaviour
if lineX1 and lineX2 parameters are the same the collision detection don't work anymore
changing a bit one of the parameters solves the problem
second, i want something to happen just once when collision happens not every frame
using boolean for this again breaks the collision detection
i know this is a mess but if someone can help with this, i would really appreciate it, thank you
float movex,a,b,wiggle,wiggle2;
float rect_diam=20;
boolean maxormin = false;
Rect_line test;
void setup() {
size(400, 400);
ellipseMode(RADIUS);
smooth();
}
void draw() {
background(0);
wiggle=noise(a)*width;
wiggle2=noise(b,20)*height*0.4;
test= new Rect_line(movex+0.1,height*0.4,movex,height*0.6,wiggle,height*0.5,rect_diam,rect_diam);
test.display();
a+=0.01;
b+=0.03;
}
class Rect_line {
float lineX1;
float lineY1;
float lineX2;
float lineY2;
float rectX;
float rectY;
float rectWidth;
float rectHeight;
float thick;
boolean touching;
Rect_line(float _lineX1,float _lineY1,float _lineX2,float _lineY2,float _rectX,float _rectY,float _rectWidth,float _rectHeight){
lineX1 = _lineX1;
lineY1 = _lineY1;
lineX2=_lineX2;
lineY2=_lineY2;
rectX = _rectX;
rectY = _rectY;
rectWidth = _rectWidth;
rectHeight = _rectHeight;
// breaks the collision detection if activiated
//touching= lineRectangleIntersect(lineX1, lineY1, lineX2, lineY2, rectX, rectY, rectWidth, rectHeight);
}
void display(){
if (lineRectangleIntersect(lineX1, lineY1, lineX2, lineY2, rectX, rectY, rectWidth, rectHeight) == true &&
touching != lineRectangleIntersect(lineX1, lineY1, lineX2, lineY2, rectX, rectY, rectWidth, rectHeight))
{
touching= lineRectangleIntersect(lineX1, lineY1, lineX2, lineY2, rectX, rectY, rectWidth, rectHeight);
fill(random(255));
}
else {
fill(0);
touching= lineRectangleIntersect(lineX1, lineY1, lineX2, lineY2, rectX, rectY, rectWidth, rectHeight);
};
stroke(255);
strokeCap(PROJECT);
line(lineX1, lineY1, lineX2, lineY2);
rectMode(CENTER);
strokeWeight(1);
rect(rectX, rectY, rectWidth, rectHeight);
line(0,height/2,width,height/2);
move();
}
void move(){
if (movex <= 0) {
maxormin = true;
}
if (movex >= width) {
maxormin = false;
}
if (maxormin == true) { //if you've hit the min, increase your value
movex +=1;
}
if (maxormin == false) { //if you've hit the max, decremet your value
movex -=1;
}
}
boolean lineRectangleIntersect(float x1, float y1, float x2, float y2,
float rx, float ry, float rw, float rh) {
float topIntersection;
float bottomIntersection;
float topPoint;
float bottomPoint;
// Calculate m and c for the equation for the line (y = mx+c)
float m = (y2-y1) / (x2-x1);
float c = y1 -(m*x1);
// If the line is going up from right to left then the top intersect point is on the left
if(m > 0) {
topIntersection = (m*rx + c);
bottomIntersection = (m*(rx+rw) + c);
}
// Otherwise it's on the right
else {
topIntersection = (m*(rx+rw) + c);
bottomIntersection = (m*rx + c);
}
// Work out the top and bottom extents for the triangle
if(y1 < y2) {
topPoint = y1;
bottomPoint = y2;
} else {
topPoint = y2;
bottomPoint = y1;
}
float topOverlap;
float botOverlap;
// Calculate the overlap between those two bounds
topOverlap = topIntersection > topPoint ? topIntersection : topPoint;
botOverlap = bottomIntersection < bottomPoint ? bottomIntersection : bottomPoint;
return (topOverlap<botOverlap) && (!((botOverlap<ry) || (topOverlap>ry+rh)));
}
}
1