#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Irregular triangle bounce

edited December 2016

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);

}
``````
Tagged:

## Answers

• This is a collision detection problem.

Conceptually you can check:

1. is the highest point higher than the top of the box?
2. is the leftmost point further than the left of the box?
3. is the rightmost point further than the right of the box?
4. is the lowest point lower than the bottom of the box?

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

``````boolean xHorizontal, yVertical;
float xPos, yPos;
float x1,y1,x2,y2,x3,y3;
int speed = 1;

void setup() {
size(250, 350);
strokeWeight(5);

xHorizontal = true;
yVertical=true;

/*xPos = width/2;
yPos = height/2;*/
xPos=random(250);
yPos=random(350);
}

void draw() {
background(204);

// Control at sides
if (xHorizontal){
xPos -= speed;
if (x1 <= 0){ //the leftmost
xHorizontal = !xHorizontal;
}
}
else{
xPos += speed;
if(x3>=width){//the rightmost
xHorizontal = !xHorizontal;
}
}

// Control up-down
if (yVertical){
yPos += speed;
if (y2 >= height){ //the lowest
yVertical = !yVertical;
}
}
else{
yPos -= speed;
if(y3<=0){ //the highest
yVertical = !yVertical;
}
}
driehoek(xPos, yPos);
}

void driehoek(float xLinks, float yLinks) {
x1 = xLinks -53.33;
y1 = yLinks -4.33;
x2 = xLinks -23.33;
y2 = yLinks +95.67;
x3 = xLinks +76.67;
y3 = yLinks -124.33;
triangle(x1, y1, x2, y2, x3, y3);
}
``````
• Thank you very much @jeremydouglass and @laimperiestro ! Very helpful.

Sign In or Register to comment.