void Ball(){
fill(255,0,0);
triangle (xa,ya,xb,yb,xc,yc);
//have ALL of your points moving
xa=xa+xSpeed;
xb=xb+xSpeed;
xc=xc+xSpeed;
ya=ya+ySpeed;
yb=yb+ySpeed;
yc=yc+ySpeed;
if (xa>width || xa<0 || xb>width || xb<0 || xc>width || xc<0){ //if ANY of the X values s goes out of bounds, reverse everyone's speed
xSpeed=xSpeed*-1;
}
if (ya>height || ya<0 || yb>height || yb<0 || yc>height || yc<0 ){ //if ANY of the Y values s goes out of bounds, reverse everyone's speed
ySpeed=ySpeed*-1;
}
}
You have all the right code, just the problem is you’re a little too specific. What you want, is that as soon as ANY point of the triangle goes out, to change xSpeed or ySpeed to the opposite direction. And then the triangle won’t grow or stretch. It used to grow, because sometimes some points would be going at an opposite xSpeed, while the rest would keep on going normally.
In general – with a triangle, rectangle, or anything else – don’t apply bounce rules to each point individually. This is asking for trouble.
Instead, track the center point of the object, and only move that – then draw the perimeter relative to that center. You can still do bounce checking logic on those perimeter points, but calculate them on the fly from the center – that way you only have one center, and the shape can never distort. (Unless you want it to distort!)