We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
You need -
I think I know how to handle collision detection, but the force calculation still has me stumped...
You'd need to learn about vectors for that. Then learn how the forces work etc. A lot of work. Or, more easy, just use some library like Box2D for Processing. It will handle everything for you, and you'll be able to create far more complex simulations also.
For an overview of working with Box2D in Processing see Shiffman's Nature of Code Ch.5--Physics Libraries.
There is also a discussion of force calculations using vectors in Ch2--Forces.