We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I have been trying to make this irregular triangle bounce. Now it works on the top but not on the three other boundaries.
Could somebody help? Thank you !
Here is the code: It displays well in this Processing Online editor http://js.do/blog/processing/editor/#!star_painted
boolean xRight, xLeft, yDown; //, yTop;
int xPos, yPos;
int speed = 1;
void setup() {
size(250, 350);
strokeWeight(5);
xRight = true;
yDown = true;
xPos = width/2;
yPos = height/2;
}
void draw() {
background(204);
// Control right
if (xRight) xPos += speed;
else xPos -= speed;
if (xPos +76.67>= width || xPos <= 0) xRight = !xRight;
// Control left
if (xLeft) xPos -= speed;
else xPos += speed;
if (xPos -23.33 >= width || xPos >= 0) xLeft = !xLeft;
// Control down and up
if (yDown) yPos += speed;
else yPos -= speed;
if (yPos >= height || yPos -124.33 <= 0) yDown = !yDown;
driehoek(xPos, yPos);
}
void driehoek(int xLinks, int yLinks) {
int xA = xLinks -53.33;
int yA = yLinks -4.33;
int xB = xLinks -23.33;
int yB = yLinks +95.67;
int xC = xLinks +76.67;
int yC = yLinks -124.33;
triangle(xA, yA, xB, yB, xC, yC);
}
Answers
@Salutflo --
This is a collision detection problem.
Conceptually you can check:
That's four checks, but one of your three triangle points (C) is getting checked twice -- it is both the highest and the furthest right.
For the general case, I recommend consulting the Poly / Line example from Jeffrey Thompson's guide to collision detection:
Your triangle is the polygon, and each bounding line of the box is a line to check collision against. If it seems complex, go back and read through the previous examples leading up to that one.
Note also that you could draw your irregular triangle as a PShape and then simply use PShape.width and PShape.height to check if the bounding box of your shape is inside the screen area.
P.S. Please edit your post and fix the int->float in your demo code above, which does not run.
@Salutflo
Here is the code, as @jeremydouglass said is neccesary to focus on the rightmost, leftmost, highest and lowest
Thank you very much @jeremydouglass and @laimperiestro ! Very helpful.