# How to make squares bounce off of each other?

please format code with </> button * homework policy * asking questions

``````int []xspeed;
int []yspeed;
int []xcor;
int []ycor;
int []direction;
int []steps;
boolean movin[];
boolean gamemode;
float r[];
float b[];
float g[];
boolean flashmode[];
//all the arrays and variables
void setup(){
size(400,400);
xcor= new int;
ycor= new int;
xspeed= new int;
yspeed= new int;
direction= new int;
steps= new int;
movin= new boolean;
r= new float;
b= new float;
g= new float;
gamemode=false;
flashmode = new boolean;
setvalues();
//all arrays are defined
}
void draw(){
if(!gamemode&&!allstop()){
begin();
}
else if(gamemode&&!allstop()){
background(255);
boxes();
Move();
Bounce();
}
else if(allstop()&&gamemode){
end();
}
//executes all functions needed
}
void boxes(){
flash();
strokeWeight(4);
for(int i = 0; i < 8; i++){
if(movin[i]){
fill(r[i],b[i],g[i]);
rect(xcor[i],ycor[i],30,30);
}
}
//displays are squares
}
void Move(){
for(int i = 0; i < 8; i++){
if(movin[i]){
if(direction[i]==1){
xcor[i]+=xspeed[i];
steps[i]++;
}
else if(direction[i]==2){
xcor[i]-=xspeed[i];
steps[i]++;
}
else if(direction[i]==2){
ycor[i]+=yspeed[i];
steps[i]++;
}
else{
ycor[i]-=yspeed[i];
steps[i]++;
}
if(steps[i]%10==0){
direction[i] = direction_change();
}
}
}
//makes all squares move and move in a random pattern
}
void Bounce(){
for(int i = 0; i < 8; i++){
if(xcor[i]<0||xcor[i]>367){
xspeed[i]*=-1;
}
if(ycor[i]<0||ycor[i]>367){
yspeed[i]*=-1;
}
}
//bounces squares off the wall
}
int direction_change(){
return int(random(1,5));
//changes the direction once steps reach a certain point
}
boolean allstop(){
for(int i = 0; i < 8; i++){
if(movin[i]){
return false;
}
}
return true;
//checks to see if all squares have stopped
}
void begin(){
background(185);
fill(40,220,20);
rect(150,300,100,50);
fill(0,0,255);
textSize(25);
textAlign(CENTER);
text("Crazy Squares",200,150);
text("EPILEPSY WARNING!",200,100);
textSize(11);
text("Click inside the square twice. This increases the speed and changes color.",200,175);
text("Once CRAZY, press any key while mouse inside the square to destroy it.",200,200);
text("Win by destroying all of them!",200,225);
text("Press play to begin!",200,250);
fill(0);
textSize(30);
text("Play",200,335);
//Begining screen once you run the code
}
void end(){
background(185);
fill(40,20,220);
rect(50,300,100,50);
fill(40,220,20);
rect(250,300,100,50);
fill(0,0,255);
textSize(35);
textAlign(CENTER);
text("Nice! You won!",200,200);
fill(0);
textSize(15);
text("Play Again",300,330);
textSize(30);
//end screen once you freeze all squares
}
void setvalues(){
for(int i = 0; i < 8; i++){
xcor[i] = 0 + 50 * i;
ycor[i] = 0 + 50 * i;
xspeed[i] = 1;
yspeed[i] = 1;
direction[i] = direction_change();
steps[i] = 0;
movin[i] = true;
flashmode[i] = false;
r[i] = 0;
b[i] = 0;
g[i] = 0;
}
}
void flash(){
for(int i = 0; i < 8; i++){
if((xspeed[i]==3||xspeed[i]==-3)&&(yspeed[i]==3||yspeed[i]==-3)){
if(r[i]==255){
r[i]=0;
b[i]=0;
g[i]=255;
}
else if(b[i]==255){
r[i]=255;
b[i]=0;
g[i]=0;
}
else if(g[i]==255){
r[i]=0;
b[i]=255;
g[i]=0;
}
flashmode[i]=true;
}
else if((xspeed[i]==2||xspeed[i]==-2)&&(yspeed[i]==2||yspeed[i]==-2)){
r[i]=255;
b[i]=255;
g[i]=255;
}
else if(!movin[i]||((xspeed[i]==1||xspeed[i]==-1)&&(yspeed[i]==1||yspeed[i]==-1))){
r[i]=0;
b[i]=0;
g[i]=0;
}
}
//sets the color of the squares
}
void mousePressed(){
for(int i = 0; i < 8; i++){
if(mouseX>=xcor[i]&&mouseX<=xcor[i]+30&&mouseY>=ycor[i]&&mouseY<=ycor[i]+30){
if(xspeed[i]>0&&xspeed[i]<3){
xspeed[i]+=1;
}
else if(xspeed[i]<0&&xspeed[i]>-3){
xspeed[i]-=1;
}
if(yspeed[i]>0&&yspeed[i]<3){
yspeed[i]+=1;
}
else if(yspeed[i]<0&&yspeed[i]>-3){
yspeed[i]-=1;
}
}
//speeds up squares once you press them and changes their colors
}
if(gamemode&&allstop()&&mouseX>=50&&mouseX<=150&&mouseY>=300&&mouseY<=350){
setvalues();
gamemode=false;
}
if(gamemode&&allstop()&&mouseX>=250&&mouseX<=350&&mouseY>=300&&mouseY<=350){
setvalues();
}
if(!gamemode&&!allstop()&&mouseX>=150&&mouseX<=250&&mouseY>=300&&mouseY<=350){
gamemode=true;
}
//resets all the values and brings player to home screen
}
void keyPressed(){
for(int i = 0; i < 8; i++){
if(mouseX>=xcor[i]&&mouseX<=xcor[i]+30&&mouseY>=ycor[i]&&mouseY<=ycor[i]+30&&flashmode[i]&&keyPressed){
movin[i]=false;
}
//stops an individual square once you meet certain requirements
}
}
``````

Hello @cannonman

I think this tutorial on how to determine when 2 objects touch will be of use.

Hope this helps! Thanks. I understand how to make two objects interact and have borders but I’m unsure how to do it with arrays with multiple objects.

I’m not confident enough in working with arrays to offer guidance, so will have to step back…

One suggestion that may elicit more responses is if you posted an abbreviated version of your code that focuses specifically on the issue you are working thru. So in your case an array with multiple objects bouncing off each other. That is, show an attempt, eventhough you know the code or logic may be incorrect. Otherwise, people need to comb thru a lot of code that is not relevant to your specific question. 