How do I calculate the velocities of a ball bouncing inside an equilateral triangle?

edited February 2017 in Questions about Code

I know how to make a ball bounce in rectangle like this:

int x=width/2;
int y=height/2;
int xSpeed=int(random(10));
int ySpeed=int(random(10));
void setup() {
  size(300, 300);
}

void draw() {
  background(0);
  x+=xSpeed;
  y+=ySpeed;
  if (x+15>=width || x-15<=0) {
    xSpeed *=-1;
  }

  if (y+15>=height || y-15<=0) {
    ySpeed *=-1;
  }
  ellipse(x, y, 30, 30);
}

However, when I try to bounce it inside of an equilateral triangle I can't figure out how I should affect the velocities on the edges that are tilted in relation to the x and y axis...

Answers

Sign In or Register to comment.