hi all
i'm attending a course of Processing, and i have an exercise that i'm having a big trouble with understanding array issues.
we have to develop a multiple collision detection system. the objective of the exercise is to make the balls invert their speed when colliding with the borders of the canvas, and also with another one of the balls.
i'm posting the code of what i did below. object collision is commented.
Quote://aula6 exemplo2
//eduardo fernandes. 25/09/08
int x[];//x position
int y[];//y position
int raio[];//radius
int vx[];//x speed
int vy[];//y speed
int n;//number of objects
void setup(){
size(512,512);
background(0);
fill(255);
smooth();
noStroke();
ellipseMode(CENTER);
n=4;
x=new int[n];
y=new int[n];
raio=new int[n];
vx=new int[n];
vy=new int[n];
for(int i=0; i<n; i++){
x[i]=int(random(512));
y[i]=int(random(512));
raio[i]=int(random(10,30));
vx[i]=int(random(2,6));
vy[i]=int(random(2,6));
}
}
void draw(){
background(0);
for(int i=0; i<n; i++){
//distance between balls
float d[]=new float[i];
d[i]=sqrt(sq(x[i+1]-x[i])+sq(y[i+1]-y[i]));
//actualizes ball speed
x[i]=x[i]+vx[i];
y[i]=y[i]+vy[i];
//wall collision
if(x[i]<0 || x[i]>512){
vx[i]=-vx[i];
x[i]=x[i]+vx[i];
}
if (y[i]<0 || y[i]>512){
vy[i]=-vy[i];
y[i]=y[i]+vy[i];
}
//object collision
/*if (d[i]>raio[i]){
}
else{
vx[i]=-vx[i];
vy[i]=-vy[i];
x[i]=x[i]+vx[i];
y[i]=y[i]+vy[i];
}*/
}
for(int i=0; i<n; i++){
ellipse(x[i],y[i],raio[i]*2,raio[i]*2);
}
}
sofar i had sucess in making the objects bounce on the walls, but i had trouble in making balls recognize other balls.
we have been teached that a good way to measure distance between points is using good old pythagorean theorem a=sqr(sq(a)+sq(b)), so i used
Quote: float d[]=new float[i];
d[i]=sqrt(sq(x[i+1]-x[i])+sq(y[i+1]-y[i]));
i've tried a lot of different ways to measure the distance and i always get different responses, all but what i need to do.
i think the the error i'm making is that i'm not being able to differentiate one ball from the others, and estabilish that differentiation on the calculation of distances, for a different number of "particles". the usual error is to make the calculation equals zero, so it won't move. other issue is that i'm not interacting with array correctly.
can anybody assist me? i have seen a lot of examples of this over the web, but they are all OOP, and i haven't learned that already, so anything that goes in that direction won't be of use(in this phase of the course, at least).
thanks