Circle Line Intersect Reaction
in
Programming Questions
•
2 years ago
Hello!
I am new to processing and this is my first attempt at a simple game. A pendulum swinging at the top of the window interacts with a moving target, the pendulum being a line and the target a circle. I want the circle to rebound off the pendulum when they intersect. So far I have been able to detect if there is an intersection and which side of the line the intersection happens on. But, when I try to reverse the x direction, after an intersection, to look like the circle is bouncing off, the circle freaks out.
Any suggestions?
Here is my code so far:
//circle target
noStroke();
ellipse(circleX,circleY,circleR,circleR);
circleX += (speedX+(millis()/8000)) * directionX;
if ((circleX > width-circleR) || (circleX < circleR)) {
directionX = -directionX;
}
circleY += speedY * directionY;
if ((circleY > height-circleR) || (circleY < circleR)) {
directionY = -directionY;
}
//pendulum
float pendX2=((cos(angle))*250)+width/2;
float pendY2=(sin(angle))*250;
angle+=speedP*directionP;
stroke(75);
strokeWeight(5);
strokeCap(SQUARE);
line(pendX,pendY,pendX2,pendY2);
if((angle>PI) || (angle<0)) {
directionP=-directionP;
}
if (circleLineIntersect(pendX, pendY,pendX2,pendY2, circleX, circleY, circleR) == true ) {
if(side(pendX,pendY,pendX2,pendY2,circleX,circleY)<0) {
println("LEFT");
directionX=-directionX;
}
}
//booleans
float side ( float Sx1, float Sy1, float Sx2, float Sy2, float pointSx, float pointSy )
{
return (Sx2 - Sx1) * (pointSy - Sy1) - (Sy2 - Sy1) * (pointSx - Sx1);
}
boolean circlecircleIntersect(float Cx1, float Cy1, float Cr1, float Cx2, float Cy2, float Cr2) {
return dist(Cx1, Cy1, Cx2, Cy2) < Cr1 + Cr2;
}
//Adapted from C.Reas on OpenProcessing
boolean circleLineIntersect(float x1, float y1, float x2, float y2, float cx, float cy, float cr ) {
float dx = x2 - x1;
float dy = y2 - y1;
float a = dx * dx + dy * dy;
float b = 2 * (dx * (x1 - cx) + dy * (y1 - cy));
float c = cx * cx + cy * cy;
c += x1 * x1 + y1 * y1;
c -= 2 * (cx * x1 + cy * y1);
c -= cr * cr;
float bb4ac = b * b - 4 * a * c;
//println(bb4ac);
if (bb4ac < 0) { // Not intersecting
return false;
}
else {
float mu = (-b + sqrt( b*b - 4*a*c )) / (2*a);
float ix1 = x1 + mu*(dx);
float iy1 = y1 + mu*(dy);
mu = (-b - sqrt(b*b - 4*a*c )) / (2*a);
float ix2 = x1 + mu*(dx);
float iy2 = y1 + mu*(dy);
// The intersection points
//ellipse(ix1, iy1, 10, 10);
//ellipse(ix2, iy2, 10, 10);
float testX;
float testY;
// Figure out which point is closer to the circle
if (dist(x1, y1, cx, cy) < dist(x2, y2, cx, cy)) {
testX = x2;
testY = y2;
}
else {
testX = x1;
testY = y1;
}
if (dist(testX, testY, ix1, iy1) < dist(x1, y1, x2, y2) || dist(testX, testY, ix2, iy2) < dist(x1, y1, x2, y2)) {
return true;
}
else {
return false;
}
}
}
1